Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Quick question, I'm learning about interfaces and inheritance.

This is not actual code, just an example. Let's say I have the abstract class Animal. There's some inheritance with groups like horses, and canines. There's also an interface "Pets". It's gonna be used on different subclasses of Animal. The subclass of canine "Dog" implements the interface "Pets". Therefore all subclasses of "Dog" also implement the interface "Pet" without having to individually implement "Pets" on each subclass of "Dog", right?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you have:

abstract class StaffMember implements MyInterface

where

interface MyInterface {
    void myMethod();
} 

then all of the classes extending StaffMember will inherit the type MyInterface, and you will be able to refer to them by this base type in other parts of the code where a MyInterface instance is expected as an operand/argument, for example:

void otherMethod(MyInterface param) { //... }

The actual implementation of the interface MyInterface can take place either in the abstract class, or in any of the classes extending the abstract class. The important thing is simply, in this case, that myMethod() is specified somewhere in the inheritance hierarchy, so that the JVM can find a definition of it to invoke.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...