Home >Java >javaTutorial >Abstraction: Abstract Class vs Interface
When designing software in Java, choosing between abstract classes and interfaces can have a big impact on flexibility, maintainability, and readability. In this post, we’ll explore their key differences, when to use one over the other, and look at practical examples to help you master this concept.
Feature | Abstract Class | Interface | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Achieves partial implementation | Achieves complete abstraction | ||||||||||||||||||||||||
Method Implementations | Can have both abstract and concrete methods | All methods are abstract (except default/static methods in Java 8 ) | ||||||||||||||||||||||||
Multiple Inheritance | A class can extend only one abstract class | A class can implement multiple interfaces | ||||||||||||||||||||||||
Fields/Variables | Can have instance variables of any type | All variables are implicitly public, static, and final | ||||||||||||||||||||||||
Constructors | Can have constructors | Cannot have constructors | ||||||||||||||||||||||||
Inheritance Support | Can implement multiple interfaces | Cannot extend an abstract class | ||||||||||||||||||||||||
Use Case | Useful for sharing common code and state | Useful for defining a contract across unrelated classes |
You need to share state or behavior across related classes.
Example: Employee can have fields like name and id with a common getDetails() method, shared among its subclasses Manager and Lead.
abstract class Employee { String name; int id; Employee(String name, int id) { this.name = name; this.id = id; } // Concrete method shared among subclasses public void getDetails() { System.out.println(name + " (ID: " + id + ")"); } // Abstract method to be implemented by subclasses abstract double getSalary(); } class Manager extends Employee { Manager(String name, int id) { super(name, id); } @Override double getSalary() { return 75000; } }
You want to define fields and constructors that subclasses can use.
You need to provide partial implementations or utility methods for subclasses.
You want to define a common behavior across unrelated classes.
Example: Both Car and Drone could implement an Operable interface, as they both share a start() method but are unrelated classes.
interface Operable { void start(); void stop(); } class Car implements Operable { @Override public void start() { System.out.println("Car started."); } @Override public void stop() { System.out.println("Car stopped."); } } class Drone implements Operable { @Override public void start() { System.out.println("Drone started."); } @Override public void stop() { System.out.println("Drone stopped."); } }
You need multiple inheritance to combine different behaviors. For example, a class can implement both Runnable and Serializable.
You want to define default methods to add new functionality without breaking backward compatibility.
Why Can’t an Interface Have Constructors?
Since interfaces define pure abstractions, there’s no need for constructors. Only abstract classes, which contain some implementation, require constructors to initialize state.
Why Use Abstract Classes Instead of Interfaces?
Use abstract classes when:
Can an Abstract Class Implement an Interface?
Yes! Abstract classes can implement one or more interfaces. They can even provide default implementations for the interface methods.
Can You Use Both Abstract Classes and Interfaces in the Same Class?
Yes, a class can extend an abstract class and implement multiple interfaces. This allows for flexible design patterns and multiple inheritance.
By mastering the differences and knowing when to use each, you’ll be better equipped to design scalable, maintainable software systems.
Java Fundamentals
Array Interview Essentials
Java Memory Essentials
Java Keywords Essentials
Collections Framework Essentials
Happy Coding!
The above is the detailed content of Abstraction: Abstract Class vs Interface. For more information, please follow other related articles on the PHP Chinese website!