🔹Interface abstract class difference
1. Basic Concept
- Abstract Class
- A class that cannot be instantiated and may contain both abstract (no body) and concrete (with body) methods.
- Interface
- A completely abstract contract (before Java 8) that defines what a class must do, not how.
🔹 2. Methods
- Abstract Class
- Can have:
- Abstract methods
- Concrete methods
- Can have:
- Interface
- Traditionally only abstract methods
- Since Java 8:
defaultmethods (with body)staticmethods
- Since Java 9:
privatemethods (for internal use)
🔹 3. Variables
- Abstract Class
- Can have instance variables (fields)
- Interface
- Variables are:
publicstaticfinal(constants only)
- Variables are:
🔹 4. Constructors
- Abstract Class
- Can have constructors
- Interface
- Cannot have constructors
🔹 5. Inheritance
- Abstract Class
- A class can extend only one abstract (or any) class
→ supports single inheritance
- A class can extend only one abstract (or any) class
- Interface
- A class can implement multiple interfaces
→ supports multiple inheritance
- A class can implement multiple interfaces
🔹 6. Access Modifiers
- Abstract Class
- Methods and variables can have any access modifier (
private,protected,public)
- Methods and variables can have any access modifier (
- Interface
- Methods are implicitly
public(unless private helper methods) - Fields are always
public static final
- Methods are implicitly
🔹 7. When to Use
- Abstract Class
- Use when:
- You want to share code (implementation) among related classes
- There is an “is-a” relationship (e.g., Animal → Dog)
- Use when:
- Interface
- Use when:
- You want to define a contract
- You need multiple inheritance
- Classes are unrelated but share behavior (e.g.,
Flyable,Runnable)
- Use when:
🔹 8. Example
Abstract Class
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Sleeping...");
}
}
Interface
interface Animal {
void sound(); // implicitly public and abstract
}
🔹Difference between Abstract Class and Interface
The abstract class achieves partial abstraction (0 to 100%), whereas the interface achieves full abstraction (100%). The following table shows the key differences between difference between abstract class and interface:
| Abstract Class | Interface |
|---|---|
| Abstract classes can have abstract and non-abstract methods. | An interface can have only abstract methods. Since Java 8, it can have default and static methods also. |
| Abstract class does not support multiple inheritance. | Interface supports multiple inheritance. |
| Abstract class can have final, non-final, static and non-static variables. | An interface has only static and final variables. |
| Abstract class can provide the implementation of the interface. | An interface cannot provide the implementation of an abstract class. |
| The abstract keyword is used to declare an abstract class. | The interface keyword is used to declare an interface. |
| An abstract class can extend another Java class and implement multiple Java interfaces. | An interface can extend another Java interface only. |
| An abstract class can be extended using the keyword “extends”. | An interface can be implemented using the keyword “implements”. |
| A Java abstract class can have class members like private, protected, etc. | Members of a Java interface are public by default. |
| Example: public abstract class Shape{ public abstract void draw(); } |
Example: public interface Drawable{ void draw(); } |