Upcasting
Once you have upcast an object reference, you can access only the fields and methods declared by the base class.
However, if you invoke a method on e
that is defined in Employee but overridden in Manager, the Manager version is executed.
For example:
public class A {
public void print() {
System.out.println("Hello from class A");
}
}
public class B extends A {
public void print() {
System.out.println("Hello from class B");
}
}
// ...
A obj = new B();
obj.print();
In the case, the output is "Hello from class B".
From within a subclass, you can explicitly invoke a base class’s version of a method by using the
super.
prefix on the method call.
No comments:
Post a Comment