Java - OOP - Inheritance - Multiple Interfaces
In Java, it is not possible to inherit from multiple classes, but we can inherit from multiple interfaces. An interface is a contract that specifies a set of methods that a class must implement. When a class implements an interface, it agrees to implement all of the methods defined in that interface.
To implement multiple interfaces, a class must use the "implements" keyword followed by the names of the interfaces separated by commas.
Let's consider an example to understand it better. Suppose we have three classes: ClassA, ClassB, and ClassC. ClassB and ClassC both inherit from ClassA, and another class ClassD inherits from both ClassB and ClassC. This is an example of multiple inheritance.
class ClassA {
public void methodA() {
System.out.println("Method of ClassA");
}
}
class ClassB extends ClassA {
public void methodB() {
System.out.println("Method of ClassB");
}
}
class ClassC extends ClassA {
public void methodC() {
System.out.println("Method of ClassC");
}
}
class ClassD extends ClassB, ClassC {
public void methodD() {
System.out.println("Method of ClassD");
}
}
In this example, ClassA is the parent class of both ClassB and ClassC. ClassB and ClassC have their own methods, methodB and methodC, respectively. ClassD is a child class that inherits from both ClassB and ClassC and thus inherits all the methods of its parent classes.
Now, let's say we create an object of ClassD and call its methods:
ClassD obj = new ClassD();
obj.methodA(); // Method of ClassA
obj.methodB(); // Method of ClassB
obj.methodC(); // Method of ClassC
obj.methodD(); // Method of ClassD
As you can see, we can call all the methods from the parent classes as well as the child class.
Multiple inheritance can be useful in certain scenarios, but it can also lead to complexity and conflicts, which is why it is not supported in some programming languages. In Java, multiple inheritance is achieved through interfaces, which is a topic for another discussion.
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}
interface AirVehicle {
void fly();
}
interface WaterVehicle {
void swim();
}
class AmphibiousVehicle extends Vehicle implements AirVehicle, WaterVehicle {
public void fly() {
System.out.println("Amphibious vehicle is flying");
}
public void swim() {
System.out.println("Amphibious vehicle is swimming");
}
}
public class Main {
public static void main(String[] args) {
AmphibiousVehicle av = new AmphibiousVehicle();
av.run();
av.fly();
av.swim();
}
}
In this example, we have a class Vehicle which has a method run(). We also have two interfaces, AirVehicle and WaterVehicle, which have methods fly() and swim() respectively.
The class AmphibiousVehicle extends the Vehicle class and implements both AirVehicle and WaterVehicle interfaces. This means that it inherits the run() method from Vehicle and has to implement the fly() and swim() methods from the interfaces.
In the main() method, we create an object of AmphibiousVehicle and call its run(), fly(), and swim() methods, which respectively print "Vehicle is running", "Amphibious vehicle is flying", and "Amphibious vehicle is swimming" to the console.