Java provides three core building blocks for object-oriented design:
- Class
- Abstract Class
- Interface
All three define types, but they differ in purpose, capabilities, and usage. This note explains each one clearly with rules, examples, and a comparison table.
A class is a blueprint from which objects are created. It contains:
- Fields (variables)
- Methods (fully implemented)
- Constructors
- Static blocks
- Final methods
- Inner classes
class Car {
String brand;
Car(String brand) {
this.brand = brand;
}
void start() {
System.out.println(brand + " starting...");
}
}- Can be instantiated (
new Car("BMW")) - Supports complete implementation
- Can extend one parent class
- Can implement multiple interfaces
An abstract class is a partially implemented class. It can contain:
- Abstract methods (no body)
- Concrete methods (with body)
- Constructors
- Variables
- Final/static blocks
- Static methods
abstract class Animal {
abstract void sound(); // must be overridden
void sleep() { // fully implemented
System.out.println("Sleeping...");
}
}- Cannot be instantiated directly
- Child class must implement abstract methods
- Allows shared code + partial abstraction
- Can implement interfaces
- Can extend only one class
- When you want base functionality + force child to implement some behavior.
An interface defines a contract. Classes implementing the interface must provide the behavior.
Interfaces contain:
- Abstract methods
- Default methods (Java 8+)
- Static methods (Java 8+)
- Private methods (Java 9+)
- Constants (
public static final)
interface Payment {
void pay(double amount); // abstract method
}Implementation:
class UPI implements Payment {
public void pay(double amount) {
System.out.println("Paid: " + amount);
}
}- Cannot have instance fields (only constants)
- No constructors
- Supports multiple inheritance
- Implemented using
implementskeyword - 100% abstraction (except default/static methods)
- When you want common behavior across unrelated classes
(e.g.,
Runnable,Comparable,List)
| Feature | Class | Abstract Class | Interface |
|---|---|---|---|
| Instantiation | ✔ Yes | ❌ No | ❌ No |
| Methods allowed | Concrete | Abstract + Concrete | Abstract, Default, Static |
| Variables allowed | Any type | Any type | Only public static final |
| Constructors | ✔ Yes | ✔ Yes | ❌ No |
| Inheritance | Single | Single | Multiple |
| Access modifiers | All allowed | All allowed | Abstract methods are always public |
| Supports multiple inheritance | ❌ No | ❌ No | ✔ Yes |
| Level of Abstraction | 0% | 0–100% | 100% (traditionally) |
| Keyword | class |
abstract class |
interface |
| Can extend another class? | ✔ Yes | ✔ Yes | ❌ No |
| Can implement interfaces? | ✔ Yes | ✔ Yes | ✔ Yes (extend only interfaces) |
| Object creation | Yes | No | No |
- You need full implementation
- No abstraction is required
- You want to create objects directly
- You want to share common behavior among related classes
- You want to enforce some mandatory methods
- You need constructors or non-final variables
- You need partial abstraction
Example use case:
All vehicles have common features → base class Vehicle.
- You want multiple inheritance
- You want to define behavior for unrelated classes
- You want pure abstraction/contract
- You want to allow different implementations
Example use case:
Runnable, Comparable, List, Map
You can build a house (object) directly.
Foundation is ready, but child classes must complete missing parts.
“A building must have doors, windows, walls.” But how they are built → up to the implementing class.
abstract class Animal {
abstract void sound();
void breathe() {
System.out.println("Breathing");
}
}interface Pet {
void play();
}class Dog extends Animal implements Pet {
void sound() {
System.out.println("Bark");
}
public void play() {
System.out.println("Dog playing");
}
}Because interfaces cannot create objects.
Yes. Only abstract methods must be overridden.
Yes, but must provide implementation or remain abstract.
To avoid the diamond problem with classes while still supporting polymorphism.
No. It can only extend other interfaces.
-
Class → Full implementation
-
Abstract Class → Partial abstraction + common code
-
Interface → Full abstraction + multiple inheritance
-
Use abstract class for related types, interface for common behavior
-
Interface methods are public by default
-
Abstract class can have constructors, interfaces cannot