DemiSheep
DemiSheep

Reputation: 698

Inheritance of overloaded methods in Java and C++ differ in what fundamental way?

I am studying for my final exam in my comparative languages course and the professor gave us an old exam to get a feel for the real deal. One question I don't understand is this:

"The inheritance of overloaded methods in Java and C++ differ in a fundamental way. Clearly show the difference in the two approaches taken and argue why Java's design decision is preferable."

The only idea I have about this question is a memory I have about Java operator overloading and how you can't do that, but in C++ you can.

Upvotes: 1

Views: 1327

Answers (3)

Adrian Shum
Adrian Shum

Reputation: 40036

seems that most answers here talk about inheritance and method "Overriding" in C++/Java.

However, there is a keyword in the question makes me think that he is in fact asking for another thing: "The inheritance of overloaded methods"

There is one fundamental difference if I remember correctly, about Inheritance + Method overloading:

In C++, if you overloaded a method in child class which is declared in parent class, it will cause method shadowing. This may cause some surprisings in your design if you are not aware of it. If I remember correctly, if parent class have 2 overloading methods, and in child class, you override one of it, it will still cause shadowing.

However everything is fine in Java.

If this is really what is asking, then honestly Java's way is preferrable, because method overloading shouldn't interfere the method inheritance and overriding. And such shadowing causes many design problem, like you will not be able to access parent class method directly in the overloading+inheritance+shadowing scenario.

Upvotes: 3

Bowie Owens
Bowie Owens

Reputation: 2976

I would ask the lecturer for an example inheritance hierarchy that demonstrates the "inheritance of overloaded methods." I would take it to mean something like the following in C++:

struct A1 { };
struct A2 { };

struct B1 {
    virtual void f(A1*) = 0;
};

struct B2 {
    virtual void f(A2*) = 0;
};

struct D : B1, B2 { };

void f(D& d)
{
    A1 a1;
    A2 a2;
    d.f(&a1);
    d.f(&a2);
}

And the following in Java (my Java is a little rough so I hope I have gotten it about right):

class M {
    class A1 { };
    class A2 { };

    interface B1 {
        void f(A1 a1);
    };
    interface B2 {
        void f(A2 a2);
    };

    abstract class D implements B1, B2 {
    }; 

    void f(D d)
    {
        A1 a1 = new A1();
        A2 a2 = new A2();
        d.f(a1);
        d.f(a2);
    }
};

Note that the difference here is that the C++ code will not compile because f() is ambiguous. Whereas the Java code will compile. If this is what they are talking about, you could argue that Java is doing what the programmer would expect whereas C++ is likely to surprise the programmer. One important thing I learned at Uni is that passing Uni courses in programming sometimes means answering questions the way the assessor expects you to first and correctly second.

Upvotes: 3

iammilind
iammilind

Reputation: 69988

The inheritance of overloaded methods in Java and C++ differ in a fundamental way.

  1. In Java, when base class method is overridden by derived class, they both are related in polymorphic way by default. In C++ you have to make base method as virtual
  2. In Java you can stop the method overriding by making it final. In current C++, that is not available

argue why Java's design decision is preferable.

That's controversial. Java enforces single way of design, which helps programmers understand the code better/faster. Also reduces confusion and exploitation of language features.

C++ supports multi paradigm and pay per use terminology, which provides flexibility, but also making it an expert oriented language.

Upvotes: -1

Related Questions