In multiple Inheritance,ex: class A, Class B inherited from Class A, Class C inherited from class A, Class D inherited From Class B,and Class C.in this Class D have dual copy of the methods ,datamembers defined in Class A, Vertual Function Is there in C++ but,again the pointer are not used in java. So to avoid this ambiguity, only single chain inheritance is there, not multiple inheritance.
Above answer was rated as good by the following members: swpnl_ptl, bino75
In multiple Inheritance ex: class A Class B inherited from Class A Class C inherited from class A Class D inherited From Class B and Class C.in this Class D have dual copy of the methods datamembers defined in Class A Vertual Function Is there in C++ but again the pointer are not used in java. So to avoid this ambiguity only single chain inheritance is there not multiple inheritance.
Multiple inheritance will cause ambiguity in Java if any two different classes have the same method. The necessity of Multiple Inheritance is catered using Interfaces
The first part of this question is ill-formed. Polymorphism is a concept and a feature of many object oriented languages including Java that support handling multiple data types through a uniform interface. The question implies that there is something special that must be done to use polymorphism when implementing an interface. Any time an interface is implemented the implementation itself is intrinsically an application of polymorphism.
The generally accepted explanation of why Java does not support multiple inheritance is that the language designer's wanted to keep the language simple and the complexities introduced by multiple inheritance would defeat that desired simplicity.
interface MyInterface{ public void HiMsg(); } public class MyClass implements MyInterface{ public void HiMsg(){ System.out.println("Hi Guys"); } } Class PolyWithInterface{ public static void main(String args[]){
MyInterface myInterface new MyClass(); myInterface.HiMsg();
Lets say a class Test implements an interface ITest. Accordingly
public interface ITest { public void testMethod(); }
//a class implementing ITest public class Test implements ITest { public void testMethod() { System.out.println("inside testMethod of class Test...."); }
}
public class Test2 implements ITest { public static void main(String arg[]) { ITest iTest new Test2(); //interface reference variable iTest actually points to //object of class Test and so will dynamically call //implemented method testMethod() of class Test iTest.testMethod(); }
public void testMethod() { System.out.println("inside testMethod of Test2...."); } }
Multiple inheritance is not used in JAVA to add robustness and security.
I think your question is how to achieve Runtime Polymorphism using using interface. then the answer is : interface MyInf { void show(); } class A implements MyInf { public void show() { System.out.println("In class A"); } } class B implements MyInf { public void show() { System.out.println("In class B"); } } public class Test { public static void main(String args[]) { MyInf reff; reff new A(); reff.show(); reff new B(); reff.show(); } } Now see here in Test class reference of interface MyInf can refer to any of the class A's or B's object. Presence of show() method will be checked at compile time and in interface MyInf only. But which of the methods (method in A or method in B) will be called using this reference will be resolved at run time this is how Runtime Polymophism achieved in Java. Calling of the method will be done on the basis of the object denoted by the reference.