Interface Implementation Rules
- Whenever we implement an interface, we must provide implementation for all methods. Otherwise, the class must be declared as abstract.
- All interface methods are public and abstract by default. Hence, while implementing, methods must be declared as public, otherwise a compile-time error occurs.
Explanation:
Interface acts like a contract. If a class signs (implements) that contract, it must fulfill all conditions (methods). If it cannot, responsibility is passed to the child class by declaring it abstract.
Interface acts like a contract. If a class signs (implements) that contract, it must fulfill all conditions (methods). If it cannot, responsibility is passed to the child class by declaring it abstract.
First Way (Complete Implementation)
interface Interf {
void m();
void m1();
}
class ServiceProvider implements Interf {
public void m() {
System.out.println("Hello");
}
public void m1() {
System.out.println("World");
}
}
class Test {
public static void main(String[] args) {
ServiceProvider sp = new ServiceProvider();
sp.m();
sp.m1();
}
}
Output:
Hello
World
Hello
World
Second Way (Partial Implementation using Abstract Class)
interface Interf {
void m();
void m1();
}
abstract class ServiceProvider implements Interf {
public void m() {
System.out.println("Hello");
}
}
class ServiceProvider2 extends ServiceProvider {
public void m1() {
System.out.println("World");
}
}
class Test {
public static void main(String[] args) {
ServiceProvider2 sp = new ServiceProvider2();
sp.m();
sp.m1();
}
}
Explanation:
Here, the parent class does not implement all methods, so it is declared abstract. The child class completes the remaining implementation.
Here, the parent class does not implement all methods, so it is declared abstract. The child class completes the remaining implementation.
Wrong Way (Common Mistakes)
class ServiceProvider implements Interf {
void m() { // ERROR: must be public
System.out.println("Hello");
}
}
Error: attempting to assign weaker access privileges; was public
Reason:
Interface methods are always public. Reducing visibility (default) causes compile-time error.
Interface methods are always public. Reducing visibility (default) causes compile-time error.
Extends vs Implements
- A class can extend only one class
- An interface can extend multiple interfaces
- A class can implement multiple interfaces
- A class can extend a class and implement interfaces simultaneously
Example
interface A {}
interface B {}
interface C extends A, B {}
class X extends Y implements A, B {}
Important Rules
✔ A class cannot extend multiple classes ❌
✔ A class can implement multiple interfaces ✔
✔ An interface cannot implement another interface ❌
✔ An interface can extend multiple interfaces ✔
✔ A class can implement multiple interfaces ✔
✔ An interface cannot implement another interface ❌
✔ An interface can extend multiple interfaces ✔
Concept-Based Questions
Q1: X extends Y → Valid when?
Ans: Both must be classes OR both must be interfaces.
Ans: Both must be classes OR both must be interfaces.
Q2: X extends Y, Z
Ans: All must be interfaces.
Ans: All must be interfaces.
Q3: X implements Y, Z
Ans: X = class, Y & Z = interfaces.
Ans: X = class, Y & Z = interfaces.
Q4: X extends Y implements Z
Ans: X & Y = classes, Z = interface.
Ans: X & Y = classes, Z = interface.
Q5: implements X extends Y ❌
Ans: Invalid syntax (extends must come first).
Ans: Invalid syntax (extends must come first).
0 Comments
Hello