Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a superclass implement interface methods which are supposed to be implemented by the child?

Tags:

java

interface

Here is a parent class

public class Parent {
    public void f() {

    }
}

Here is a simple interface

public interface If {
    public void f();
}

Here is a child class

public class Child extends Parent implements If{}

My question is: Although it is Child which claims to implement the interface If, the interface method is implemented in Parent. Why is this allowed?

like image 838
Amit Avatar asked Dec 19 '22 23:12

Amit


2 Answers

Why is this allowed?

Because Child satisfies the contract. The contract Child makes, by implementing If, is that it will provide a public f function that accepts no arguments and has no return value. It does that. How it does it, in this case by inheriting it from a superclass, is irrelevant. The contract is satisfied, which is all that matters.

the interface method is implemented in Parent

Not really, it just happens to have the f method. If you did If inst = new Parent() you'd get get a compilation error, because Parent doesn't say it implements If (there's no implements If on it); implementing If isn't part of its contract. Child, on the other hand, does provide that contract, and so If inst = new Child() works just fine.

FWIW, you probably wouldn't do this on purpose, although there's arguably nothing wrong with it. But it could come up if it just happened that the superclass you want to use has a perfect fit for one of the methods in the interface you want to implement.

When that happens, you have three choices:

  1. Do nothing and let Parent's method directly implement the interface method.

  2. Override Parent's method even though you're not going to do anything differently, e.g.:

    @Override
    public void f() {
        super.f();
    }
    

    ...which really doesn't accomplish anything unless you have code you want to run prior to or after calling Parent's f.

  3. Override f and implement it yourself, ignoring Parent's version. But that only makes sense if Parent's f does something completely different from the interface method's f — in which case, you shouldn't be subclassing Parent in the first place, because if you reimplement f in terms of the interface's behavior, your f won't satisfy the Parent#f contract anymore (in terms of its behavior) and you'll break the "is a" rule for subclassing.

So really, option 1 is perfectly reasonable if it happens organically. You just probably wouldn't set it up on purpose.

like image 134
T.J. Crowder Avatar answered Dec 26 '22 00:12

T.J. Crowder


It is completely legal because when you implement the method, it works for both.

From class perspective and from interface perspective. A single implementation would serve the both purpose and there is no point of ambiguity it.

There is no way to restrict a Class or Interface to having same method signature, unless you have a relationship with them, but in this case there will be no problems.

like image 21
Suresh Atta Avatar answered Dec 25 '22 23:12

Suresh Atta