I've been recently reading a book on Java design patterns and have come across classes inheriting from other classes and being instantiated by putting
Superclass s = new Subclass()
as opposed to
Subclass s = new Subclass()
My initial thoughts were that it's more from a design point of view and due to abstraction or information hiding. However, I'm still unsure and haven't been able to find a clear explanation.
Why would someone instantiate by using Superclass s = new Subclass()
, and is it ever acceptable to use Subclass s = new Subclass()
?
I'd greatly appreciate any help from someone who can help shed some light on this for me.