Inheritance, Polymorphism, Encapsulation, and Abstraction

Object-Oriented Programming (OOP) is a programming paradigm that helps developers build organized and reusable code. The four core principles of OOP—Inheritance, Polymorphism, Encapsulation, and Abstraction—are essential for designing robust and maintainable software. Let’s break down each concept with simple explanations and examples.

1. Inheritance

Inheritance allows a class (child/subclass) to acquire properties and behaviors from another class (parent/superclass). It promotes code reusability and establishes a natural hierarchy between classes.

Example:

Editclass Animal {

    void sound() {

        System.out.println("Animal makes a sound");

    }

}

class Dog extends Animal {

    void sound() {

        System.out.println("Dog barks");

    }

}

Here, Dog inherits from Animal and overrides the sound() method to provide its own behavior.

2. Polymorphism

Polymorphism means "many forms." It allows one interface to be used for different underlying forms (data types). The most common types are compile-time (method overloading) and run-time (method overriding) polymorphism.

Example:

Animal a;

a = new Dog();

a.sound();  // Outputs: Dog barks

Even though the reference is of type Animal, the sound() method of Dog is called at runtime.

3. Encapsulation

Encapsulation is the process of wrapping data (variables) and methods into a single unit (class) and restricting direct access to some of the object’s components. This is typically done using private variables and public getter/setter methods.

Example:

class Person {

    private String name;

    public void setName(String newName) {

        name = newName;

    }

    public String getName() {

        return name;

    }

}

Encapsulation protects the internal state of the object from unauthorized access.

4. Abstraction

Abstraction means hiding the complex implementation details and showing only the essential features of the object. It can be achieved using abstract classes or interfaces.

Example:

abstract class Vehicle {

    abstract void move();

}

class Car extends Vehicle {

    void move() {

        System.out.println("Car moves on roads");

    }

}

Here, Vehicle defines a general blueprint, and Car provides the specific implementation.

Conclusion

Understanding these four pillars of OOP—Inheritance, Polymorphism, Encapsulation, and Abstraction—can greatly enhance your ability to design clean, efficient, and scalable applications. They form the foundation of modern programming languages like Java, C++, Python, and more.

Learn  Full Stack Java Training

Setting Up Java Development Environment (JDK, IntelliJ, Eclipse)

Java Basics: Variables, Data Types, and Operators

Control Flow Statements in Java (if, switch, loops

Understanding Object-Oriented Programming in Java

Visit Our Quality Thought Training Institute 









Comments

Popular posts from this blog

Understanding the useEffect Hook

What Is Tosca? A Beginner’s Guide

Exception Handling in Java