What is multiple inheritance?
Ans:- In multiple inheritance, a class can inherit from more than one classes. but Java doesn't support multiple inheritance.
Why Java doesn't support multiple inheritance?
Ans:- Java does not support multiple inheritance because Java does not support ambiguity. Assuming if there are 2 classes A and B having a method name show( ). If both are inherited by a class C then there will be an ambiguity of which show( ) method is to be called.
Example:-
class A {
public void show( ) {
System.out.print("In show of A");
}
}
class B {
public void show( ) {
System.out.print("In show of B");
}
}
class C extends A , B {
public static void main(String [ ] args) {
C obj=new C();
obj.show();
}
}
Output:-
Compile time error!
In the above code, on calling of the method show( ) using C class object will cause compile time error such as whether to call A class show() or B class show() method.
How can we achieve multiple inheritance in Java?
Ans:- As we know, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity.
Example:-
interface A {
public void show();
}
interface B {
public void print();
}
class C implements A, B {
public void show( ) {
System.out.println("Calling show() method...");
}
public void print( ) {
System.out.println("Calling print() method...");
}
}
public class Demo {
public static void main(String [ ] args) {
C obj=new C();
obj.show();
obj.print();
}
}
Output:-
Calling show() method...
Calling print() method...
In the above code,The interface A and B have one abstract method each i.e. show() and print(). The class C implements the interfaces A and B. In the method main() in class Demo, an object obj of class C is created. Then the methods show() and print() are called.
Ans:- In multiple inheritance, a class can inherit from more than one classes. but Java doesn't support multiple inheritance.
Why Java doesn't support multiple inheritance?
Ans:- Java does not support multiple inheritance because Java does not support ambiguity. Assuming if there are 2 classes A and B having a method name show( ). If both are inherited by a class C then there will be an ambiguity of which show( ) method is to be called.
Example:-
class A {
public void show( ) {
System.out.print("In show of A");
}
}
class B {
public void show( ) {
System.out.print("In show of B");
}
}
class C extends A , B {
public static void main(String [ ] args) {
C obj=new C();
obj.show();
}
}
Output:-
Compile time error!
In the above code, on calling of the method show( ) using C class object will cause compile time error such as whether to call A class show() or B class show() method.
How can we achieve multiple inheritance in Java?
Ans:- As we know, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity.
Example:-
interface A {
public void show();
}
interface B {
public void print();
}
class C implements A, B {
public void show( ) {
System.out.println("Calling show() method...");
}
public void print( ) {
System.out.println("Calling print() method...");
}
}
public class Demo {
public static void main(String [ ] args) {
C obj=new C();
obj.show();
obj.print();
}
}
Output:-
Calling show() method...
Calling print() method...
In the above code,The interface A and B have one abstract method each i.e. show() and print(). The class C implements the interfaces A and B. In the method main() in class Demo, an object obj of class C is created. Then the methods show() and print() are called.
0 Comments