Object-oriented programming generally referred to as OOPS is the backbone of java as java is not a purely object oriented language but it is object oriented language. Java organizes a program around the various objects and well-defined interfaces. There are four pillars been here in OOPS which are listed below. TheseĀ conceptsĀ aim to implement real-world entities in programs.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Abstraction is a process of hiding implementation details and exposes only the functionality to the user. In abstraction, we deal with ideas and not events. This means the user will only know āwhat it doesā rather than āhow it doesā.
There are two ways to achieve abstraction in Java
- Abstract class (0 to 100%)
- Interface (100%)
Real-Life Example: A driver will focus on the car functionality (Start/Stop -> Accelerate/ Break), he/she does not bother about how the Accelerate/ brake mechanism works internally. And this is how the abstraction works.
Certain key points should be remembered regarding this pillar of OOPS as follows:
- The class should be abstract if a class has one or many abstract methods
- An abstract class can have constructors, concrete methods, static method, and final method
- Abstract class canāt be instantiated directly with the new operator. It can be possible as shown in pre tag below:
A b = new B();
- The child class should override all the abstract methods of parent else the child class should be declared with abstract keyword
Example:
Java
// Abstract class public abstract class Car { Ā Ā Ā Ā public abstract void stop(); } Ā Ā // Concrete class public class Honda extends Car { Ā Ā Ā Ā // Hiding implementation details Ā Ā Ā Ā @Override public void stop() Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "Honda::Stop" ); Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Mechanism to stop the car using break" ); Ā Ā Ā Ā } } Ā Ā public class Main { Ā Ā Ā Ā public static void main(String args[]) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Car obj Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new Honda(); // Car object =>contents of Honda Ā Ā Ā Ā Ā Ā Ā Ā obj.stop(); // call the method Ā Ā Ā Ā } } |
Pillar 2: Encapsulation
Encapsulation is theĀ process of wrapping code and data together into a single unit.
Real-Life Example:
A capsule which is mixed of several medicines. The medicines are hidden data to the end user.
In order to achieve encapsulation in java follow certain steps as proposed below:
- Declare the variables as private
- Declare the setters and getters to set and get the variable values
Note: There are few advantages of encapsulation in java as follows:
- Control Over Data: We can write the logic in the setter method to not store the negative values for an Integer. So by this way we can control the data.
- Data Hiding: The data members are private so other class canāt access the data members.
- Easy to test: Unit testing is easy for encapsulated classes
Example:
Java
// AĀ JavaĀ classĀ whichĀ isĀ aĀ fullyĀ encapsulatedĀ class. public Ā class Car { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // privateĀ variableĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā private Ā StringĀ name; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // getterĀ methodĀ forĀ nameĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā public Ā StringĀ getName() Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return Ā name; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // setterĀ methodĀ forĀ nameĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā public Ā void Ā setName(StringĀ name) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this .name = name;Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā // JavaĀ classĀ toĀ testĀ theĀ encapsulatedĀ class.Ā Ā public class Test { Ā Ā Ā Ā Ā Ā public Ā static Ā void Ā main(String[]Ā args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // creatingĀ instanceĀ ofĀ theĀ encapsulatedĀ classĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā CarĀ car Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new Ā Car(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // settingĀ valueĀ inĀ theĀ nameĀ memberĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā car.setName( "Honda" ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // gettingĀ valueĀ ofĀ theĀ nameĀ memberĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(car.getName()); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā } |
Pillar 3: Inheritance
Inheritance is the process of one class inheriting properties and methods from another class in Java. Inheritance is used when we have is-a relationship between objects. Ā Inheritance in Java is implemented usingĀ extendsĀ keyword.
Real-life Example:
The planet Earth and Mars inherits the super class Solar System and Solar system inherits the Milky Way Galaxy. So Milky Way Galaxy is the top super class for Class Solar System, Earth and Mars.
Let us do discuss the usage of inheritance in java applications with a generic example before proposing the code. So consider an example extending the ExceptionĀ class to create an application-specific Exception class that contains more information like error codes. For exampleĀ NullPointerException.
There are 5 different types of inheritance in java as follows:
- Single Inheritance: Class B inherits Class A using extends keyword
- Multilevel Inheritance: Class C inherits class B and B inherits class A using extends keyword
- Hierarchy Inheritance: Class B and C inherits class A in hierarchy order using extends keyword
- Multiple Inheritance: Class C inherits Class A and B. Here A and B both are superclass and C is only one child class. Java is not supporting Multiple Inheritance, but we can implement using Interfaces.
- Hybrid Inheritance: Class D inherits class B and class C. Class B and C inherits A. Here same again Class D inherits two superclass, so Java is not supporting Hybrid Inheritance as well.
Example:
Java
// super class class Car { Ā Ā Ā Ā // the Car class have one field Ā Ā Ā Ā public String wheelStatus; Ā Ā Ā Ā public int noOfWheels; Ā Ā Ā Ā Ā Ā // the Car class has one constructor Ā Ā Ā Ā public Car(String wheelStatus, int noOfWheels) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā this .wheelStatus = wheelStatus; Ā Ā Ā Ā Ā Ā Ā Ā this .noOfWheels = noOfWheels; Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā // the Car class has three methods Ā Ā Ā Ā public void applyBrake() Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā wheelStatus = "Stop" System.out.println( Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Stop the car using break" ); Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā // toString() method to print info of Car Ā Ā Ā Ā public String toString() Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return ( "No of wheels in car " + noOfWheels + "\n" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + "status of the wheels " + wheelStatus); Ā Ā Ā Ā } } Ā Ā // sub class class Honda extends Car { Ā Ā Ā Ā Ā Ā // the Honda subclass adds one more field Ā Ā Ā Ā public Boolean alloyWheel; Ā Ā Ā Ā Ā Ā // the Honda subclass has one constructor Ā Ā Ā Ā public Honda(String wheelStatus, int noOfWheels, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Boolean alloyWheel) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā // invoking super-class(Car) constructor Ā Ā Ā Ā Ā Ā Ā Ā super (wheelStatus, noOfWheels); Ā Ā Ā Ā Ā Ā Ā Ā alloyWheel = alloyWheel; Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā // the Honda subclass adds one more method Ā Ā Ā Ā public void setAlloyWheel(Boolean alloyWheel) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā alloyWheel = alloyWheel; Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā // overriding toString() method of Car to print more Ā Ā Ā Ā // info Ā Ā Ā Ā @Override public String toString() Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā return ( super .toString() + "\nCar alloy wheel " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + alloyWheel); Ā Ā Ā Ā } } Ā Ā // driver class public class Main { Ā Ā Ā Ā public static void main(String args[]) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Honda honda = new Honda( 3 , 100 , 25 ); Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(honda.toString()); Ā Ā Ā Ā } } |
Pillar 4: Polymorphism in javaĀ
Polymorphism is the ability to perform many things in many ways. The word Polymorphism is from two different Greek words- poly and morphs. āPolyā means many, and āMorphsā means forms. So polymorphism means many forms. The polymorphism can be present in the case of inheritance also. The functions behave differently based on the actual implementation.
Real-life Example:
A delivery person delivers items to the user. If itās a postman he will deliver the letters. If itās a food delivery boy he will deliver the foods to the user. Like this polymorphism implemented different ways for the delivery function.
There are two types of polymorphism as listed below:
- Static or Compile-time Polymorphism
- Dynamic or Run-time Polymorphism
Static or Compile-time Polymorphism when the compiler is able to determine the actual function, itās called compile-time polymorphism. Compile-time polymorphism can be achieved by method overloading in java. When different functions in a class have the same name but different signatures, itās calledĀ method overloading. A method signature contains the name and method arguments. So, overloaded methods have different arguments. The arguments might differ in the numbers or the type of arguments.
Example 1: Static Polymorphism
Java
public class Car{ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public void speed() { Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public void speed(String accelerator) { Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā public int speed(String accelerator, int speedUp) { Ā Ā Ā Ā Ā Ā Ā Ā return carSpeed; Ā Ā Ā Ā } } |
Dynamic or Run-time Polymorphism occurs when the compiler is not able to determine whether itās superclass method or sub-class method itās called run-time polymorphism. The run-time polymorphism is achieved by method overriding. When the superclass method is overridden in the subclass, itās called method overriding.
Example 2: Dynamic Polymorphism
Java
import java.util.Random; Ā Ā class DeliveryBoy { Ā Ā Ā Ā Ā Ā public void deliver() { Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "Delivering Item" ); Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā public static void main(String[] args) { Ā Ā Ā Ā Ā Ā Ā Ā DeliveryBoy deliveryBoy = getDeliveryBoy(); Ā Ā Ā Ā Ā Ā Ā Ā deliveryBoy.deliver(); Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā private static DeliveryBoy getDeliveryBoy() { Ā Ā Ā Ā Ā Ā Ā Ā Random random = new Random(); Ā Ā Ā Ā Ā Ā Ā Ā int number = random.nextInt( 5 ); Ā Ā Ā Ā Ā Ā Ā Ā return number % 2 == 0 ? new Postman() : new FoodDeliveryBoy(); Ā Ā Ā Ā } } Ā Ā class Postman extends DeliveryBoy { Ā Ā Ā Ā @Override Ā Ā Ā Ā public void deliver() { Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "Delivering Letters" ); Ā Ā Ā Ā } } Ā Ā class FoodDeliveryBoy extends DeliveryBoy { Ā Ā Ā Ā @Override Ā Ā Ā Ā public void deliver() { Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "Delivering Food" ); Ā Ā Ā Ā } } |
Delivering Letters