1

I notice that in Java, if you override a method in subclass, you can still call the overriden method declaced only in superclass. But in C++, it will be hiden.

The Java code:

class Fish {
    public void foo() {}
}

class Tuna extends Fish {
    public void foo(int i) {}
}

public class Test{
    public static void main(String[] args) {
        Tuna t = new Tuna();
        t.foo();
        t.foo(1);
    }
}

The C++ code:

class Fish {
public:
    void foo() {}
};

class Tuna : public Fish {
public:
    void foo(int i) {}
};
int main() {
    Tuna t;
    t.foo(1); // ok
    t.foo(); // error
    return 0;
}

I noticed some people present a few explanations in the Why does an overridden function in the derived class hide other overloads of the base class? . If it is true that

name hiding would prove to be a lesser evil

, why doJava choose a more evil way?

Why do Java and C++ behave different? Is it only becasue of the different name looking-up strategies between Java and C++?

Community
  • 1
  • 1
chenzhongpu
  • 6,193
  • 8
  • 41
  • 79
  • I don't know if anyone here can tell you why. Probably only James Gosling could. But I spotted the overloading right away. If you're an experienced Java programmer this just isn't a problem. – markspace Mar 24 '17 at 02:51
  • I suggest tremoving subjective and inflammatory terms like "evil" from a question to make it more objective. It sounds a bit like a ["rant in disguise"](http://stackoverflow.com/help/dont-ask) currently. – Erwin Bolwidt Mar 24 '17 at 03:35
  • 1
    Why can a tuna breathe water but an ocelot can't? Java and C++ aren't the same. Why should any particular feature match between them? – Lew Bloch Mar 24 '17 at 03:46
  • Different isn't the same. – Pete Becker Mar 24 '17 at 11:04

0 Answers0