开发者

Should a parent function call be calling the parent's parent if the parent has no such method?

For academic purposes, I am wondering if a parent method call should lead to a parent's parent method call in case when the parent has no开发者_JS百科 such method?

For example (pseudo-code):

class A {
    function doSomething() {
    }
}

class B extends A {}

class C extends B {
    function doSomething() {
        parent::doSomething();
    }
}

i = new C();
i->doSomething();

Does parent calling in object oriented languages mean that if the parent is missing the method, its parent will have its method called instead?


In OOP this question is usually answered by the function doSomething's visibility.

If it was protected or public in class A, then yes, it will be called if it is not overridden in B

(I assume here also that the extends keyword is public inheritance. See http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/ for more information.)


Semantics of parent-child "extends"/single inheritance strongly imply that "wherever you could use the parent, you may also use the child".

That is, the features of the child should be a superset of the parent's.

Thus, most sane implementations of single-inheritance will make the default behavior of the child identical to the parent, because of the above principle.

Of course there may be other things at work, for example the language may support interfaces, design-by-contract, etc. -- in which case the parent method may be a stub or abstract uncallable method, or who-knows-what policy may be in effect.

note: The other answers's talk about public-vs-private are really specific to Java-like languages, and not (as the question's tags imply) general OOP. Unless you consider visibility to be part of general OOP (the python language might object).


If the scope of DoSomething as defined on A is Public, then DoSomething is also a part of B's Public intefrace as well. Therefore, the DoSomething method will also be a part of C's public interface. Since the original method as defined on A in inherited by derived classes, the parent method will be called by derived classes unless it is overridden.

TO a degree, the details of how all that works within certain scopes might be deteermined by specific languages, but general OO principles follow the concept above . . .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜