OOP - Inheritance

2019-04-15 15:47发布

If I have a class, class Animal.

And I have a class Dog and a class Cat both inheriting Animal.

Cat can call methods inside Animal. But can Cat call methods of Dog?

标签: oop class
8条回答
2楼-- · 2019-04-15 16:46

The methods defined in a superclass are accesible to any subclass. But "sister" classes cannot access the (private) methods of one another.

So if your classes Cat and Dog inherit from Animal, then both Cat and Dog objects can call methods from Animal, but they cannot (must not) access the methods of each other.

查看更多
闹够了就滚
3楼-- · 2019-04-15 16:46

I'm afraid that... no. Cat cannon invoke Dog methods.

Inheritance allows to "view" parents (public/protected if you are using Java) methods inside child classes, but not inside siblings classes.

Obviosly if Cat has an instance of Dog as property (I don't know why it should have...) it can call public methods of that Dog instance.

查看更多
登录 后发表回答