In the previous article, we learned what an interface is and why it acts as a contract between the client and the service provider.
Now it's time to understand one of the most frequently asked Java interview topics:
- What is the difference between
extendsandimplements? - Can a class extend multiple classes?
- Can a class implement multiple interfaces?
- Why are interface methods always
public abstract? - Why are interface variables always
public static final?
These concepts form the foundation of Java's multiple inheritance model and are commonly used in frameworks like Spring Boot, Hibernate, and the Java Collections Framework.
Why Do We Need extends and implements?
Java supports two kinds of inheritance:
- Implementation Inheritance (using classes)
- Contract Inheritance (using interfaces)
To distinguish between them, Java provides two keywords:
extendsimplements
extends vs implements
The extends keyword is used when inheriting from a class or extending another interface.
The implements keyword is used when a class provides the implementation for an interface.
| Keyword | Used With | Purpose |
|---|---|---|
extends |
Class → Class, Interface → Interface | Inherit existing behavior |
implements |
Class → Interface | Provide implementation for a contract |
A Class Can Extend Only One Class
Java does not support multiple inheritance of classes.
Example:
class Employee {
public void work() {
System.out.println("Employee is working.");
}
}
class Manager extends Employee {
}
Enter fullscreen mode Exit fullscreen mode
Step-by-Step Explanation
Step 1
Employee defines the behavior.
Step 2
Manager extends Employee.
Step 3
Manager inherits the work() method.
Why Can't a Class Extend Multiple Classes?
Suppose Java allowed this:
class A {
public void display() {
System.out.println("Class A");
}
}
class B {
public void display() {
System.out.println("Class B");
}
}
class C extends A, B {
}
Enter fullscreen mode Exit fullscreen mode
Which display() method should Java inherit?
A
\
\
C
/
/
B
Enter fullscreen mode Exit fullscreen mode
This is known as the Diamond Problem.
To avoid ambiguity, Java allows a class to extend only one class.
A Class Can Implement Multiple Interfaces
Unlike classes, interfaces contain contracts rather than implementations.
Therefore, there is no ambiguity.
Example:
interface PaymentService {
void processPayment();
}
interface NotificationService {
void sendNotification();
}
class OnlineOrderService implements PaymentService, NotificationService {
@Override
public void processPayment() {
System.out.println("Payment processed.");
}
@Override
public void sendNotification() {
System.out.println("Notification sent.");
}
}
Enter fullscreen mode Exit fullscreen mode
Step-by-Step Explanation
Step 1
PaymentService defines one service.
Step 2
NotificationService defines another service.
Step 3
OnlineOrderService implements both interfaces.
Step 4
Both methods must be implemented.
A Class Can Extend One Class and Implement Multiple Interfaces
Java allows both together.
Example:
interface PaymentService {
void processPayment();
}
interface NotificationService {
void sendNotification();
}
class Order {
public void displayOrder() {
System.out.println("Displaying order.");
}
}
class OnlineOrder extends Order
implements PaymentService, NotificationService {
@Override
public void processPayment() {
System.out.println("Payment completed.");
}
@Override
public void sendNotification() {
System.out.println("Customer notified.");
}
}
Enter fullscreen mode Exit fullscreen mode
This is a very common design in enterprise applications.
An Interface Can Extend Multiple Interfaces
Interfaces support multiple inheritance.
Example:
interface PaymentService {
void processPayment();
}
interface NotificationService {
void sendNotification();
}
interface OrderService
extends PaymentService, NotificationService {
}
Enter fullscreen mode Exit fullscreen mode
Here, OrderService inherits both method declarations.
Valid Syntax Combinations
| Expression | Valid? | Explanation |
|---|---|---|
class B extends A |
✅ | Class inherits another class |
interface B extends A |
✅ | Interface extends interface |
class C implements A |
✅ | Class implements interface |
class C extends A implements B |
✅ | Extend one class and implement interfaces |
interface C extends A, B |
✅ | Interface extends multiple interfaces |
class C implements A extends B |
❌ | Invalid syntax |
Interface Methods
Every method declared inside an interface is implicitly:
publicabstract
These declarations are equivalent:
interface PaymentService {
void processPayment();
}
Enter fullscreen mode Exit fullscreen mode
interface PaymentService {
public abstract void processPayment();
}
Enter fullscreen mode Exit fullscreen mode
The compiler automatically adds public and abstract.
Why Are Interface Methods Public?
Interfaces represent contracts.
Any implementation class, regardless of package, must be able to access and implement these methods.
Hence, they are public.
Why Are Interface Methods Abstract?
Interfaces specify what should happen.
Implementation classes decide how it happens.
Illegal Modifiers for Interface Methods
Traditional abstract interface methods cannot use the following modifiers:
privateprotectedfinalsynchronizednative
For example:
interface PaymentService {
final void processPayment();
}
Enter fullscreen mode Exit fullscreen mode
Compile-Time Error
modifier final not allowed here
Enter fullscreen mode Exit fullscreen mode
Note: Since Java 8, interfaces can also declare default and static methods, and since Java 9, private methods are allowed inside interfaces for code reuse. Those modern features will be covered in a later article. In this article, we're focusing on traditional abstract interface methods.
Interface Variables
Interfaces can contain variables.
However, every interface variable is implicitly:
publicstaticfinal
Example:
interface ApplicationConstants {
int MAX_USERS = 100;
}
Enter fullscreen mode Exit fullscreen mode
The compiler treats it as:
public static final int MAX_USERS = 100;
Enter fullscreen mode Exit fullscreen mode
Why Are Interface Variables Public Static Final?
Public
Every implementation class should access the constant.
Static
The value belongs to the interface itself.
No object is required.
Example:
System.out.println(ApplicationConstants.MAX_USERS);
Enter fullscreen mode Exit fullscreen mode
Final
The value should never change.
Implementation classes cannot modify it.
Interface Variables Must Be Initialized
Incorrect:
interface ApplicationConstants {
int MAX_USERS;
}
Enter fullscreen mode Exit fullscreen mode
Compile-Time Error
= expected
Enter fullscreen mode Exit fullscreen mode
Correct:
interface ApplicationConstants {
int MAX_USERS = 100;
}
Enter fullscreen mode Exit fullscreen mode
Interface Variables Cannot Be Modified
Example:
interface ApplicationConstants {
int MAX_USERS = 100;
}
class Test implements ApplicationConstants {
public static void main(String[] args) {
// MAX_USERS = 200; ❌ Compile-time error
System.out.println(MAX_USERS);
}
}
Enter fullscreen mode Exit fullscreen mode
The constant is inherited but cannot be reassigned.
Common Beginner Mistakes
Mistake 1: Trying to Extend Multiple Classes
class C extends A, B {
}
Enter fullscreen mode Exit fullscreen mode
Java does not allow this.
Mistake 2: Forgetting to Implement Every Interface Method
Every abstract method must be implemented unless the class is abstract.
Mistake 3: Trying to Modify Interface Variables
MAX_USERS = 500;
Enter fullscreen mode Exit fullscreen mode
This results in a compile-time error because interface variables are final.
Mistake 4: Thinking Interface Variables Are Instance Variables
They are static.
Access them using the interface name.
ApplicationConstants.MAX_USERS
Enter fullscreen mode Exit fullscreen mode
Best Practices
- Use
extendsonly for inheritance between related classes. - Prefer interfaces to define behaviors and contracts.
- Keep interface variables limited to true constants.
- Avoid storing mutable state inside interfaces.
- Use meaningful interface names such as
PaymentService,NotificationService, andOrderRepository.
Interview Questions
1. What is the difference between extends and implements?
extends is used for inheritance, while implements is used to provide the implementation of an interface.
Why interviewers ask
To verify your understanding of Java inheritance.
2. Can a class extend multiple classes?
No.
Java prevents multiple class inheritance to avoid ambiguity.
3. Can a class implement multiple interfaces?
Yes.
A class can implement any number of interfaces.
4. Can an interface extend multiple interfaces?
Yes.
This is Java's way of supporting multiple inheritance of type.
5. Why are interface methods public abstract?
Because interfaces define publicly accessible contracts that implementing classes must provide.
6. Why are interface variables public static final?
They represent shared constants that belong to the interface and cannot be modified.
7. Why must interface variables be initialized?
Because they are final, and final variables must receive a value exactly once.
Quick Memory Trick 🧠
Remember EIM:
E → Extends → One Class
I → Implements → Many Interfaces
M → Multiple Interface Inheritance
Enter fullscreen mode Exit fullscreen mode
Think EIM whenever you see Java inheritance.
Key Takeaways
-
extendsis used for class inheritance and interface inheritance. -
implementsis used when a class implements an interface. - A class can extend only one class.
- A class can implement multiple interfaces.
- An interface can extend multiple interfaces.
- Interface methods are implicitly
public abstract. - Interface variables are implicitly
public static final. - Interface constants must be initialized when declared.
If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.
Happy Coding!
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.