2

Here is an example of interface which is showing multiple inheritance.And i want to know how can we achieve multiple inheritance by interface and why can't we use it by class?

interface Printable // interface1
{  
    void print();  
} 

interface Showable //interface2
{  
    void print();  
}  

class TestTnterface1 implements Printable,Showable
{  
    public void print()
{
    System.out.println("Hello");
}  

public static void main(String args[])
{  
    TestTnterface1 obj = new TestTnterface1();  
    obj.print();  //which print method will be called now?
}  
}  
  • It'll call `TestTnterface1.print()`. The interfaces are not involved in that. You can remove `implements Printable,Showable`, and it'll still compile, and it'll still call `TestTnterface1.print()` when executed. – Andreas Aug 24 '16 at 06:28

5 Answers5

2

First question:

The implementation satisfies both contracts, so whether you cast your concrete class to Printable or Showable, it will be used just the same. You will notice that there is an "impossible" situation, like this:

public interface Printable{
    String print();
}

public interface Showable{
    void print();
}

public class Impl implements Printable,Showable{
    /*impossible to implement because you cannot have the same method signature with two different return types*/
}

The multiple inheritance would usually imply there is something useful added by each parent. For example, if Printable would have the method #print and Showable would have the method #show, inheriting them both would add functionality to your program.

If you want to combine functionality from several concrete classes, you might want to look into composition instead of inheritance.

The answer to the second question is more tricky. You can find a longer discussion here. While it would have been possible for the creators of Java to put in such option, it would have opened the door to some pretty messed up code. Think of the example you gave: what if Printable and Showable had concrete implementations for the #print method? Which implementation should the inheriting class have chosen?

Community
  • 1
  • 1
Deroude
  • 1,052
  • 9
  • 24
1

There is only one concrete method that can be called

i.e.

class TestTnterface1 implements Printable,Showable
{  
    public void print()
    {
       System.out.println("Hello");
    } 
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

In any case the runtime will call the TestTnterface1#print() method, because it is the implementation (code that can be executed).

An interface is a contract that the class that implements must follow, and is used only at compile time. The compiler checks, if the implementor has (non abstract) methods with same name and signature.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • why there will be no ambiguity in both the methods at run time? Please explain in brief. – Mayank Dudeja Aug 24 '16 at 06:29
  • @Mayank Dudeja Because a method in an interface has no code (to be exact, before Java 8). So there is no ambiguity at runtime. At runtime there will be only one implementation (you cannot have more than one method with the same signature in a class). – PeterMmm Aug 24 '16 at 06:38
0

Question 1: Which method is called?

There is only one method-implementation that can actually be called. So, there is no ambiguity.

Question 2: How can we achieve multiple inheritance by interface?

You already gave an example for this. The special point about this kind of inheritance is that there can't be multiple implementations (until Java 7). Java 8 in contrast allows default methods in interfaces.

Question 3: Why can't we use it by class?

It avoids inheritance of state.

https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html gives some more information.

mm759
  • 1,404
  • 1
  • 9
  • 7
-2

Since both interfaces have print() method and TestInterface1 class has its implementation, it will call its implementation.

public void print()
{
    System.out.println("Hello");
} 
Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
viki s
  • 95
  • 6