简体   繁体   中英

OOP - Inheritance

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?

The answer to your question can be answered by stating it like this Is a cat a dog?

I'll let you answer this one.

No it can not and no it should not (at least not when talking about proper OOP). If a cat really needs a dog (for whatever sick reason) you should inject the dog in the cat. If you think you need it you probably have the wrong inheritance / abstraction.

class Animal
{
    public annoy();
}

class Cat extends Animal
{
    private $dog;

    public function __construct(Dog $dog)
    {
        $this->dog = $dog;
    }

    public function annoyDog()
    {
        $this->dog->annoy();
    }
}

class Dog extends Animal
{
}

$dog = new Dog();
$cat = new Cat($dog);
$cat->annoyDog();

Above is an example of injection. Note that as Tony Hopkinson stated you are calling the methods on the instance of dog in this case. You could call methods on the class if they are public static , but since it is tagged OOP you really shouldn't. PS example is in PHP.

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.

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?

in javascript , yes , i'll use coffeescript because it is easier to write :

class Animal
   bark:-> "i'm an animal"

class Dog extends Animal
  eatBone : -> "i'm eating a bone"

class Cat extends Animal

cat = new Cat

alert Dog::eatBone.call cat

Does it make sense ? probably not, but in javascript methods of a prototype are not bound to an object(unless you force it with closures). Now one could argue javascript doesnt have classes like java , but are C++ classes like Java ones ? python classes like Java ones ? who decides what's a real class or not ?

My point is the question is irrelevant outside of a language context.

Here cat and dog are two distinct classes and there is no direct relation between them.If we wish we can add few codes to link these classes. Until then the members in one cannot be accessed from the other.

如果猫不是从狗继承而来

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.

In java context [I have come across OOPs via Java]

No you cannot.

The methods that can be called by an object is determined by the reference variable.

If you use Dog/Cat reference variable it can only access methods in Dog/Cat class(including inherited and overridden ones)

If you use Animal(ieParent Class) as a reference variable and have it refer to a Cat/Dog class object. You can only call the inherited/overridden methods of subclass using that reference variable.This is decided at compile time.[if the fn being called is identified by the reference variable or no]

The Dog class object is unaware of exclusive Cat class functions[other than inherited and overridden ones] so it cant call the methods.

As pointed by others, If u have a case as such u might not be looking for inheritance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM