Is the C# “explicit implementation” of the interface present in Java?

前端 未结 4 1704
忘掉有多难
忘掉有多难 2020-12-17 14:23

In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently

4条回答
  •  误落风尘
    2020-12-17 15:07

    No, there's nothing like C#'s explicit interface implementation in Java.

    On the plus side, Java has covariant return types, so if you want to provide a more strongly typed implementation than the interface specifies, that's okay. For instance, this is fine:

    interface Foo
    {
        Object getBar();
    }
    
    public class Test implements Foo
    {
        @Override
        public String getBar()
        {
            return "hi";
        }
    }
    

    C# wouldn't allow that - and one of the ways around it is typically to implement the interface explicitly and then have a more specific public method (usually called by the interface implementation).

提交回复
热议问题