I'm a little confused exactly about how polymorphism and extending classes etc... works in Java when creating objects from these classes.
I recently had a problem which someone on here helped me resolved (see Not able to access object sub class's variable? (Java / Android)) for background.
I was attempting to create an object like so:
Quad hero = new Hero();
Where Hero was a subclass of Quad();
I had variables in my Hero class which I wasn't above to access.
The solution was to change my object creation to:
Hero hero = new Hero();
doing this, through my hero object, I was able to access all methods and variables in both my Quad and Hero classes.
My question now is - Why is this?
And with this in mind, when would it be useful to use my original method:
Quad hero = new Hero();
That one makes sense to me as Hero is also a quad. I've seen this type of declaration many times in Java code examples, and thought I understood, it but recent events have proved otherwise.
Would be grateful if someone could explain this for me - thanks