Talk:Method overriding

message: 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)
thread_title:  Major Contents

message: 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)
thread_title:  

message: 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)
thread_title:  Merge of CallSuper 

message: === 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)
thread_title:  C++ example 

message: "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)
thread_title:  Description 

message: 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 g++ -Wall wiki.cpp -o wiki and then runs with ./wiki in Linux. --Nigelj (talk) 18:14, 30 September 2010 (UTC)
thread_title:  Example code 

message: 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 b.foo(); prints “base” and d.foo(); prints “derived”.
However, at a quick glance, I misread the sentence as: c.foo(); prints “base” and d.foo(); prints “derived”.  Of course, c.foo(); 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 c, the sentence may get even more confusing.)
sheep0x (talk) 18:20, 30 May 2016 (UTC)
replies: text: The text talks about objects, not the type of the variables that are used to invoke the functions. The last part covers both c.foo() and d.foo() because both c and d refers to "an object of the subclass". 90.230.55.237 (talk) 11:42, 4 December 2021 (UTC)
thread_title:  Misleading claim? 

message: 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.
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>
struct Base
{
    Base(int k) { key = k; }
    void set(int k) { key=k; }
    //void set(int k1, int k2) { key=k1+k2; }
    int key;
};
struct Derived: public Base
{
    Derived(int k1, int k2) : Base(k1) { key=(k1<k2)?k1:k2; }
    void set(int k1, int k2) { key=(k1<k2)?k1:k2; }
};
int main()
{
    Base k(5);
    Derived d1(3,5);
    //Derived d2(5);
    k.set(6);
    d1.set(4,6);
    d1.set(6); // override cannot find base class function
}
Mr.user1 (talk) 19:05, 16 August 2017 (UTC)
replies: text: 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)
thread_title:  Criteria for method overriding 

message: 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)
thread_title:  External links modified (January 2018)