Object-Oriented Programming

MRINAL RANJAN
3 min readFeb 7, 2021

As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. An object-oriented language aims to implement real-world concepts into the programming. (ex:- Inheritance, Polymorphism, Hiding, etc).

What are Class and Object?

A class is what's called a blueprint and an Object is an actual product build on the blueprint.

THE FOUR PILLARS OF OOPS

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

What is Inheritance?

Inheritance is an object-oriented concept which allows code re-use by creating parent and child relationship. A child class inherits a parent’s code and can modify it if not protected. Though Inheritance allows code reuse it is not the best way to reuse code, instead, Composition is better than inheritance.

Example Code:

What is Polymorphism?

In Object-Oriented Programming, polymorphism is not a concept but a principle. It means many forms for example you in class behave like a student, in the supermarket like a customer, etc. What this means in an object-oriented programming language like Java is that, a single line of code can have multiple meanings, and which meaning is to be used depends upon various factors.

Example Code:

Types of Polymorphism:

Java supports two types of polymorphism:

  1. Compile-time polymorphism
  2. Run-time polymorphism.

Compile-time polymorphism:

Java supports compile-time polymorphism through method overloading. Polymorphism occurs in method overloading because method overloading allows access to different methods through the same interface. In method overloading, two or more methods in a class can use the same name as long as their parameter declarations are different.

Run-time polymorphism:

Run-time Polymorphism is the type of polymorphism that occurs dynamically when a program executes. Java supports run-time polymorphism by dynamically dispatching methods at run time through method overriding. For this type of polymorphism, method invocations are resolved at run time by the JVM and not at the compile time.

What is Encapsulation?

Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule that is a mixture of several medicines.

What is Abstraction?

Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces.

The keyword abstract is a non-access modifier, used for classes and methods:

  • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Conclusion:

Object-Oriented programming requires thinking about the structure of the program and planning at the beginning of coding. Looking at how to break up the requirements into simple, reusable classes that can be used to blueprint instances of objects. Overall, implementing OOP allows for better data structures and reusability, saving time in the long run.

References

--

--