Object-Oriented Programming (OOPs)

Surabhibadal
3 min readMay 9, 2021

Today let's discuss OOPS…..

Object-Oriented Programming is the type of programming in which emphasis is given to objects and classes. Before we begin let's first understand what object and class are

Object and Class

An object is a unique entity that has certain characteristics(data) and behavior(functions). Any real-world thing that we come across in our daily life is an object. Some examples are dog, cat, pen, laptop, rose, lily, hibiscus, marigold. Still confused, let's take the rose into the picture. The characteristics of rose are color, sepal length, petal length, etc. And the behavior of the rose is the smell.

Class is a blueprint from which objects are created. In other words, we can say the class is a set of similar objects. The flower is a class of similar objects like rose, lily, hibiscus, marigold.

Pillars of OOPs

The Four cardinal pillars of object-oriented programming are:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

1. Abstraction

The act of representing the essential feature without knowing the background details. For example, for diving a car you don't need to know the internal details like how ignition works, how gear mechanism works. In the case of Java abstraction is achieved with the help of abstract class and interface.

2. Encapsulation

The process of wrapping data and function into a single unit ie object. Let's take the example of cars. The car has data elements like model no, manufacturer, fuel type, year of purchase, etc. And functions of the car are driving, parking, reverse. Suppose there are 1000 cars, we won't be defining the data and functions of 1000 cars. Instead, we create a class and for every car, we create an object of that car.

Now we have to create objects of car class.

3. Inheritance

Inheritance is nothing but inheriting the properties of the parent class. The main objective of inheritance is code reusability. Let's take the example of Heavy_vehicle. Heavy_vehicle has all properties that are present in car class along with some more properties like load_capacity.

4.Polymorphism

The process of using a function for more than one purpose is called Polymorphism. Polymorphism is of two types:

  1. Static Polymorphism or function overloading

Suppose we want to calculate the area of square, and rectangle . Here we can define functions with the same name but with different list of parameters. Depending on the type of the argument passed the corresponding area will be computed.

2. Dynamic Polymorphism or function overriding

Suppose we have declared a function sort in the parent class that will sort the array in ascending order. Now we want to configure the sort function in such a way that it will sort the array in descending order. To solve this problem function overriding comes into the picture.

--

--