June 2018
Beginner
722 pages
18h 47m
English
Assigning a parent object reference to the variable of the child class type is called narrowing reference conversion or downcasting. It is possible only after widening reference conversion has been applied first.
Here is a code example that demonstrates the case:
class SomeBaseClass{ void someMethod(){ ... }} class SomeClass extends SomeBaseClass{ void someOtherMethod(){ ... }}SomeBaseClass someBaseClass = new SomeBaseClass();someBaseClass = new SomeClass();someBaseClass.someMethod(); //works just fine//someBaseClass.someOtherMethod(); //compilation error((SomeClass)someBaseClass).someOtherMethod(); //works just fine//The following methods are available as they come from Object:int h = someBaseClass.hashCode();Object o = someBaseClass.clone(); ...Read now
Unlock full access