I recently started learning Java. I got to the point of upcasts and downcasts and this is what I encountered. I understand how type conversion works, but this moment keeps me awake. If you create class fields outside the constructor and assign values to them, then during the upcast, as per the rules, you cannot get the field of the child class, but if these fields are created directly in the constructor, then during the upcast I can safely refer to the fields of the child class.
Parent class:
public class Parent {
String name = "Parent name";
int age = 30;
Parent() {
}
public void hello(){
System.out.println("Parent say hello");
}
}
Child class with creation in constructor:
public class Child extends Parent {
public Child() {
this.name = "Child name";
this.age = 10;
}
public void hello() {
System.out.println("CHild say hello");
}
}
Child class with creation out constructor:
public class Child extends Parent {
public String name;
public int age;
public Child() {
this.name = "Child name";
this.age = 10;
}
public void hello() {
System.out.println("CHild say hello");
}
}
Main code testing first case:
public class Main {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name);
}
}
Output: Child name
Main code testing second case:
(The Main code is the same.)
Output: Parent name
I looked at a ton of video tutorials and documentation sites, but no one considered my case.
In the first test case, class
Parentis instantiated and the string Parent name is assigned to member variablenamein classParent. Then theChildclass is instantiated and its constructor assigns a new value to membernamein classParent, namely Child name. Now in methodmain, of classMain,p.namewill return the current value of variablenamein classParentwhich is Child name.In the second test case, you are creating another
namevariable in classChild. This is a different variable tonamein classParent. Hence when you assign a value tonamein the constructor of classChild, you are assigning it to the variable in classChildand not to the variable in classParent. Hencenamein classParentis still Parent name. So, in methodmain, of classMain,p.namerefers tonamein classParent(even though you assignpto an instance of classChild) and therefore the second test case prints Parent name.Here
pis aParentand not aChildand therefore has no access to member variables in classChild. This is known as variable hiding. There are many questions (and answers) regarding variable hiding. Just enter the following in the search box at the top of this Web page: