Abstraction in Java


Abstraction is one of the core principles of Object-Oriented Programming (OOP). It allows you to hide the complex implementation details of a system and expose only the essential features. This helps in reducing complexity and increasing efficiency.

In Java, abstraction is achieved through:

  1. Abstract Classes
  2. Interfaces

Abstract Classes

An abstract class is a class that cannot be instantiated on its own and may contain both abstract methods (without implementation) and concrete methods (with implementation).

  • Syntax:
            
            abstract class Vehicle {
                abstract void start(); // Abstract method
    
                void stop() {          // Concrete method
                    System.out.println("Vehicle is stopping...");
                }
            }
            
            
  • Key Points:
    • Can have both abstract and concrete methods.
    • Can have constructors, fields, and methods with code.
    • Used when you want to share code among related classes.
  • Example:
                
                class Car extends Vehicle {
                    void start() {
                        System.out.println("Car is starting...");
                    }
                }
    
                public class Main {
                    public static void main(String[] args) {
                        Car car = new Car();
                        car.start(); // Car is starting...
                        car.stop();  // Vehicle is stopping...
                    }
                }
                
                

Abstract Methods

An abstract method is a method without a body. It’s meant to be implemented by subclasses.

  • Syntax:
                
                abstract class Shape {
                    abstract void draw(); // Abstract method
                }
                
                
  • Key Points:
    • Declared using the abstract keyword.
    • Must be implemented by any concrete subclass.
  • Example:
                
                class Circle extends Shape {
                    void draw() {
                        System.out.println("Drawing a Circle...");
                    }
                }
    
                public class Main {
                    public static void main(String[] args) {
                        Shape shape = new Circle();
                        shape.draw(); // Drawing a Circle...
                    }
                }
                
                

Interfaces

An interface is a contract that defines a set of methods that a class must implement. All methods in an interface are implicitly abstract (before Java 8) and public.

  • Syntax:
                
                interface Drivable {
                    void drive(); // Abstract method
                }
                
                
  • Key Points:
    • Cannot have constructors or instance variables (only constants).
    • A class can implement multiple interfaces (supports multiple inheritance).
    • From Java 8 onwards, interfaces can have default and static methods.
  • Example:
                
                interface Flyable {
                    void fly();
                }
    
                class Bird implements Flyable {
                    public void fly() {
                        System.out.println("Bird is flying...");
                    }
                }
    
                public class Main {
                    public static void main(String[] args) {
                        Bird bird = new Bird();
                        bird.fly(); // Bird is flying...
                    }
                }
                
                
Differences Between Abstract Classes and Interfaces
Aspect Abstract Class Interface
Instantiation Cannot be instantiated directly Cannot be instantiated directly
Methods Can have both abstract and concrete methods Only abstract methods (before Java 8)
Constructors Yes No
Multiple Inheritance No (single inheritance) Yes (multiple inheritance)
Use Case When classes share code When classes share behavior/contracts