BCA / B.Tech 9 min read

Abstraction in Java

Abstraction in Java:


What is Abstraction?

Abstraction means "to hide". That is, to show only the essential information (important details) of something and to hide the rest.

Real-life example: Suppose you are driving a car. You just turn the steering wheel, apply the brakes, and press the accelerator. But you do not see the internal workings like how the engine is functioning or how the gears are changing.

In the same way, abstraction also exists in programming.

Meaning of Abstraction in Java:

  • Only necessary things are visible to the user.
  • The underlying code is hidden.
  • It helps in security and code simplification.

2. How to achieve Abstraction?

Abstraction in Java is achieved in two ways:

 1. Abstract Class
 2. Interface

1. What is an Abstract Class?

An Abstract Class is a class that is incomplete. It has some methods that only have a name (no body).

Key Points:

  • It is created with the `abstract` keyword.
  • It can have both abstract and normal methods.
  • It cannot be used directly; it must be extended.

Example: Abstract Class

// Creating an abstract class
abstract class Vehicle {
    abstract void start(); // Abstract method (no body)
    
    void stop() { // Normal method
        System.out.println("Vehicle has stopped.");
    }
}

// Inheriting the abstract class
class Car extends Vehicle {
    void start() { // It is necessary to define the abstract method
        System.out.println("Car has started.");
    }
}

public class Main {
    public static void main(String args[]) {
        Vehicle myCar = new Car(); // Creating an object
        myCar.start(); // Output: Car has started.
        myCar.stop();  // Output: Vehicle has stopped.
    }
}

  • Vehicle is an abstract class.
  • The start() method is abstract (it has no body).
  • The Car class extended it and defined the start() method.

What is an Interface?

An Interface is completely abstract. It only contains method declarations (names), not their bodies.

Key Points:

  • It is created with the `interface` keyword.
  • All methods are abstract by default.
  • It is used by implementing it.
  • A class can implement more than one interface.

Example: Interface

// Creating an interface
interface Animal {
    void sound(); // Method only has a name (no body)
}

// Implementing the interface
class Dog implements Animal {
    public void sound() { // It is necessary to define the method
        System.out.println("The dog is barking.");
    }
}

public class Main {
    public static void main(String args[]) {
        Animal myDog = new Dog();
        myDog.sound(); // Output: The dog is barking.
    }
}

  • Animal is an interface.
  • The sound() method is abstract (no body).
  • The Dog class implemented it and defined the sound() method.

Why is Abstraction Important?

  • Security: The user sees only the necessary things; other details are hidden.
  • Simple Code: The code becomes shorter and easier to understand.
  • Code Reusability: Code created once can be used repeatedly.
  • Loose Coupling: Different parts can be used without dependency.