top of page
sirishasaripalli20

Four main pillars of Object Oriented Programming concepts of Java

Overview:

In this article, we will learn the basics about Java and the oops concepts.

 

Basic Concepts:

Java is Object Oriented Programming Language used to solve any real-world problems which converts high level language into machine language. It is an open-source technology and platform independent. The four major properties of Java are P, I,A,E which is Polymorphism, Interface, Abstraction and Encapsulation.


Polymorphism: Ploymorphism is one of the most important pillar of Object Oriented programming. Program activates dynamically according to the behavior of the application. Poly means “many” and Morphs means “forms”, So Polymorphisms means many forms. One method can take more than one form. It is the capability of a method or function to do different actions based on the object that it is acting upon. There are two different types of polymorphism.


1.Static or Compile time or Early binding (Over Loading)

2.Dynamic or Runtime or Late Binding (Overriding)


Static or Compile time or early binding Polymorphism is achieved by Overloading in Java. When the method name is same with different arguments or input parameters within the same class is called Overloading. Method overloading cannot be implemented in Inheritance. It is called compile time polymorphism because the compiler decides which function to call based on the input parameters or number of arguments at the compilation.

 


The second type of polymorphism is Dynamic, Runtime or Late Binding Polymorphism which is achieved by Overriding in Java. When the same method is present in parent class and as well as in child class with the same name and same number of arguments it is called overriding. When a super class method is overridden in the sub class it is called method overriding. Base class/super class method is overriden by subclass/child class. Method body inside the child class is executed by overridden method in Runtime polymorphism.



Inheritance: Inheritance is the process of inheriting some properties from parents. Child class will inherit all the methods or properties from its parent class. Parent class can have many child classes, but a child class can have only one parent class. Inheritance represents IS-A-Relationship or Parent Child Relationship between objects. You must use the Extends keyword in the child class to get the properties of parent class.


There are different types of Inheritance in Java


1.      Single Inheritance: When a class inherits another class it is called Single Inheritance. Class B inherits Class A by using the extends keyword. A-->B

2.      Multi-level Inheritance: When there is chain of Inheritance then it is Multi-level Inheritance. Class C inherits Class B and Class B inherits Class A by using the extends keyword. A--> B--> C

3.      Multiple Inheritance: Class C inherits Class A and B . Here both A and B are super class and Class C is the child class or the sub class. This is not supported by Java. A + B --> C (We can achieve this partially by interface)

4.      Hierarchy Inheritance: When two or more classes inherit the same class then it is Hierarchy Inheritance. Class B, C and D inherit Class A in hierarchy order.  A-->B, A-->C, A-->D

      

 

Abstraction: Another pillar of the Java Object oriented Programming is Abstraction. Both Abstract Class and interface are used to create Abstraction. You need to use the keyword Abstract to make a class as Abstract Class. If a particular class is Abstract, then it should have at least one abstract method (With no Method body).  Abstract class can have Abstract methods and non-Abstract methods.  Abstract class is used to hide the implementation logic. You cannot create object of the Abstract class, it cannot be instantiated by itself. It needs to be subclassed by another class to use its properties . Through Abstract class you can achieve partial abstraction. Abstract Class can have static, final,non-final and non-static Variables.


 Interface: The other way to achieve abstraction is by Interface. You can achieve 100% abstraction with Interface as all the methods in an Interface are by default Abstract and can be implemented by any class that implements the Interface. You only create a blueprint or prototype in interface. You cannot create an object of the interface class. Interface can only have final and static variables.

 

 

     

 When you extend the Interface with implements keyword it will suggest to add the unimplemented methods. Once you click on the Add unimplemented methods, it will automatically add the unimplemented methods with @override method. This is how you can achieve 100% Abstraction by using Interface.


Encapsulation: It’s a mechanism of hiding the data members whether it can be a Variable or a method. It is part of OOPS Concept

How to implement Encapsulation in Java?

Make the instance variables private, so that these variables cannot be accessed directly from outside the class.

You must create setter and getter methods, to set and get the values of the fields.

You create the instance of the object in the main method and then use the set methods to set the values and get methods to get the values and print them on the console.

Advantages of Encapsulation:

It improves the maintenance of the code.

Flexibility and reusability

We use encapsulation to provide high level security in Java and can hide and restrict access to data variables and methods.

 Happy learning!!!

43 views

Recent Posts

See All
bottom of page