Lesson 41 of 50 – Java Abstraction
82%

Java Abstraction

Abstraction is one of the four main principles of Object-Oriented Programming (OOP). It means hiding implementation details and showing only the essential features of an object.

1. What Is Abstraction?

Abstraction allows users to focus on what an object does instead of how it performs its work.

For example, when you use an ATM, you select an operation such as withdrawing money. You do not need to understand every internal banking process.

Java supports abstraction mainly through:

  • Abstract classes
  • Interfaces
Real-life example: You drive a car using the steering wheel, accelerator, and brakes without needing to understand the entire engine mechanism.
2. What Is an Abstract Class?

An abstract class is declared using the abstract keyword. It can contain abstract methods, concrete methods, fields, and constructors.

abstract class Animal {
    abstract void sound();

    void sleep() {
        System.out.println("Animal is sleeping");
    }
}

Here, Animal is an abstract class. The sound() method has no body, while sleep() has a body.

You cannot create an object directly from an abstract class using new Animal().
3. What Is an Abstract Method?

An abstract method is declared without a method body. A concrete subclass must implement inherited abstract methods unless that subclass is also abstract.

abstract class Shape {
    abstract void draw();
}

The draw() method has no implementation in the Shape class. Subclasses can provide their own implementations.

4. Implementing an Abstract Method
abstract class Animal {
    abstract void sound();
}

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

class Main {
    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.sound();
    }
}
Output:
Dog barks

The Dog class provides the implementation of the abstract sound() method.

5. Abstract Class with Multiple Methods
abstract class Vehicle {
    abstract void start();

    abstract void stop();

    void fuel() {
        System.out.println("Vehicle needs energy");
    }
}

class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car starts");
    }

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

class Main {
    public static void main(String[] args) {
        Car c1 = new Car();

        c1.start();
        c1.fuel();
        c1.stop();
    }
}
Output:
Car starts
Vehicle needs energy
Car stops

The abstract class defines required operations and also provides a common concrete method.

6. Creating an Abstract Class Reference

Although an abstract class cannot be instantiated, its reference can refer to an object of a concrete subclass.

abstract class Animal {
    abstract void sound();
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Cat meows");
    }
}

class Main {
    public static void main(String[] args) {
        Animal a1 = new Cat();

        a1.sound();
    }
}
Output:
Cat meows

The reference type is Animal, but the actual object is Cat. Java calls the overridden method implemented by Cat.

7. What Is an Interface?

An interface defines a contract that implementing classes agree to follow. It is declared using the interface keyword.

interface Animal {
    void sound();
}

class Dog implements Animal {
    @Override
    public void sound() {
        System.out.println("Dog barks");
    }
}

class Main {
    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.sound();
    }
}
Output:
Dog barks

The Dog class implements the Animal interface and provides a public implementation of sound().

8. Multiple Interfaces

A Java class can implement more than one interface. This allows a class to follow multiple contracts.

interface Printable {
    void print();
}

interface Scannable {
    void scan();
}

class Machine implements Printable, Scannable {
    @Override
    public void print() {
        System.out.println("Printing document");
    }

    @Override
    public void scan() {
        System.out.println("Scanning document");
    }
}

class Main {
    public static void main(String[] args) {
        Machine m1 = new Machine();

        m1.print();
        m1.scan();
    }
}
Output:
Printing document
Scanning document

The Machine class provides implementations for both interfaces.

9. Abstract Class vs Interface
Abstract Class Interface
Declared using abstract class. Declared using interface.
Can have constructors. Cannot have constructors.
Can have instance fields and concrete methods. Fields are implicitly public, static, and final.
A class can extend only one class. A class can implement multiple interfaces.
Useful for sharing state and common implementation. Useful for defining a contract across different classes.
Modern Java interfaces can also contain default, static, and private methods. Abstract classes and interfaces are not limited to only one kind of method.
10. Real-Life Example: Payment System
abstract class Payment {
    abstract void pay(double amount);

    void showMessage() {
        System.out.println("Processing payment");
    }
}

class UpiPayment extends Payment {
    @Override
    void pay(double amount) {
        System.out.println("Paid using UPI: " + amount);
    }
}

class CardPayment extends Payment {
    @Override
    void pay(double amount) {
        System.out.println("Paid using card: " + amount);
    }
}

class Main {
    public static void main(String[] args) {
        Payment p1 = new UpiPayment();
        Payment p2 = new CardPayment();

        p1.showMessage();
        p1.pay(500);

        p2.showMessage();
        p2.pay(1000);
    }
}
Output:
Processing payment
Paid using UPI: 500.0
Processing payment
Paid using card: 1000.0

The Payment class defines the common structure, while each subclass provides its own payment behavior.

11. Advantages of Abstraction
  • Hides unnecessary implementation details.
  • Provides a clear contract for subclasses or implementations.
  • Encourages reusable and maintainable code.
  • Allows different implementations behind a common type.
  • Helps separate what an object does from how it does it.
Limitations
  • Too many abstractions can make a small program harder to understand.
  • Abstract designs require careful planning.
  • Abstract classes cannot be instantiated directly.
12. Abstract Class with Constructor

An abstract class can have a constructor. The constructor runs when a concrete subclass object is created.

abstract class Person {
    String name;

    Person(String name) {
        this.name = name;
    }

    abstract void display();
}

class Student extends Person {
    Student(String name) {
        super(name);
    }

    @Override
    void display() {
        System.out.println("Student name: " + name);
    }
}

class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Rahul");
        s1.display();
    }
}
Output:
Student name: Rahul

The Student constructor calls the Person constructor using super(name), initializing the inherited name field.

13. Practice Assignments
  1. Create an abstract Animal class with an abstract sound() method. Implement it in Dog and Cat.
  2. Create an abstract Shape class with an area() method. Implement it in Rectangle and Circle.
  3. Create a Vehicle abstract class with start() and stop() methods. Implement it in Car and Bike.
  4. Create a Payment interface and implement it in UpiPayment and CardPayment.
  5. Create Printable and Scannable interfaces and implement both in a Machine class.
  6. Create an abstract Person class with a constructor and a display() method. Extend it using Student.
  7. Explain the difference between an abstract class and an interface with examples.
Practice Tip: Write each program, compile it, and test the output before moving to the next lesson.

🧠 Quick Quiz

1. Which keyword is used to declare an abstract class?




2. Which keyword is used when a class implements an interface?




3. Can an abstract class have a constructor?