GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 470 of 502    Print  
Implement Interface using Polymorphism
How an interface can be implemented using polymorphism?

Why multiple inheritence is not used in Java?



  
Total Answers and Comments: 7 Last Update: August 19, 2009     Asked by: adarsh01 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: anubandhan
 
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
April 09, 2008 03:20:17   #1  
anubandhan Member Since: April 2008   Contribution: 2    

RE: Implement Interface using Polymorphism
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.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
April 14, 2008 07:36:39   #2  
abuthahir.d Member Since: January 2008   Contribution: 9    

RE: Implement Interface using Polymorphism
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

 
Is this answer useful? Yes | No
May 28, 2008 19:35:37   #3  
rariedel Member Since: May 2008   Contribution: 8    

RE: Implement Interface using Polymorphism

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.


 
Is this answer useful? Yes | No
June 01, 2008 15:49:19   #4  
Karuna Reddy Member Since: June 2007   Contribution: 40    

RE: Implement Interface using Polymorphism
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();

}
}

 
Is this answer useful? Yes | No
June 06, 2008 00:49:38   #5  
Karuna Reddy Member Since: June 2007   Contribution: 40    

RE: Implement Interface using Polymorphism

interface I1
{
void disp();
}


class MyClass implents I1

{

void disp("JAI SREE RAM");

}
public class TestPoly
{
public static void main(String args[])

{

I1 i new MyClass();
i.disp();

}
}


 
Is this answer useful? Yes | No
March 30, 2009 12:17:01   #6  
shouvanik1979 Member Since: March 2009   Contribution: 5    

RE: Implement Interface using Polymorphism
Polymorphism means "one form many actions".

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.

 
Is this answer useful? Yes | No
August 18, 2009 08:15:44   #7  
Sarje Member Since: August 2009   Contribution: 62    

RE: Implement Interface using Polymorphism
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.



 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape