I've started java recently and this is what is bothering me.
consider this code.
class Superclass{
void print(){
System.out.print("hi");
}
}
class Subclass extends Superclass{
void Print(){
System.out.print("HI");
}
}
class RandomTestDrive{
public static void main(String[] args){
Superclass a = new Subclass();
a.print();
}
}
so my question here is, why would anyone declare their reference variable as superclass and object as subclass.
since you cannot use the methods of subclass
from a reference variable variable that is of type superclass
, why do it in the first place?
why not just make the reference variable of the same type as the object
?