OOPS Concepts – Inheritance, Polymorphism, Abstraction and Encapsulation

OOPS Concepts – Inheritance, Polymorphism, Abstraction and Encapsulation:
What is OOPS?
Object Oriented Programming (OOP) is a methodology to write a program using classes and objects.

What is Inheritance?
When one object gets all the properties of super class or parent class is known as Inheritance. Subclass can also have its own properties in addition to parent class properties.

What is Polymorphism?
An object that takes various forms is known as Polymorphism.

What is Abstraction?
Hiding implementation details and showing essential features is known as abstraction.

What is Encapsulation?
Encapsulating (wrapping) the data and code into a single entity is known as Encapsulation.

Encapsulation and Abstraction seems like almost same, Let us see the difference between encapsulation and abstraction.

Abstraction Encapsulation
Abstraction helps at design level Encapsulation helps at implementation level
Abstraction hides irrelevant data and shows what is required Encapsulation hides the data to protect it from other developers

Inheritance Example:
We have a class Employee that has one property employeeName. We have another class ContractEmployee that has property salary. Now ContractEmployee extends Employee class. So ContractEmployee class can use the property  employeeName of Employee Class. This is known as Inheritance.

Output: Inheritance

When one object takes various forms. Before proceeding to example, We must know what is Method Overriding and Method Overloading in Polymorphism.

Method Overriding:
When same method name used in both Parent class and subclass with same arguments and same return type, Then the method can be overridden.

We need to make sure,

  • Arguments and Return type should be same for the method in parent class and subclass
  • Subclass must extend parent class to implement Method Overriding
  • Access level should not be restrictive

Method Overloading:
Method overloading is two or more methods having same name both in parent class and subclasses but with different arguments.

In this example, We have a method Display() in Parent class (Employee) and Subclass (ContractEmployee). We have a Calculate method both in Parent class and Subclass but with different arguments. We have created objects and we call methods in Display() method. We have implemented both Method Overriding and Method Overloading in this example.

Output: Polymorphism


Hiding Irrelevant data and show only necessary data.

In this example, We have an abstract class with an abstract method and non-abstract method. So when abstract class is extended, it shows abstract method but hides non-abstract method. Showing only the necessary method and hiding unwanted method is known as abstraction.

Output: Abstraction


Hiding data from outside world.

In this example, We have two properties, String name and string department. We have assigned name = “Beta” and department = “Java”. These properties are accessible only within the same class and outside class cannot access these properties. But outside class can use getters and setters for accessing variables. Thus the Beta and Java are hided to outside class.

Output: Encapsulation


One thought to “OOPS Concepts – Inheritance, Polymorphism, Abstraction and Encapsulation”

Leave a Reply

Your email address will not be published. Required fields are marked *