Java中的接口继承何时有用? [英] When interface inheritance in Java is useful?

查看:189
本文介绍了Java中的接口继承何时有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看一下下面的Java代码.

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


上面的Java代码显示了接口的多级继承,其中 ThirdInterface 上面有两个接口. show()方法首先在接口 SecondInaterface 中被覆盖,然后在接口 ThirdInterface 中再次被覆盖,该接口最终被类 Demo 继承.强>.


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.

在这种情况下,Demo类将合并上述接口中的show()方法的哪个版本?编译器如何在运行时动态解析show()方法的特定版本?

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?

我觉得上述接口继承层次结构(即ThirdInterface)中最后一个接口中的show()方法将由编译器调用.如果是这样的话,上述两个接口(即FirstInaterface,SecondInaterface)中的show()方法是无用的,根本没有任何作用,因为它们是在接口中声明的,因此它们自己从不在任何地方都有自己的实现.这种接口继承什么时候有用?

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?

推荐答案

只有方法实现可以覆盖,而不能覆盖接口中的方法规范.

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

在这种情况下,Demo类将合并上述接口中的show()方法的哪个版本?编译器如何在运行时动态解析show()方法的特定版本?

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.

如果是这样,则上述两个接口(即FirstInaterface,SecondInaterface)中的show()方法是无用的,根本没有任何作用,因为它们是在接口中声明的,所以它们本身永远不可能拥有自己的实现任何地方.

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.

嗯,直接实现FirstInterface的类呢?如果有的话,没有任何(技术上的)目的是在子接口中覆盖"方法.如果SecondInterfaceThirdInterface没有show()方法,它将具有完全相同的语法效果.

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.

但是,在子接口中重写方法可能有充分的理由:提供不同的注释来说明方法协定中的语义变化.在Java API的集合框架中可以看到这样的示例:Set.add()Collection.add()具有不同的协定,因为它增加了限制,即不应添加重复的元素.

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.

这篇关于Java中的接口继承何时有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆