Implement Interface using Polymorphism

How an interface can be implemented using polymorphism?

Why multiple inheritence is not used in Java?

Questions by adarsh01

Showing Answers 1 - 21 of 21 Answers

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.

abuthahir.d

  • Apr 14th, 2008
 

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

  Was this answer useful?  Yes

rariedel

  • May 28th, 2008
 

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.

  Was this answer useful?  Yes

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();

}
}

  Was this answer useful?  Yes

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();

    }
}

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

Sarje

  • Aug 18th, 2009
 

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.  


Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions