An abstract class in Java is a class that cannot be instantiated (object cannot be created) and is used as a base class for other classes.
It may contain:
Declared using the keyword abstract
An interface is a completely abstract blueprint of a class that contains only abstract methods (by default) and constants.
interfaceCannot create objects directly
Can have:
Supports single inheritance
Subclass must implement all abstract methods
implementsabstract class Animal {
abstract void sound(); // abstract method
void sleep() { // concrete method
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Barking");
}
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
d.sleep();
}
}
Animal is an abstract class.
It has:
sound() → abstractsleep() → concreteDog inherits Animal using extends.
Dog must override sound().
Object of Dog is created (not Animal).
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
System.out.println("Car starts");
}
}
public class Test {
public static void main(String[] args) {
Car c = new Car();
c.start();
}
}
Vehicle is an interface.Car implements it using implements.start() must be defined in Car.| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Abstract + Concrete | Mostly Abstract |
| Variables | Normal variables allowed | Only constants |
| Inheritance | Single | Multiple |
| Constructors | Allowed | Not allowed |
| Keyword | extends |
implements |
abstract keywordimplements keywordinterface A {
void show();
}
interface B {
void display();
}
class Test implements A, B {
public void show() {
System.out.println("Show method");
}
public void display() {
System.out.println("Display method");
}
}
✔ Java allows multiple inheritance using interfaces (not classes)
AnimalDogDog → Animal)VehicleCarCar → Vehicle)extendsimplementsAbstract Class
Interface
implementsKey Difference
Open this section to load past papers