When interface inheritance in Java is useful?

2019-07-23 17:14发布

Let's take a glance at the following code in Java.

interface FirstInaterface
{
    public void show();
}

interface SecondInterface extends FirstInaterface
{
    @Override
    public void show();

    public void someConcreteMethod();
}

interface ThirdInterface extends SecondInterface
{
    @Override
    public void show();
}

final class Demo implements ThirdInterface
{
    @Override
    public void show()
    {
        System.out.println("The show() method invoked");
    }

    @Override
    public void someConcreteMethod()
    {
        System.out.println("The someConcreteMethod() method invoked");
    }

}

final public class Main
{
    public static void main(String[] args)
    {
        Demo demo=new Demo();
        demo.show();
        demo.someConcreteMethod();
    }
}

The above code in Java shows a multilevel inheritance of interfaces in which ThirdInterface has two interfaces above it. The show() method is firstly being overridden in the interface SecondInaterface ahd then again in the interface ThirdInterface and this interface is finally being inherited by the class Demo.


In such a scenario, which version of the show() methods from the above interfaces will be incorporated by the class Demo? How is the compiler able to resolve a specific version of the show() method dynamically at run time?


I feel that the show() method in the last interface in the above interface inheritance hierarchy (namely ThirdInterface) will be invoked by the compiler. If it is so then, the show() methods in the above two interfaces (namely, FirstInaterface, SecondInaterface) are useless and serve no purpose at all, since they are declared within interfaces, they themselves can never have their own implementations anywhere. When such kind of interface inheritance is useful?

5条回答
别忘想泡老子
2楼-- · 2019-07-23 17:51

In such a scenario, which version of the show() methods from the above interfaces will be incorporated by the class Demo? How is the compiler able to resolve a specific version of the show() method dynamically at run time?

 Demo demo=new Demo();

showMethod() of the object will get invoked. So here object is of class Demo so its version will be taken.

also at compile time Demo class need to have declaration of show and someConcreteMethod method otherwise it will turn into compile time error


I feel that the show() method in the last interface in the above interface inheritance hierarchy (namely ThirdInterface) will be invoked by the compiler

In interface you actually can't provide the implementation


If it is so then, the show() methods in the above two interfaces (namely, FirstInaterface, SecondInaterface) are useless and serve no purpose at all, since they are declared within interfaces, they themselves can never have their own implementations anywhere. When such kind of interface inheritance is useful?

Yes it is just declaration.. also it implicitly inherit from the parent interface. so if you write it or don't it doesn't make any difference

查看更多
来,给爷笑一个
3楼-- · 2019-07-23 17:54

The compiler will not choose the show() method of any interface : it will call the method of the class, that's all. The interfaces just tell that a show method must be implemented in the class.

Overriding a method in an inteface is useless (except maybe if you add annotations). Interface inheritance is only useful if you add methods.

查看更多
看我几分像从前
4楼-- · 2019-07-23 17:55

Only method implementations can override, not method specifications in an interface.

In such a scenario, which version of the show() methods from the above interfaces will be incorporated by the class Demo? How is the compiler able to resolve a specific version of the show() method dynamically at run time?

What "versions"? The method signatures are identical, so there is really only one method specification. At runtime, there will be an implementation class, and only the method implementation in that class is what gets actually called.

If it is so then, the show() methods in the above two interfaces (namely, FirstInaterface, SecondInaterface) are useless and serve no purpose at all, since they are declared within interfaces, they themselves can never have their own implementations anywhere.

Um, what about classes that implement FirstInterface directly? If anything, what serves no (technical) purpose is "overriding" methods in child interfaces. If SecondInterface and ThirdInterface did not have a show() method, it would have exactly the same syntactical effect.

However, there can be a valid reason to override methods in a child interface: to provide a different comment explaining semantic changes in the method contract. Examples for this can be seen in the collections framework of the Java API: Set.add() has a different contract than Collection.add(), since it adds the restriction that duplicate elements should not be added.

查看更多
Luminary・发光体
5楼-- · 2019-07-23 18:02

The only use I can think of in overriding superinterface methods in subinterfaces is documentation. This way, you see at one glance which methods this interface requires, and do not have to check in superinterfaces to find additional methods. It also might make sense if you want to clarify the use of method for this subinterface in contrast to the use in the superinterface or in other subinterfaces.

For the compiler there is no difference at all if you override those methods or not.

Of course, if you would use AOP languages as AspectJ, you could inject an implementated method into an interface, and this question might become relevant. In those cases the compiler would take the first implementation it comes accross in the interface hierarchy starting with the interface your class implements.

查看更多
乱世女痞
6楼-- · 2019-07-23 18:06

the show() method of Demo will be invoke because when you write Demo demo=new Demo(); then at the run time JVM run method from class.... interface are just contract .... they can't give implementation
if u do not override show(); and someConcrete() then it will show an compile time error...

When such kind of interface inheritance is useful?   

these type of implemention are use full because of reuse ability that means there is some behavior in fist Interface , some in 2nd something in 3rd ... and if u want a class which have all these functionality and some extra then u have 2 way..
1. make another interface having all those functionality .Or
2. make a interface and inharite it through those interface which have some functionality and add some extra in your interface..

查看更多
登录 后发表回答