Talk:Method overriding

Latest comment: 2 years ago by 90.230.55.237 in topic Criteria for method overriding

Major Contents

edit

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)Reply

edit

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)Reply

Merge of CallSuper

edit

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)Reply


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)Reply

Terrible Example

edit

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)Reply
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.

C++ example

edit

Description

edit

"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)Reply

Example code

edit

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)Reply

#include <iostream>
using namespace std;

class Rectangle
{
public: 
    Rectangle(double l, double w);
    void print();
 
private:
    double length;
    double width;
};

Rectangle::Rectangle(double l, double w)
{
    length = l;
    width = w;
}

void Rectangle::print()  // print() method of base class
{
    cout << "Length = " << length << "; Width = " << width;
}
 
class Box : public Rectangle
{
public:
    Box(double l, double w, double h);
    void print();
 
private:
    double height;
};

Box::Box(double l, double w, double h)
    :Rectangle(l, w)
{
    height = h;
}
 
void Box::print()  // print() method of derived class
{
   Rectangle::print();  // Invoke parent print() method.
   cout << "; Height= " << height;
}

int main ()
{
    Rectangle myRectangle(5.0, 3.0);
    myRectangle.print();
    cout << '\n';
    // outputs:
    // Length = 5.0; Width = 3.0
     
    Box myBox(6.0, 5.0, 4.0);
    myBox.print();
    cout << '\n';
    // outputs:
    // Length = 6.0; Width = 5.0; Height = 4.0
}

Misleading claim?

edit

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)Reply

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)Reply

Criteria for method overriding

edit

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)Reply

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)Reply
edit

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:

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

This message was posted before February 2018. After February 2018, "External links modified" talk page sections are no longer generated or monitored by InternetArchiveBot. No special action is required regarding these talk page notices, other than regular verification using the archive tool instructions below. Editors have permission to delete these "External links modified" talk page sections if they want to de-clutter talk pages, but see the RfC before doing mass systematic removals. This message is updated dynamically through the template {{source check}} (last update: 5 June 2024).

  • If you have discovered URLs which were erroneously considered dead by the bot, you can report them with this tool.
  • If you found an error with any archives or the URLs themselves, you can fix them with this tool.

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