JAVA - Zero to Hero / Object Oriented Programming (OOP)

Abstraction in JAVA

❔ What is Abstraction?

Abstraction is the process of hiding implementation details and showing only essential features.

In Java, it’s achieved using:

  • abstract classes

  • interfaces

🎯 Focus on what an object does, not how it does it.

💡 Real-Life Analogy

When you drive a car, you just:

  • Press the accelerator

  • Use the brake

You don’t know the internal engine mechanics → That’s abstraction.


🎭 Why Use Abstraction?

  • To simplify complex systems

  • To hide unnecessary details

  • To define common behavior across multiple classes

  • To achieve loose coupling


🧩 What is an Abstract Class?

  • An abstract class is a class that cannot be instantiated (you cannot create its object directly).

  • It is declared using the abstract keyword.

  • It may contain both:

    • Abstract methods (methods without a body)

    • Concrete methods (methods with full code/logic)

🛠️ Why is an Abstract Class Required?

  • It provides a common base for other classes to extend.

  • It helps you define a template or blueprint for related classes.

  • You can write common code once and reuse it across multiple subclasses.

  • It allows you to enforce certain methods that must be implemented by all child classes.

  • It supports polymorphism, letting you work with different objects using a common abstract class type.


📌 Important Points to Remember

  • You cannot create an object of an abstract class directly.

  • If a class has even one abstract method, the class must be declared abstract.

  • Subclasses must override all abstract methods, or they too must be declared abstract.

  • Abstract classes can have:

    • Variables

    • Constructors

    • Abstract and non-abstract methods


🧩 Using Abstract Class

🔸 Syntax:

abstract class ClassName {
    abstract void methodName();  // abstract method
    void regularMethod() {
        // normal method
    }
}

🔸 Example:

abstract class Animal {
    abstract void makeSound();  // abstract method

    void breathe() {
        System.out.println("Breathing...");
    }
}

class Dog extends Animal {
    void makeSound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.makeSound();
        a.breathe();
    }
}
Dog barks
Breathing...

🧱 Rules of Abstract Class

  • Cannot be instantiated (you can't do new Animal())

  • Can have both abstract and non-abstract methods

  • Must be extended by a subclass


🔌 What is an Interface?

  • An interface in Java is a blueprint of a class.

  • It contains only abstract methods (method declarations without bodies) — until Java 7.

  • From Java 8 onward, interfaces can also have:

    • default methods (with body)

    • static methods

  • From Java 9, private methods are also allowed inside interfaces.


🧠 Why is an Interface Used?

  • To achieve 100% abstraction (only method definitions, no implementation).

  • To define a contract: Any class that implements the interface must provide its own version of the methods.

  • To support multiple inheritance (Java doesn’t allow multiple class inheritance, but you can implement multiple interfaces).

  • To allow loose coupling in large applications (like in real-world APIs, frameworks, and architecture layers).


⚙️ Key Features of Interface:

  • All methods are implicitly public and abstract (unless marked otherwise).

  • All variables are public, static, and final by default.

  • A class uses the implements keyword to use an interface.

  • A class can implement multiple interfaces.


🧪 Example:

interface Vehicle {
    void start();  // abstract method
    void stop();
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car starts");
    }

    public void stop() {
        System.out.println("Car stops");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle myCar = new Car();  // Polymorphism
        myCar.start();  // Output: Car starts
        myCar.stop();   // Output: Car stops
    }
}
Car starts
Car stops

🔁 Difference Between Abstract Class and Interface (in short):

  • An abstract class can have both abstract and concrete methods; an interface mostly has abstract methods.

  • A class can extend only one abstract class, but it can implement multiple interfaces.

  • Interfaces are used when different classes need to follow the same set of rules or behavior.


🚪 Abstraction Using Interfaces

An interface is like a 100% abstract class — all methods are abstract (until Java 7).

interface Vehicle {
    void start();
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car is starting");
    }
}
Car is starting

🆚 Abstract Class vs Interface

Feature

Abstract Class

Interface

Methods

Can have both abstract & concrete

Only abstract (Java 7 & earlier)

Constructors

Allowed

Not allowed

Variables

Can have instance variables

Only static & final (constants)

Inheritance

extends

implements

Multiple Inheritance

Not supported

Supporteds