Interface
Interface
1. Interface
An interface is a contract that defines a set of methods that a class must
implement, but it does not provide any implementation itself (except in some
cases like default methods in Java).
Example in Java:
interface Animal {
void makeSound(); // No implementation
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
}
2. Abstract Class
An abstract class is a class that can have both abstract methods (without
implementation) and concrete methods (with implementation).
Example in Java:
abstract class Animal {
abstract void makeSound(); // Abstract method (must be implemented)
3. Key Differences:
2. Declaration of an Interface
In Java, an interface is declared using the interface keyword.
interface Vehicle {
void start(); // Abstract method (implicitly public and abstract)
void stop();
}
Here:
Output:
Car is starting...
Car is stopping...
Another example:
Here's a complete example that includes both the interface declaration and its
implementation:
// Interface declaration
public interface Animal {
void eat();
void sleep();
}
@Override
public void sleep() {
System.out.println("Dog is sleeping");
}
}
In Java, multiple inheritance is not allowed with classes, but it is allowed with
interfaces.
Why Interface?
✅ Java allows multiple interface implementations, but not multiple class
inheritance.
🚀 Why Interface?
✅ If you want full abstraction, use interfaces instead of abstract classes.
Since Java 8, interfaces can have default and static methods with
implementations.
This allows code reusability without breaking existing implementations.
🚀 Why Interface?
✅ Interfaces can evolve (add methods without affecting old implementations).
// Second interface
interface Sound {
void makeSound();
}
🔹 Output:
Dog is eating...
Dog is barking...
interface A {
default void show() {
System.out.println("Interface A's show method");
}
}
interface B {
default void show() {
System.out.println("Interface B's show method");
}
}
🔹 Output:
💡 If two interfaces have the same default method, the class must override it!
🔹 Conclusion
Multiple interfaces allow Java to mimic multiple inheritance, making code flexible,
reusable, and loosely coupled. 🚀
Multiple Inheritance in Java
In Java, multiple inheritance refers to a class inheriting from more than one parent. Java
does not support multiple class inheritance to avoid ambiguity, but it supports multiple
inheritance using interfaces.
class A {
public:
void show() { cout << "Class A" << endl; }
};
int main() {
D obj;
obj.show(); // Ambiguity: Which show() to call? A from B or A from C?
}
interface Animal {
void eat();
}
interface Sound {
void makeSound();
}
// A class implementing multiple interfaces
class Dog implements Animal, Sound {
public void eat() {
System.out.println("Dog is eating...");
}
🔹 Output:
Dog is eating...
Dog is barking...
interface A {
default void show() {
System.out.println("Interface A's show()");
}
}
interface B {
default void show() {
System.out.println("Interface B's show()");
}
}
🔹 Output:
💡 If two interfaces have the same default method, the class must override it!
🔹 Key Takeaways
✅ Java does not support multiple class inheritance to avoid ambiguity.
✅ Java supports multiple inheritance using interfaces (a class can implement multiple
interfaces).
✅ If two interfaces have conflicting default methods, the class must override them.
✅ This approach provides flexibility without the complexity of multiple class
inheritance. 🚀