OOPs Principle in Java

In this article, we will discuss OOPs Principle with respect to Java Programming Language.

OOPs which stands for Object-Oriented Programming defines different principle approaches which needs to be adhered while constructing/developing Object-Oriented Software application.

Thus, OOPs Principle isn’t specified only to Java rather it is very broad and applies for all Object-Oriented Programs like C++, C#, Java, etc.

But here, we will discuss these principles with respect to Java language.

The 4 OOPs Principle are,

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

We will discuss 4 principles one-by-one in the above-mentioned order, as it is very much required in that way to remember it easily. Also, before delving more into these concepts, we will see simple Java class with member fields and behavior.

 

Java Class:

  • Class is the blueprint for every Object in Java (in heap space)
  • Whenever this class is defined i.e.; class with member fields (private data) and behavior (public code)
  • Then every Object created from this class, will have exactly same number of fields and this newly created object have access to this private data (fields) through code (behavior)
  • Note :- when we say created it means, it is created outside of the class otherwise it is very much accessible within class even if it is private
  • Thus, class is the basic building block in the OOPs concepts/principle

Student class with

  • 3 private member variables (fields)
  • 1 public display method (behavior)

 

1. Encapsulation:

  • Encapsulation helps to bind data (member fields) with code (behavior)
  • Where we are restricting outside view of fields (data) and allowing its access only through public methods (behavior)
  • The classical example to explain Encapsulation is Java Class, as shown in the above figure
  • Where we are making all member fields (data) are private and allowing code (behavior) to access these private data after proper authorization

 

2. Abstraction:

  • Abstraction hides critical data and allows access to relevant details to outside world
  • Like, if anyone want to access any data (member fields) then they must come through public method (behavior) for accessing fields (private data)
  • Abstraction is interchangeably called as Data Abstraction, as it is primarily focusing on how to hide data (fields)
  • And it shows/exhibit only relevant details to outside world, but how?
  • The classic example for Abstraction or rather Data Abstraction is, interface where it exposes all methods to outside world and accesses data only through these methods
  • In the below figure, we have exposed all methods through interface and hidden its implementation details which could be accessed only through interface
  • But if you noticed, then in a way 1st OOP principle Encapsulation helps to achieve 2nd OOP principle Abstraction

2.1 Student interface:

2.2 Student interface implementation:

 

3. Inheritance:

  • Inheritance helps to derive parent details to child via parent-child inheritance relationship
  • In short, making available parent properties (fields) to child by extending a class
  • Important keywords in inheritance relationship is extends when extending another class (or implements in case of interface)
  • But what do you mean by keywords extends or implements?
  • Basically, telling child-class to use parent-class’ properties and also if required, give more specialized definition to any parent-class method in child-class (method-overriding)
  • In the below figure, a general Person class is defined as parent-class
  • whereas more specialized-class Employee is defined as child-class along with inheriting properties from parent-class

3.1 Parent class –> Person.java

3.2 Child class –> Employee.java

 

4. Polymorphism:

  • In OOPs concepts, polymorphism is defined as one-name takes many-forms
  • The classical example of Polymorphism is, method-overloading and method-overriding where,
  • Method-Overloading : defining multiple method within same class with same-name with difference in sequence of input-parameters. See Figure 4.1 for method-overloading
  • Method-overloading is also called as static polymorphism, as it gets bind during compile-time itself. So, in turn alternatively referred as compile-time polymorphism
  • Method-Overriding : defining same method in parent-class as well as in deriving child-class with exactly same method-signature. See Figure 4.2 for method-overriding
  • Method-overriding is also called as dynamic polymorphism, as it gets bind during runtime only. So, in turn alternatively referred as runtime polymorphism
  • Another example for polymorphism is, parent-class reference is used to refer child-class object, as shown in the below figure

4.1 Method-Overloading:

  • display() method is overloaded with,
  • 1st method is with empty arguments and
  • other method is with 2 input-arguments

4.2 Method-Overriding:

  • display() method of parent-class is overridden in child-class with exactly same signature

 

Happy Coding !!
Happy Learning !!

Java - Throw exception when second instance is created
Java - Interview question and answers on this keyword