Does Java supports Multiple Inheritance
NO. Java does not support multiple inheritance for classes.
Let us understand the reason behind this.
Consider the following example:
class Animal{
public void speaks(){
System.out.println("Animal speaks...");
}
}
class Human{
public void speaks(){
System.out.println("Animal speaks...");
}
}
//suppose if multiple inheritance was allowed
class Species extends Human, Animal{
public static void main(String[] args){
Species s = new Species();
s.speaks(); //this line is ambiguous
}
}
Here both Human and Animal contains speaks() method, Since Species class is trying to access this method here, JVM can't decide which class method to choose hence it renders a compile time error.
Note: Mutiple inheritance is not allowed for Java classes. But for interfaces, multiple inheritance is allowed.
Comments
Post a Comment