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.

package OopsConcepts;
public class OopsConcepts {
    
    //Inheritance
class Employee{
    String employeeName;

        public String getEmployeeName() {
            return employeeName;
        }

        public void setEmployeeName(String employeeName) {
            this.employeeName = employeeName;
        }
    }

class ContractEmployee extends Employee{
    int salary;

        public int getSalary() {
            return salary;
        }

        public void setSalary(int salary) {
            this.salary = salary;
        }
    }

public void Display(){
Employee emp = new Employee();
ContractEmployee contractEmployee = new ContractEmployee();
System.out.println("---------Employee Display----------");
emp.setEmployeeName("Alpha");
System.out.println("Employee Name: "+emp.getEmployeeName());
System.out.println("---------Contract Employee Display----------");
contractEmployee.setEmployeeName("Beta");
contractEmployee.setSalary(45000);
System.out.println("Contract Employee Name: "+contractEmployee.getEmployeeName());
System.out.println("Contract Employee Name: "+contractEmployee.getSalary());
}
public static void main(String args[]){
    OopsConcepts inheritance = new OopsConcepts();
    inheritance.Display();   
}    
}

Output: Inheritance

inheritance
Polymorphism:
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.

public class OopsConceptsPolymorphism {
    
class Employee{
    public void Display(){
        System.out.println("Inside Employee...");
    }
    
    public void Calculate(int a){
        System.out.println("Employee Calculate: "+(a+5));
    }
}

class ContractEmployee extends Employee{
     public void Display(){
        System.out.println("Inside Contract Employee...");
    }
     
     public void Calculate(int a, int b){
         System.out.println("Contract Employee Calculate: "+(a+b));
     }
}

public void Display(){
Employee emp = new Employee();
ContractEmployee contractEmployee = new ContractEmployee();
System.out.println("----------------------- Method Overriding----------------------");
System.out.println("Employee Class Running . . . ");
emp.Display();
System.out.println("Contract Employee Running . . . ");
contractEmployee.Display();

System.out.println("----------------------- Method Overloading----------------------");
System.out.println("Employee Class Running . . . ");
emp.Calculate(20);
System.out.println("Contract Employee Running . . . ");
contractEmployee.Calculate(20,30);
}

public static void main(String args[]){
   OopsConceptsPolymorphism polymorphism = new OopsConceptsPolymorphism();
   polymorphism.Display();
}
    
}

Output: Polymorphism

polymorphism

Abstraction:
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.

public abstract class AbstractionExample {
    public abstract void display();
    public void print(){
        System.out.println("Non-Abstract Method. . . . ");
    }
}

class ExtendingAbstract extends AbstractionExample{
    public void display() {
        System.out.println("Abstract Method extended. . ."); //To change body of generated methods, choose Tools | Templates.
    }

    public static void main(String args[]){
        ExtendingAbstract extendingAbstract = new ExtendingAbstract();
        extendingAbstract.display();
    }
}

Output: Abstraction

abstraction

Encapsulation:
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.

class Excapsulations{
    private String name = "Beta";
    private String department = "Java";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }
}

public class EncapsulationExample {
    public static void main(String args[]){
        Excapsulations encapsulation = new Excapsulations();
        encapsulation.setName("Alpha");
        encapsulation.setDepartment("Computer Science");
        System.out.println("Employee Name: "+encapsulation.getName());
        System.out.println("Employee Department: "+encapsulation.getDepartment());
    }
}

Output: Encapsulation

encapsulation

By Sri

2 thoughts on “OOPS Concepts – Inheritance, Polymorphism, Abstraction and Encapsulation”

Leave a Reply

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