3

I wish someone would explain me how this decision was taken. I understand that, the overloaded version is chosen based on the declared type, but why, on the second call, the decision was taken based the runtime type?

 public class Test {
 public static void main(String[] args) {
 Parent polymorphicInstance = new Child();
 TestPolymorphicCall tp = new TestPolymorphicCall();
 tp.doSomething(polymorphicInstance);
 // outputs: Parent: doing something... 
 call(polymorphicInstance);
 // outputs: Child: I'm doing something too 
 }
 public static void call(Parent parent){
 parent.doSomething();
 }
 public static void call(Child child){
 child.doSomething();
 }
 }
 public class Parent {
 public void doSomething() {
 System.out.println("Parent: doing something...");
 }
 }
 public class Child extends Parent{
 @Override
 public void doSomething() {
 System.out.println("Child: I'm doing something too"); 
 }
 }
 public class TestPolymorphicCall {
 public void doSomething(Parent p) {
 System.out.println("Parent: doing something...");
 }
 public void doSomething(Child c) {
 System.out.println("Child: doing something...");
 }
 }

Thanks in advance!

asked Aug 2, 2013 at 17:31

1 Answer 1

4

Your Parent class reference is referring to Child class object:

Parent polymorphicInstance = new Child();

So, when you pass the reference in call method, the actual method invoked is the one with Parent parameter type only. But when you call the method doSomething(), on parent reference:

public static void call(Parent parent){
 parent.doSomething();
}

It will call doSomething() method, that you have overridden in Child class.


This is the classic case of polymorphism. Suppose you have a class Shape and a subclass Circle, which overrides the method calculateArea() defined in Shape class.

Shape circle = new Circle();
// This will invoke the method in SubClass.
System.out.println(circle.calculateArea());

When you override a super class method in subclass, then the actual method invoked is decided at runtime, based on what actual object your super class reference points to. This is called Dynamic Dispatch of method call.

answered Aug 2, 2013 at 17:36
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I kind of confuse myself with this tricky example, because the TestPolymorphicCall class methods isn't calling the argument's reference at all. But you're right, regardless the overloaded version of method called, which is chosen based on the declared type, when dealing with polymorphic types, the behavior called is the subtype one.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.