Abstraction is 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."
๐ TV Remote: When you press a TV remote button, you don't know how it works inside (circuit, IR signal) โ you just press it and it performs the action.
๐ง That's abstraction: Hide the complex working, expose only simple interface.
Java provides two ways to achieve abstraction:
Tool | Abstraction Level | Can Have Method Body? | Can Have Constructors? | Can Have Variables? |
---|---|---|---|---|
abstract class |
Partial | Yes (abstract + normal methods) | Yes | Yes |
interface |
Full (100%) | Yes (Java 8+ allows default/static methods) | No | Only static + final |
A class declared with the abstract
keyword.
It cannot be instantiated, but can have both abstract and concrete (normal) methods.
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");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
a.sound(); // Dog barks
a.sleep(); // Sleeping...
}
}
Animal a = new Animal(); โ
An interface is a fully abstract blueprint of a class that contains only method declarations (until Java 7).
From Java 8 onward, interfaces can have some implemented methods too (default, static, private).
default
methods โ allow method body (for backward compatibility)static
methods โ useful for utility/helper methodsprivate
methods (Java 9+) โ for internal reuse inside interfacepublic static final
โ like a global constant.interface Vehicle {
void start(); // public abstract by default
default void honk() {
System.out.println("Beep Beep!");
}
static int getWheels() {
return 4;
}
private void helper() {
System.out.println("Internal helper method");
}
int MAX_SPEED = 120; // public static final by default
}
class Car implements Vehicle {
public void start() {
System.out.println("Car starts with key");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car();
v.start(); // Output: Car starts with key
v.honk(); // Output: Beep Beep!
System.out.println("Max Speed: " + Vehicle.MAX_SPEED); // Output: 120
System.out.println("Wheels: " + Vehicle.getWheels()); // Output: 4
}
}
public abstract
by default (unless marked default/static)public static final
default
, static
, and private
methodsUse Case | Go For... |
---|---|
You want partial abstraction | Abstract Class |
You need to define common base with some logic | Abstract Class |
You want 100% abstraction (only method declarations) | Interface |
You need multiple inheritance | Interface |
You're building a plug-and-play system | Interface |
You can use both together:
interface Printable {
void print();
}
abstract class Machine {
abstract void start();
}
class Printer extends Machine implements Printable {
public void print() {
System.out.println("Printing...");
}
void start() {
System.out.println("Machine started.");
}
}
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 |
Can abstract class have a constructor? | โ Yes |
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. |
Can we implement multiple interfaces? | โ Yes |
Can abstract class implement interface? | โ Yes, but must override methods or remain abstract |
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 | Multiple |