Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 10, 2025

Abstraction, Encapsulation, Inheritance, Polymorphism

 

In Java, Abstraction, Encapsulation, Inheritance, and Polymorphism are four fundamental Object-Oriented Programming (OOP) concepts. Let’s go through them one by one:


Abstraction:

Definition: Abstraction is the process of hiding the complex implementation details and showing only the essential features of an object.

Purpose: To reduce complexity and increase code reusability.

How it's achieved:

Using abstract classes (abstract class) and interfaces (interface).

Example:

abstract class Shape { 

    abstract void draw();  // Abstract method (no implementation)

}

 

class Circle extends Shape { 

    void draw() { 

        System.out.println("Drawing Circle"); 

    } 

}

 

public class TestAbstraction { 

    public static void main(String[] args) { 

        Shape shape = new Circle(); 

        shape.draw();  // Output: Drawing Circle

    } 

}


Encapsulation:

Definition: Encapsulation is the wrapping of data (variables) and code (methods) into a single unit, usually a class. It also involves restricting direct access to data by using access modifiers.

Purpose: To protect data from unauthorized access and modification, and to provide control over how data is accessed or modified.

How it's achieved:

Using private access modifiers and providing public getter and setter methods.

Example:

class Student { 

    private String name;  // Private data member

 

    // Getter method

    public String getName() { 

        return name; 

    } 

 

    // Setter method

    public void setName(String name) { 

        this.name = name; 

    } 

}

 

public class TestEncapsulation { 

    public static void main(String[] args) { 

        Student student = new Student(); 

        student.setName("Krishna"); 

        System.out.println(student.getName());  // Output: Krishna

    } 

}


Inheritance:

Definition: Inheritance is a mechanism where one class acquires the properties (fields) and behaviors (methods) of another class.

Purpose: To promote code reuse and establish a parent-child relationship.

How it's achieved:

Using the extends keyword.

Types of Inheritance in Java: Single, Multilevel, Hierarchical. (Java does not support Multiple Inheritance directly but can be achieved using interfaces).

Example:

class Animal { 

    void eat() { 

        System.out.println("Eating..."); 

    } 

}

 

class Dog extends Animal { 

    void bark() { 

        System.out.println("Barking..."); 

    } 

}

 

public class TestInheritance { 

    public static void main(String[] args) { 

        Dog dog = new Dog(); 

        dog.eat();  // Output: Eating...

        dog.bark(); // Output: Barking...

    } 

}


Polymorphism:

Definition: Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. It enables one interface to be used for a general class of actions.

Purpose: To perform a single action in different ways.

Types of Polymorphism:

Compile-time Polymorphism (Method Overloading)

Runtime Polymorphism (Method Overriding)

Method Overloading Example (Compile-time):

class MathOperations { 

    int add(int a, int b) { 

        return a + b; 

    } 

 

    double add(double a, double b) { 

        return a + b; 

    } 

}

 

public class TestOverloading { 

    public static void main(String[] args) { 

        MathOperations obj = new MathOperations(); 

        System.out.println(obj.add(10, 20));       // Output: 30

        System.out.println(obj.add(10.5, 20.5));   // Output: 31.0

    } 

}

Method Overriding Example (Runtime):

class Vehicle { 

    void run() { 

        System.out.println("Vehicle is running"); 

    } 

}

 

class Bike extends Vehicle { 

    void run() { 

        System.out.println("Bike is running"); 

    } 

}

 

public class TestOverriding { 

    public static void main(String[] args) { 

        Vehicle obj = new Bike(); 

        obj.run();  // Output: Bike is running (due to overriding)

    } 

}


Key Differences Between These Concepts:

Concept

Purpose

Implementation Mechanisms

Abstraction

Hiding complexity, showing essential features

Abstract classes, Interfaces

Encapsulation

Data protection, controlled access

Private variables, Getter & Setter methods

Inheritance

Code reuse, creating hierarchical relationships

extends keyword, parent-child classes

Polymorphism

One interface, multiple implementations

Overloading (Compile-time), Overriding (Runtime)


 

No comments:

Post a Comment