5

I have 2 classes: Triangle and RightAngledTr. RightAngledTr inherits from Triangle. The code is as follows:

The Triangle class:

class Triangle {
    public void draw() { 
        System.out.println(“Base::draw\n“);
    }

    public void computeCentroid(Triangle t) {
        System.out.println Base::centroid\n“);
    }
}

The RightAngledTr class:

class RightAngledTr extends Triangle {
    public void draw() { 
        System.out.println(“RightAngle::draw\n“);
    }

    public void computeCentroid(RightAngled t) {
        System.out.println(RtAngle::centroid\n“);
    }
}

In my Driver program I have the following lines of code:

Triangle tr= new RightAngledTr ();
RightAngledTr rtr= new RightAngledTr ();
tr.computeCentroid(rtr);
tr.draw();
rtr.computeCentroid(tr);

The output I expected was:

Base::centroid
Base::draw
Base::centroid

However, the output I got was:

Base::centroid
RightAngle::draw
Base::centroid

My question is, why does tr.computeCentroid(rtr) call the Triangle class' method when tr.draw() calls the RightAngleTr class' method?

It doesn't make sense. Liskov substitution principle doesn't seem to apply.

1

3 Answers 3

3

The signatures of the computeCentroid() method are different. You are therefore not overriding, but overloading.

Sign up to request clarification or add additional context in comments.

4 Comments

He is talking about draw() method i think
And this is why you always use @Override.
But tr is an object of type Triangle, so why is the draw() method of the Triangle class not called when I do tr.draw() ?
Have a look at my answer.
1

That's how java works. The draw () method is overridden.

Have a look at oracle documentation article for understanding the concepts.

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

Triangle (parent) holds the reference of RightAngledTr (child) and draw() method is invoked on child objects. It's called overriding feature.

Childs are type of parents. Parent holding child instance invokes child's overridden method. If method is not present in child, parent method will be invoked.

Overriding is redefining base class (parent) behaviour with same signature. If you change the signature of base class method in child class, it's called overloading. You have done the same for other method : computeCentroid().

But do not expect the same for same variables declared in both parent and child.

Variables are not overridden. Only methods are overridden.

Comments

1

You are calling the method draw on the specialised object. Even when you wrap it inside the base class you are calling the overridden draw method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.