·
DEFINE
An object can be referenced using either of the following:
―Specific child subclass types
―Generic parent superclass types
To invoke an operation on the object,
reference type has to be specific enough to be
at least at the level in the class hierarchy
where the operation was first declared.
reference type has to be specific enough to be
at least at the level in the class hierarchy
where the operation was first declared.
RULES
⚠ Casting is required to assign parent to child reference type.
✅ No casting is required to assign child to parent reference type.
❌ Casting is not possible between objects of sibling types.
⬆ˑˑObjectˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑObjectˑˑˑ⚠
⬆ˑˑˑBikeˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑBikeˑˑˑˑ⬇
✅ˑˑBMXˑˑˑˑˑ<-❌->ˑˑˑˑCruiserˑˑ⬇
⬆ˑˑˑBikeˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑˑBikeˑˑˑˑ⬇
✅ˑˑBMXˑˑˑˑˑ<-❌->ˑˑˑˑCruiserˑˑ⬇
SNIPPET
//✅ BMX b1 = new BMX(); Bike b2 = new Cruiser(); Bike b3 = new BMX(); Object b4 = new BMX(); //⚠ Bike b5 =(Bike) new Object(); Cruiser b6 = (Cruiser) new Bike(); //❌ Cruiser b7 = (Cruiser) new BMX(); //does not compile
LINK