Abstraction helps us hide complex details and show only what is necessary. It makes programs easier to understand and use.
Why abstraction is required in Java
abstract class Vehicle { abstract void start(); void stop() { System.out.println("Vehicle stopped."); } }
An abstract class can have both abstract methods (without body) and concrete methods (with body).
Abstract methods must be implemented by subclasses.
abstract class Animal { abstract void sound(); } class Dog extends Animal { void sound() { System.out.println("Bark"); } }
abstract class Shape { abstract double area(); } class Circle extends Shape { double radius; Circle(double radius) { this.radius = radius; } double area() { return 3.14 * radius * radius; } }
This program shows abstraction by hiding the start details inside Car class. Main uses Vehicle type to call start and stop.
abstract class Vehicle { abstract void start(); void stop() { System.out.println("Vehicle stopped."); } } class Car extends Vehicle { void start() { System.out.println("Car started."); } } public class Main { public static void main(String[] args) { Vehicle myCar = new Car(); myCar.start(); myCar.stop(); } }
Time complexity depends on the implementation of abstract methods, not abstraction itself.
Abstraction reduces code complexity and increases reusability.
Common mistake: Trying to create an object of an abstract class directly, which is not allowed.
Use abstraction when you want to define a template for other classes without specifying exact details.
Abstraction hides complex details and shows only what is needed.
It helps protect data and makes code easier to maintain.
Abstract classes and methods are tools to achieve abstraction in Java.