Return to site

JAVA: UPCASTING & DOWNCASTING

· java

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.

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ˑˑ⬇

SNIPPET

LINK