'What's the difference between downcasting and creating child object in java?
I write this one:
parent obj1=new Child();
Child obj2 =(Child)obj1; //downcasting
like this one:
Child obj2 = new Child(); //creating child object
Solution 1:[1]
There difference is that in one case you have two references to one object. In the other, you have two references pointing to two different objects. Consider how it works with the following:
parent.kill();
if (child.isDead()) {
System.out.println("He's dead");
} else {
System.out.println("He lives!");
}
If the references point to the same object then we will print out "He's dead". If they are pointing to two different objects then we will print out "He lives!".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vsfDawg |
