0

If we call an overridden method of a subclass without using a reference variable of a super class, would it be run time polymorphism ?

  • 1
    Polymorphism is, by definition, a run time concept. – Sotirios Delimanolis Jan 07 '14 at 15:24
  • Yes. Please see the below for a complete explanation. http://stackoverflow.com/questions/8355912/overloading-is-compile-time-polymorphism-really – Kabron Jan 07 '14 at 15:27
  • 1
    @SotiriosDelimanolis In the Java world, yes, but not generally. What we Java folks call polymorphism is actually just one kind of polymorphism. – yshavit Jan 07 '14 at 15:31

1 Answers1

0

Yes, it would. Java objects "decide" which version of a method to call based on the type of the value at runtime, not the value of the variable at compile time.

Consider the following classes:

public class A {
    public String foo() {
        return "A";
    }
}

public class B extends A {
    public String foo() {
        return "B";
    }
}

All of the following invocations to foo() will return "B":

// compile-time type is A, runtime type is B
A a=new B();
a.foo();

// compile-time type is B, runtime type is B
B b=new B();
b.foo();

// compile-time type is B, runtime type is B
new B().foo();
sigpwned
  • 6,957
  • 5
  • 28
  • 48