Abstraction in Java

Abstraction in Java (Abstract Class vs Interface) With Examples

Abstraction is one of the most important concepts in Java Object-Oriented Programming (OOP). It helps us hide implementation details and show only the necessary features of an object. In this post, you will learn abstraction with easy real-life examples and beginner-friendly Java programs.

✨ Quick Summary

Abstraction means hiding internal details and showing only the required functionality. Java provides abstraction using Abstract Classes and Interfaces.

📌 Topics Covered

  • What is Abstraction?
  • Why Abstraction is Important?
  • Abstract Class in Java
  • Abstract Method Example
  • Interface in Java
  • Abstract Class vs Interface
  • Final Conclusion

✅ What is Abstraction?

Abstraction means hiding the internal implementation details and showing only the functionality.

Real-life example: When you drive a bike, you use the accelerator, brake, and clutch. You don’t need to know how the engine works internally.

⭐ Why Abstraction is Important?

Abstraction helps in:

  • Reducing complexity
  • Improving security
  • Code reusability
  • Better maintainability
  • Hiding unwanted details

🔥 Abstract Class in Java

An abstract class is a class that can have abstract methods and normal methods. You cannot create an object of an abstract class.

✅ Abstract Method

An abstract method has no body, only declaration.


abstract void display();

  

✅ Example: Abstract Class Program


abstract class Animal {
    abstract void sound();

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

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

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

  

Output:
Dog barks: Woof Woof!
Animal is sleeping...

⚡ Interface in Java

An interface is used to achieve 100% abstraction in Java. It contains method declarations and is implemented using the implements keyword.

✅ Example: Interface Program


interface Vehicle {
    void start();
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car is starting...");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
    }
}

  

Output:
Car is starting...

📌 Abstract Class vs Interface

Abstract Class Interface
Can have abstract + normal methods Mostly only method declarations
Supports single inheritance Supports multiple inheritance
Uses extends Uses implements
Can have constructor Cannot have constructor

✅ Conclusion

Abstraction is a powerful OOP concept in Java. You can achieve abstraction using abstract classes and interfaces. Abstract class is useful when you want both abstract and normal methods, while interface is best when you want full abstraction and multiple inheritance.

🔥 Next Post Suggestion

Next, we will learn: Interface in Java (Deep Explanation + Real Programs)

Comments

Post a Comment

Popular posts from this blog

Inheritance in Java (Single, Multilevel, Hierarchical)

The Java Blueprint: Your First Step into Software Development

Java Variables and Data Types: Storing Your Data