Talk:Method overriding

Major Contents
I would like to add more contents to this page by providing specific examples in C++ and Java. And since the article barely has any reference, I would like to add more references to support it and add more categories and external links as well.I have to do this as a project for one of my classes and sugestions are welcome.

LorryB10 (talk) 01:51, 21 April 2010 (UTC)

These examples are great, but I wonder if it might not be more helpful to the layreader if they were written in some sort of pseudocode (perhaps Wikicode?) instead of Python. Any thoughts?


 * Personally, I think Python is fine. The examples are highly readable and unlikely to confuse anyone unfamiliar with the language, as long as they are familiar with the concepts assumed by the article (e.g. what interface inheritance is).


 * My main concern is one that I've had for a while, and which applies to a large number of articles other than this one, which is the choice of subject matter of the examples. I recall when I was learning OOP I saw similar examples to this and my gut reaction was "so what?"  The examples were highly abstract, and I didn't see how it would apply to a real program.  This is very common though -- OOP examples always seem to model real-world objects.  I guess it may have something to do with the paradigm's roots in simulation.  I don't think the examples are as good, though, as examples of code that somebody might really use would be.  I find talking about GUI code with examples of windows and specialised types of window (e.g. buttons) more useful.


 * I'm not going to go and rewrite a load of examples without consensus, though. JulesH 19:49, 12 May 2006 (UTC)

Merge of CallSuper
Not sure if this is the best place to put it, but the current article CallSuper really shouldn't be an article of its own. I'd say it belongs here because it discusses explicity calling overridden methods. It also seems to imply that doing so is a design flaw, although I really have no idea why.

Any comments? JulesH 19:49, 12 May 2006 (UTC)

In response to JulesH, I don't think CallSuper is referring to calling overridden methods in general. It's referring to calling the base class' version of a method from a subclass that has overridden the method. Something like this (Java):

public void doSomething { super.doSomething; doOtherThings; }

This could be considered bad design because of the Coupling between the parent and base class. This could cause unexpected results if the base class' doSomething method were changed in the future. If this happens a lot in the subclasses, perhaps the methods need to be refactored to more elegantly handle the problem (something like Template method pattern).

I don't think this should be merged, since CallSuper is referring to a particular recurring problem in a specific case of method overriding, and is linked from the Anti-pattern page as well. Method overriding (programming) is not about this, but is a general description of the object-oriented paradigm.

DrSkrud 12:30, 8 June 2006 (UTC)

Terrible Example
The relationship between the derived class and the base class is erroneous. In the example, a Baby supports almost none of the actions of a Person, therefore by this definition a Baby is NOT a Person. Throwing 'not supported' exceptions in virtual methods is a terrible idea. Don't inherit from a class if you are not ONE_OF that class. A virtual method might throw a 'not implemented yet' exception, but in this example Baby simply breaks the IS_A relationship with Person. -FWIW


 * Where is this code coming from? Based on your description above, it appears Baby is an Abstract Person, which can't be instatiatied. 203.20.153.128 01:10, 3 July 2006 (UTC)


 * I was referring to the Person/Baby example given in the page. In that example, Baby is a non-abstract class deriving from Person. My opinion is that given the semantics of the example, a Baby is not a person (regular or abstract), therefore it should not derive from it. A better example would be, for instance, Athlete (instead of Baby). -FWIW.

Description
"In C++, the name of the parent or base class is used followed by the scope resolution operator to override functions." IMO, this just describes how you can use the method's base class implementation within the overriding method and not how to override a method in general. Instead, overriding is accomplished by declaring the base class method as virtual and implementing the method in the subclass as for any other non-overridden function, or am I wrong?


 * An older version of the text was correct but that changed when someone tried to "improve" it. I agree with this person that the text needed improvements but correctness is more important so I went ahead and restored the old version. 90.230.55.237 (talk) 13:58, 1 February 2018 (UTC)

Example code
I have just tested the C++ example, to make sure the syntax was correct, especially since I just altered it a bit. Here is the full code that works, if anyone's interested, for reference. This compiles with  and then runs with   in Linux. --Nigelj (talk) 18:14, 30 September 2010 (UTC)

Misleading claim?
Let's say we have some code: class Base { void foo { print("base"); } } class Deriv inherits Base { void foo { print("derived"); } } Base b = new Base; Base c = new Deriv; Deriv d = new Deriv;

In the intro section, it says “If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed”.

If I'm not mistaken, this sentence means that  prints “base” and   prints “derived”.

However, at a quick glance, I misread the sentence as:  prints “base” and   prints “derived”. Of course,  should print “derived” instead. I wonder if it's just me, or does anybody feel the same way? (Not sure how to explain this though. If we add some explanation for, the sentence may get even more confusing.) sheep0x (talk) 18:20, 30 May 2016 (UTC)


 * The text talks about objects, not the type of the variables that are used to invoke the functions. The last part covers both  and   because both c and d refers to "an object of the subclass". 90.230.55.237 (talk) 11:42, 4 December 2021 (UTC)

Criteria for method overriding
The first paragraph states the following.

"The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class."

It may be true for some languages, however, in C++ this is not the case. Same arguments and return type are not necessary. The overriding mechanism is limited to checking if the name matches. See the code below, or check out http://coliru.stacked-crooked.com/a/91c416ba683ca71d It would be nice to see what the criteria are for the different languages.

Mr.user1 (talk) 19:05, 16 August 2017 (UTC)


 * This is not an example of overriding. I think it might be refered to as "shadowing". 90.230.55.237 (talk) 11:47, 4 December 2021 (UTC)

External links modified (January 2018)
Hello fellow Wikipedians,

I have just modified one external link on Method overriding. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
 * Added archive https://web.archive.org/web/20070117210712/http://www.beginner-java-tutorial.com/java-method-overriding.html to http://beginner-java-tutorial.com/java-method-overriding.html

When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.

Cheers.— InternetArchiveBot  (Report bug) 19:13, 26 January 2018 (UTC)