• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Superclass & Subclass variable assignment question

Im still at the very beginning here, learning terminology, simple code etc so i hope ive got it right.

1. Superclass declared variables can be assigned to Subclass objects but with a loss of the Subclasses functionality when referenced through the Superclass variable

but

2. Subclass declared variables cannot be assigned to Superclass objects at all

Why not? Surely it would be a similar situation either way except in number 2 the object would be the part that does not understand any Subclass specific commands.

I don't see why anyone would mix/match anyway but its for the purpose of learning 🙂
 
Consider the superclass (aka base class) "vehicle" and two subclasses "car" and "boat".

"car" will define a property List<Wheel> Wheels. If things worked the way you would suggest, the base class would assume the Wheels property and, as such, it would then be inherited by the Boat subclass, which wouldn't make sense at all.

This highlights why it's so important to carefully design your object graph - otherwise you get nonsensiclal relationships.

Also, be careful about how you state things. Superclass variables aren't "assigned" to their subclasses; they're inherited by their subclasses and therefor callable against their subclasses. Also, we prefer the term "members" instead of "variables.". "members" includes simple fields, properties (with accessor methods), methods, etc.
 
Back
Top