Reputation: 1497
I am confused: I have taken the following quotes (with the titles of the sections in which they appear) from a learning resource, but the quotes seem to me to contradict each other.
"it is the type of the reference variable-not the type of the object that it refers to-that determines what members can be accessed"
"it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed"
Any clarification on this would be appreciated.
Upvotes: 4
Views: 2186
Reputation: 323
Suppose we have two classes.
class Vehicle{
public void drive(){
System.out.println("Vehicle is Moving");
}
}
class Car extends Vehicle{
public void drive(){
System.out.println("Car is Moving");
}
public void playMusic(){
System.out.println("Car is Playing Music");
}
}
"it is the type of the reference variable-not the type of the object that it refers to-that determines what members can be accessed"
It means if we have a code like
Vehicle vehicle = new Car();
Using vehicle
object, we can call drive()
, but not playMusic()
, Because type of vehicle
is Vehicle
.
"it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed"
It means if we have a code like
Vehicle vehicle = new Car();
vehicle.drive();
It will print "Car is Moving" not "Vehicle is Moving", becasue the object stored in vehicle
is an instance of Car
.
Upvotes: 3
Reputation: 93
Have a look at below program.
class SuperMem{
int i = 90;
int j = 100;
void show(){
System.out.println("parent show..");
System.out.println("Show inside Parent start");
System.out.println(i + " " + j);
System.out.println("Show inside Parent END");
}
}
class submem extends SuperMem{
int i = 10;
int j = 20;
void show(){
System.out.println("Child show ..");
System.out.println("Show inside Child start");
System.out.println(i + " " + j);
System.out.println("Show inside Child END");
}
}
public class SuperMemDemo {
public static void main(String[] args) {
SuperMem m = new SuperMem();
submem s = new submem();
m = s;
System.out.println("i " + m.i);
System.out.println("j " + m.j);
m.show();
}
}
Output:
i 90
j 100
Child show ..
Show inside Child start
10 20
Show inside Child END
Methods are resolved via dynamic dispatch methods i.e. at runtime. Analyse output and you will get meaning of above two statements from Complete reference Herbert Schildt
Upvotes: 1
Reputation: 13596
Superclass References and Subclass Objects
Assuming Child extends Parent, let's look at this:
Parent obj = new Child();
Now if we try to use obj
, we can only use behavior (methods) specified in the Parent class. We can't use any new methods from the Child class.
However, let's say both Parent and Child have a method public String whoAmI()
.
Parent:
return "Parent";
Child:
return "Child";
Now if we run this code:
Parent obj1 = new Child();
Parent obj2 = new Parent();
System.out.println(obj1.whoAmI());
System.out.println(obj2.whoAmI());
Output:
Child
Parent
So you can only access methods in the class you're referring to it by (Parent in the first snippet). But if you've overriden it in the class it is instantiated as (Child in the first snippet), and override a method in the parent in the child, then invoking the method present in the parent will call the method overriden in the child.
Upvotes: 0
Reputation: 393866
If classB extends ClassA and you have :
ClassA a = new ClassB ();
Using the a
variable, you can only access members defined in ClassA
(or super-classes of ClassA
or interfaces that ClassA
implements). You can't access a member defined in ClassB
that's not defined in ClassA
(unless you cast a
to ClassB
).
However, calling a method of ClassA
that's overridden in ClassB
will execute the ClassB
method.
Upvotes: 0