Super thing = new Sub();
Sub thing = new Sub();
What is the difference in terms of accessing thing and what I can do with it etc?
Super thing = new Sub();
Sub thing = new Sub();
What is the difference in terms of accessing thing and what I can do with it etc?
The difference is that the variabele Super thing
can be reassigned to any other subclass that implements Super
, whereas Sub thing
of course can only be assigned to another Sub
class.
That said, assigning new Sub()
to Super thing
uses polymorphism and overriding, of which you can find a good tutorial here and here. You can only call the methods defined in the class Super
. If these methods are overridden (read: redefined) in one of the subclasses, polymorphism will make sure the overridden method is called, instead of the method in the class Super
.