Note: In Java, you can call the constructor of the superclass (base class) from a subclass (derived class) constructor using the super keyword. This is often necessary when you want to initialize the inherited members or perform additional setup in the superclass constructor before initializing the subclass-specific members. Here’s how you can call the superclass constructors
Calling The Superclass Constructors And Hidden Members
- When a sub class wants to get a parent class’ members, it uses the keyword “super”.
- Also used for calling parent class’ constructor
Type Casting
- Assign an object to a super class variable, aka upcasting.
- E.g. Parent p = new Child();
- Assign an object referred by a super class variable, to a sub class variable, aka downcasting.
- E.g. Parent p = new Child()
Child c = (Child)p;
The Instance of Keyword
- The “instance of” keyword can be used to test if an object is of a
specified type. - Applying “instanceof” on a null reference variable returns false
- A sub class will always pass the test made by “instanceof” to check
for the super class type.