Can a superclass variable access an overridden method of a subclass. For ex:
class A {
void callMe() {
System.out.println("Inside A");
}
}
class B extends A {
void callMe() {
System.out.println("Inside B");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A();
B b = new B(); // Object of type B
A r; // Obtain a reference of type A
r = a; // Refers to A object
r.callMe(); // Calls A's version of callMe()
r = b; // Refers to B object
r.callMe(); // calls B's version of callMe() and my question is on this
}
}
I learned earlier that a superclass variable that is referencing a subclass object can access only those parts of an object that are defined by the superclass. Then how can the second r.callMe()
call B
's version of callMe()
? It should only call A
's version of callMe()
again.
Runtime Polymorphism in Java is achieved by Method overriding in which a child class overrides a method in its parent.
Dynamic dispatch method is important because it is a process through which Java implements runtime polymorphism.
With inheritance / polymorphism we don't know the concrete runtime type of an expression, thus which method to be called must be "dynamically" determined during runtime. Without dynamic dispatch, virtual methods wouldn't make sense, which is central for abstraction and encapsulation.
Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime. This is how java implements runtime polymorphism. When an overridden method is called by a reference, java determines which version of that method to execute based on the type of object it refer to.
...a superclass variable that is referencing a subclass object can access only those parts of object that are defined by superclass
That's not entirely correct. In the end, the runtime calls the actual type of the object, regardless of the reference type. So r.callme()
will actually call callme()
defined in B
, because r
is a B
object.
new B(); // <-- The object in memory is of type B and its type never
// changes.
A a = new B(); // <-- The object in memory is of type B; the reference type
// is A. But that effectively does only matter at
// compile-time, I believe.
In the example above, B
is called the object type, and A
is called the reference type.
See Java Language Specification § 15.12.4.4:
Let
X
be the compile-time type of the target reference of the method invocation.[...]
If the invocation mode is
virtual
, and the declaration inS
overridesX.m
(§8.4.8.1), then the method declared inS
is the method to be invoked, and the procedure terminates.
Let me make a rough guess at what they mean by "access only those parts [...] defined by superclass":
class A {
void doSomething() { }
}
class B extends A {
void doAnotherThing() { }
}
A a = new B();
a.doAnotherThing(); // Not valid, because doAnotherthing()
// is defined in class B.
In order to call doAnotherThing()
, a type cast must be used:
((B) a).doAnotherThing(); // Valid
In your question
r=b;
now r catch "new B()" object.When u call r.callme() then run callme method in B class. Because r has B object.
Any program will throw a compile time error since reference type of super class doesn't have a method by the name of sub class.
As the example
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
public void bark() {
System.out.println("Dogs can bark");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
b.bark();
}
}
Output
TestDog.java:26: error: cannot find symbol
b.bark();
^
symbol: method bark()
location: variable b of type Animal
1 error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With