Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface inheritance - changing method parameters

I have difficulties understanding the java way of interpreting interface inheritance, example:

public interface Model {
    Model getModel();
    void setModel(Model model);
}

public class BaseModel implements Model {
    @Override
    public BaseModel getModel() { return  null; } // works

    @Override
    public void setModel(BaseModel model) {} // compilation error, it wants Model instead of BaseModel
}

Could anyone explain why the first method works, and the second doesn't?

like image 391
n3x7 Avatar asked Dec 09 '14 08:12

n3x7


People also ask

Can methods in interface have parameters?

Yes, you can have overloaded methods (methods with the same name different parameters) in an interface. You can implement this interface and achieve method overloading through its methods.

Are interface methods inherited?

Static methods in interfaces are never inherited.

Do you have to override interface methods?

If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface. In short, you can access the default methods of an interface using the objects of the implementing classes.

Can we override interface methods in Java?

No. Interface methods can be implemented. To overload/override a method, that method must be defined first.

How is the concept of inheritance used in interface?

In this article, we will understand how the concept of inheritance is used in the interface. An interface is a set of specifications or statements that define what a class can do without specifying how the class will do it. The interface is always abstract. A concrete class must implement all the abstract methods specified in the interface.

Can interfaces be treated as parameters?

This example illustrates the important point that interfaces are lightweight contract entities, and client code must provide the contents; in other words, the method code. It's perhaps a little surprising that C# interfaces can also be treated as parameters, as shown in Listing 3.

Can static methods be overridden in interface inheritance?

In the interface inheritance, the static methods are not changed throughout the execution and they are not inherited. Hence, they cannot be overridden.

What is an interface in Java?

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body). In this article, we will understand how the concept of inheritance is used in the interface.


1 Answers

In order to understand this, you should ask yourself "can I substitude BaseModel for any usage of the Model interface"?

When you specialize the return value, this works fine. Even if getModel() returns a BaseModel, it can always be assigned to a Model variable.

Model model = myModel.getModel();

The other way round is not true, however:

SomeOtherModel other = ...;
myModel.setModel(other); // no problem
myBaseModel.setModel(other); // other is not a BaseModel!

if setModel were to accept a BaseModel parameter you'd break its ability to be set with other implementations of Model. Hence, this is not allowed.

like image 79
Mureinik Avatar answered Sep 19 '22 06:09

Mureinik