Interface vs Abstract Class
Interface vs Abstract Class
Both are used to achieve abstraction in Java. Neither can be instantiated, and both can contain methods that child classes must implement. The difference lies in what they represent and what they can hold.
In simple words:
Abstract Class = partial blueprint (shared state + code). Interface = contract (what a class must do).
Real-Life Example
1Abstract Class → "Vehicle"2 Every vehicle has wheels, fuel and speed (shared state and code),3 but each vehicle starts differently.4 5Interface → "Chargeable"6 Phone, Laptop and Car can all be charged.7 They are unrelated, but share the same capability.Abstract Class Example
abstract class Vehicle {String name;int speed;Vehicle(String name) {this.name = name;}// Abstract method: child must implementabstract void start();// Normal method: shared by all vehiclesvoid stop() {System.out.println(name + " stopped");}}class Car extends Vehicle {Car(String name) {super(name);}@Overridevoid start() {System.out.println(name + " starts with a key");}}public class Main {public static void main(String[] args) {Vehicle v = new Car("Swift");v.start();v.stop();}}// Output:// Swift starts with a key// Swift stopped
Interface Example
interface Chargeable {void charge(); // public abstract by default}class Phone implements Chargeable {@Overridepublic void charge() {System.out.println("Phone is charging");}}class ElectricCar implements Chargeable {@Overridepublic void charge() {System.out.println("Car is charging at the station");}}public class Main {public static void main(String[] args) {Chargeable c1 = new Phone();Chargeable c2 = new ElectricCar();c1.charge();c2.charge();}}// Output:// Phone is charging// Car is charging at the station
Main Differences
| Feature | Abstract Class | Interface |
|---|---|---|
| Keyword | abstract class | interface |
| Used with | extends | implements |
| Methods | Abstract and normal | Abstract, default, static, private |
| Variables | Any type (instance, static, final) | Only public static final constants |
| Constructors | Yes | No |
| Instance state (fields) | Yes | No |
| Access modifiers | Any (private, protected, public) | Methods are public (private allowed since Java 9) |
| Multiple inheritance | A class can extend only one | A class can implement many |
| Can extend / implement | Extends one class, implements many interfaces | Extends many interfaces |
| Relationship | Closely related classes (IS-A) | Unrelated classes sharing a capability (CAN-DO) |
| Speed of change | Adding a method is easy for children | Adding an abstract method breaks all implementers |
Interface with default Method (Java 8+)
Interfaces can provide a method body using default, so new methods can be added without breaking existing classes.
interface Chargeable {void charge();default void showStatus() {System.out.println("Charging status: OK");}}
Using Both Together
A class can extend one abstract class and implement many interfaces.
abstract class Vehicle {abstract void start();}interface Chargeable {void charge();}class ElectricCar extends Vehicle implements Chargeable {@Overridevoid start() {System.out.println("Electric car starts silently");}@Overridepublic void charge() {System.out.println("Electric car is charging");}}
When to Use Which?
1Classes are closely related and share code or state? → Abstract Class2 Example: Vehicle → Car, Bike, Truck3 4Unrelated classes need the same capability? → Interface5 Example: Chargeable → Phone, Laptop, ElectricCar6 7Need multiple inheritance of type? → Interface8 9Need constructors or instance variables? → Abstract Class| Choose Abstract Class when... | Choose Interface when... |
|---|---|
| You want to share common code and fields | You want to define a contract only |
| Child classes are closely related | Classes are unrelated |
| You need non-public members or constructors | You need a class to have multiple "types" |
| You expect to add non-abstract methods later | You want loose coupling and easy swapping |
Interview Definition
An abstract class is a partially implemented class that can hold state and constructors and is extended by closely related classes. An interface is a contract that defines what a class must do, and a class can implement many of them.
Remember
Abstract class = "is a" with shared state and code. Interface = "can do" capability with no state.