Understanding OOPS - Four Pillars of OOPS

 Introduction to OOPS

There are different programming methods. OOPS is one of them. OOPS stands for Object Oriented Programming Languages. In this method of programming each program is written in classes and objects. In our real life there are objects and classes. A class has different properties. And an object of a class gives the class existence in that programming environment.

What is Class?

Class is a user defined prototype ot blueprint that has no existence until the object of that class is created. 

What is Object?

Object is instances of a class. A class is instanced when object is created. And also memory is allocated when object is created.

Code Example:

class A
{
    String name;
    int age;
    
    A(String name, int age)
    {
        this.name=name;
        this.age=age;
    }
    
    void getData(){
        System.out.println("Name:"+ name + "\nAge:"+ age);
    }
}

class Main
{
    public static void main(String args[]){
        A obj = new A("Amit", 21);
        
        obj.getData();
    }
}

Output:

Name:Amit
Age:21

What are the four pillars of OOPS?

Four pillars of OOPS are:

1. Inheritance:

In inheritance there is parent - child relationship. The properties of a class is accessed by another class in inheritance. "extends" keyword is used in order to archive inheritance.

Example: There are two classes A and B. 
Now if B extends A, then the properties of A is accessed by B.

Code Example:

class A
{
    String name;
    int age;
    
    A(String name, int age)
    {
        this.name=name;
        this.age=age;
    }
    
    void getData(){
        System.out.println("Name:"+ name + "\nAge:"+ age);
    }
}
class B extends A
{
    String location;
    B(String name, int age, String location){
        super(name, age);
        this.location = location;
    }
}
class Main
{
    public static void main(String args[]){
        B obj = new B("Amit", 21, "Kolkata");
        
        System.out.println("Name:"+ obj.name + "\nAge:"+ obj.age + "\nLocation:"+ obj.location);
    }
}

Output:

Name:Amit
Age:21
Location:Kolkata

2. Encapsulation: 

Encapsulation is binding data and methods in a single unit or class in order to prevent any external interference. Encapsulation in OOPS is archived by declaring instanc variables as private and defining public setter and getter methods for controlled access to the variables. The variables are restricted from end user access directly.  The getter method is used to retrieve data values and the setter is used to modify the data values.

Code Example:

class A
{
    private String name;
    
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
}

public class Main
{
    public static void main(String args[]){
        A obj = new A();
        
        obj.setName("Amit");
        System.out.println("Name:"+ obj.getName());
    }
}

Output:

Name:Amit

3. Polymorphism:

Poly means may forms. In OOPS a method can be used in forms. There are two types of polymorphism: 

a. Static Polymorphism: In static polymorphism, which method is to be called is decided during compilation time. Static Polymorphism is archived by method overloading.

b. Dynamic Polymorphism: In dynamic polymorphism, which method is to be called is decided during run time. Dynamic polymorphism is archived by method overriding and method overloading with reference.

Code Example:

class Food
{
    void taste(){
        System.out.println("This is a Food category....");
    }
}

class Mango extends Food
{
    @Override
    void taste(){
        System.out.println("Mango tastes sweet....");
    }
}

class Main
{
    public static void main(String args[]){
        Food t = new Mango(); // creating reference for runtime polymorphism
        t.taste();
    }
}

Output:

Mango tastes sweet....

4. Abstraction:

Abstraction means hiding internal details and showing only necessary information.
In OOPS abstraction is archived by two ways:

a. Interface 
b. Abstract class

Where interface is complete implementation of abstraction and abstract class is not complete implementation of abstraction.

Interface: 

a. In interface all variables are final by default.

b. No methods should be defined inside an interface otherwise it became an abstract class. 

c. We have to implements an interface in order to define it's methods and also we have make this defined method public inside the class which implements the interface.

Code Example:

interface food
{
    // public, static and final
    int id=101;
    
    // public and abstract
    void taste();
    
}

class TestFood implements food
{
   
    public void taste(){
        System.out.println("Testing food....");
    }
}

class Main
{
    public static void main(String args[]){
        TestFood t = new TestFood();
        t.taste();
        System.out.println(t.id);
    }
}

Output:

Testing food....
101

Abstract class: 

a. In abstract class abstract keyword is used before the class name like: abstract class Demo.

b. We can have an abstract class without any abstract method.

c. We cannot create object of an abstract class, but we can create reference of an abstract class.

For more details explanation, please comment on this post.

d. If a class have at least one abstract method then it is mandatory to make that class abstract.


Code Example:


abstract class Food
{
    abstract void Info();
}

class Mango extends Food
{
   String name="Amit";
   int age = 20;
   
    void Info(){
        System.out.println("Name:"+ name + "\nAge:" + age);
    }
}

class Main
{
    public static void main(String args[]){
        Food i = new Mango();
        
        i.Info();
    }
}

Output:

Name:Amit
Age:20
Post a Comment (0)
Previous Post Next Post