June 2018
Beginner
722 pages
18h 47m
English
For example, if a class, SomeClass, extends SomeBaseClass, the following declaration and initialization are possible:
SomeBaseClass someBaseClass = new SomeBaseClass();someBaseClass = new SomeClass();
And, since every class extends the java.lang.Object class by default, the following declarations and initializations are possible too:
Object someBaseClass = new SomeBaseClass();someBaseClass = new SomeClass(); //line 2
In line 2, we assigned a subclass instance reference to a variable of a superclass type. Methods present in a subclass but not in the superclass are not accessible via the reference of the superclass type. The assignment in line 2 is called a widening of the reference because it becomes less specialized.
Read now
Unlock full access