Abstraction is like a magic trick in programming: it hides the complicated "how" behind the scenes and only shows you the simple "what" you need to know. It's 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." This makes code easier to use and understand, just like using a smartphone without knowing its internal circuits.
๐ TV Remote: When you press a TV remote button, you don't need to know the complex electronics inside (like circuits or IR signals) โ you just press it, and the TV changes channel. The remote hides the complexity and provides simple buttons for you to use.
๐ง That's abstraction: Hide the complex working, expose only simple interface.
It shows how abstraction simplifies life. In code, you call a method without worrying about its implementation, making your programs cleaner and more maintainable.
Think of abstraction as a "black box" โ you know what goes in and what comes out, but not what's inside. This is great for team work where different people handle different parts.
Java provides two main ways to achieve abstraction, each with its own strengths:
Tool | Abstraction Level | Can Have Method Body? | Can Have Constructors? | Can Have Variables? |
---|---|---|---|---|
abstract class |
Partial (0-100%) | Yes (abstract + normal methods) | Yes | Yes, any type |
interface |
Full (100% by default) | Yes (Java 8+ allows default/static methods) | No | Only static + final constants |
Choose based on whether you need shared code (abstract class) or just a contract (interface).
A class declared with the abstract
keyword. It's like a half-finished blueprint.
It cannot be instantiated (no direct objects), but can have both abstract (no body) and concrete (with body) methods. Subclasses must fill in the abstract parts.
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: Woof!");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows: Meow!");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog(); // Upcasting
dog.sound(); // Dog barks: Woof!
dog.sleep(); // Sleeping...
Animal cat = new Cat();
cat.sound(); // Cat meows: Meow!
cat.sleep(); // Sleeping...
}
}
Animal a = new Animal(); โ
Think of an abstract class as a template for a car. It defines that every car must have "drive()" but leaves how it drives (fast or slow) to specific models like SportsCar or FamilyCar.
An interface is a fully abstract blueprint (contract) that defines what methods must be implemented, but not how. Until Java 7, only declarations; from Java 8, some implementations allowed.
It's like a promise: "If you implement me, you must provide these behaviors."
default
methods โ provide a default implementation (for backward compatibility)static
methods โ useful for utility/helper methods shared across implementationsprivate
methods (Java 9+) โ for internal reuse inside the interface without exposing thempublic static final
โ like global constants everyone agrees on.๐ Power Socket: A power socket (interface) defines the shape of the plug and voltage. Any device (class) that implements it can plug in and work, but how the device uses the power is up to it (e.g., lamp lights up, fan spins).
This allows different devices to connect without knowing each other's internals.
interface Vehicle {
void start(); // public abstract by default
default void honk() {
System.out.println("Beep Beep!");
}
static int getWheels() {
return 4;
}
int MAX_SPEED = 120; // public static final by default
}
class Car implements Vehicle {
public void start() {
System.out.println("Car starts with key");
}
}
class Bike implements Vehicle {
public void start() {
System.out.println("Bike starts with kick");
}
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
car.start(); // Car starts with key
car.honk(); // Beep Beep!
Vehicle bike = new Bike();
bike.start(); // Bike starts with kick
bike.honk(); // Beep Beep!
System.out.println("Max Speed: " + Vehicle.MAX_SPEED); // 120
System.out.println("Wheels: " + Vehicle.getWheels()); // 4
}
}
public abstract
by default (unless marked default/static)public static final
default
, static
, and private
methodsA class can implement many interfaces, like class Smartphone implements Callable, Chargeable, Playable
. This is how Java handles multiple "behaviors" without multiple inheritance issues.
Use Case | Go For... | Why? |
---|---|---|
You want partial abstraction with some shared code | Abstract Class | Allows concrete methods and state |
You need to define common base for related classes | Abstract Class | Shares implementation details |
You want 100% abstraction (only method declarations) | Interface | Pure contract, no implementation |
You need multiple inheritance of behaviors | Interface | Classes can implement many |
You're building a plug-and-play system | Interface | Easy to swap implementations |
Use interfaces for "can-do" relationships (e.g., Flyable) and abstract classes for "is-a" with shared code (e.g., AbstractBird).
You can combine them for powerful designs: Interface for the contract, Abstract Class for partial implementation.
interface Printable {
void print();
}
abstract class Machine {
abstract void start();
}
class Printer extends Machine implements Printable {
public void print() {
System.out.println("Printing document...");
}
void start() {
System.out.println("Printer warming up.");
}
}
public class Main {
public static void main(String[] args) {
Printer p = new Printer();
p.start(); // Printer warming up.
p.print(); // Printing document...
}
}
This way, you get the best of both: flexibility from interfaces and shared code from abstract classes.
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, it's incomplete. |
Can abstract class have a constructor? | โ Yes, for initializing shared state. |
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, constants only. |
Can we implement multiple interfaces? | โ Yes, for multiple behaviors. |
Can abstract class implement interface? | โ Yes, but must override methods or remain abstract. |
Why use default methods in interfaces? | To add new methods without breaking existing implementations. |
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 (extends) | Multiple (implements) |
1. What keyword is used for an abstract class?
2. Can an interface have constructors?
3. Which allows multiple inheritance?