๐Ÿš€ Java Abstraction - Complete Guide

Table of Contents

๐Ÿง  What is Abstraction in Java?

Abstraction is the process of hiding internal details and showing only essential features to the user.

It focuses on "what an object does" instead of "how it does it."

๐ŸŽฏ Real-life Example:

๐Ÿ”Œ TV Remote: When you press a TV remote button, you don't know how it works inside (circuit, IR signal) โ€” you just press it and it performs the action.

๐Ÿง  That's abstraction: Hide the complex working, expose only simple interface.

๐Ÿงฉ Why Use Abstraction?

  • Reduces complexity
  • Improves security (hides internal logic)
  • Allows flexibility and extensibility
  • Encourages coding to interfaces, not implementations

โœ… How to Achieve Abstraction in Java?

Java provides two ways to achieve abstraction:

Tool Abstraction Level Can Have Method Body? Can Have Constructors? Can Have Variables?
abstract class Partial Yes (abstract + normal methods) Yes Yes
interface Full (100%) Yes (Java 8+ allows default/static methods) No Only static + final

1๏ธโƒฃ Abstract Class in Java

๐Ÿ”น Definition:

A class declared with the abstract keyword.

It cannot be instantiated, but can have both abstract and concrete (normal) methods.

โœ… Code Example:

abstract class Animal {
    abstract void sound();  // abstract method (no body)

    void sleep() {
        System.out.println("Sleeping...");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();  // Upcasting
        a.sound();             // Dog barks
        a.sleep();             // Sleeping...
    }
}
Dog barks
Sleeping...

๐Ÿ’ก Key Points:

  • You can't create object of abstract class: Animal a = new Animal(); โŒ
    ๐Ÿ‘‰ Reason: Abstract classes are incomplete โ€” they may have abstract methods without a body. So, they are meant to be inherited and implemented, not instantiated directly.
  • You must override all abstract methods in child class (if 2 abstract methods exist then override both 2 in child class)
  • Can have constructors, variables, and normal methods

2๏ธโƒฃ Interface in Java

๐Ÿ”น Definition:

An interface is a fully abstract blueprint of a class that contains only method declarations (until Java 7).

From Java 8 onward, interfaces can have some implemented methods too (default, static, private).

๐Ÿ’ก Why use Interface when Abstract Class exists?

  • Java doesn't support multiple class inheritance โ€“ but a class can implement multiple interfaces.
  • Interface defines a contract: any class implementing it must provide behavior.
  • Use interfaces when you only want to define method names (not implementation).

โœ… Java 8+ Enhancements: Interface can also have

  • default methods โ€“ allow method body (for backward compatibility)
  • static methods โ€“ useful for utility/helper methods
  • private methods (Java 9+) โ€“ for internal reuse inside interface

๐Ÿง  Why variables in interface are always public static final?

  • Interfaces can't have object state โ€“ so variables must be constants.
  • Every variable is implicitly public static final โ€“ like a global constant.

โœ… Code Example:

interface Vehicle {
    void start();  // public abstract by default

    default void honk() {
        System.out.println("Beep Beep!");
    }

    static int getWheels() {
        return 4;
    }

    private void helper() {
        System.out.println("Internal helper method");
    }

    int MAX_SPEED = 120; // public static final by default
}

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

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();  // Output: Car starts with key
        v.honk();   // Output: Beep Beep!

        System.out.println("Max Speed: " + Vehicle.MAX_SPEED); // Output: 120
        System.out.println("Wheels: " + Vehicle.getWheels());  // Output: 4
    }
}
Car starts with key
Beep Beep!
Max Speed: 120
Wheels: 4

๐Ÿ’ก Interface Summary:

  • All methods are public abstract by default (unless marked default/static)
  • Supports multiple inheritance (a class can implement multiple interfaces)
  • No constructors or instance variables
  • Variables are always public static final
  • Java 8+ adds power with default, static, and private methods

๐ŸŽฏ When to Use Abstract Class vs Interface

Use Case Go For...
You want partial abstraction Abstract Class
You need to define common base with some logic Abstract Class
You want 100% abstraction (only method declarations) Interface
You need multiple inheritance Interface
You're building a plug-and-play system Interface

๐Ÿ” Interface + Abstract Class Together

You can use both together:

interface Printable {
    void print();
}

abstract class Machine {
    abstract void start();
}

class Printer extends Machine implements Printable {
    public void print() {
        System.out.println("Printing...");
    }

    void start() {
        System.out.println("Machine started.");
    }
}

๐Ÿงช Interview Questions with One-Liner Answers

Question Answer
What is abstraction? Hiding internal details and showing only the required features.
How to achieve abstraction in Java? Using abstract classes and interfaces.
Can we instantiate an abstract class? โŒ No
Can abstract class have a constructor? โœ… Yes
Can interface have method body? โœ… Yes, from Java 8 (default/static methods)
Difference between abstract class and interface? Abstract class can have body, constructor, variables. Interface = full abstraction, no constructor.
Can we implement multiple interfaces? โœ… Yes
Can abstract class implement interface? โœ… Yes, but must override methods or remain abstract

โœ… Summary Table

Feature Abstract Class Interface
Keyword abstract class interface
Instantiation โŒ No โŒ No
Constructors โœ… Yes โŒ No
Method Body โœ… Yes (concrete methods) โœ… From Java 8 (default/static)
Variables โœ… Any type โœ… Only public static final
Inheritance Type Single Multiple