Mapa mental
Inheritence
Conteúdo do mapa
Inheritence
Extending a class
- Add "extends" after the class name and before the parent class
The is-a Relationship
- The special relationship between a sub class and the super class
- i.e. Animal & Bird, Fish, Dog
- A subclass inherits all public methods & fields of its superclass (not private ones)
Accessibility
- From within a subclass, you can access the superclass's public & protected methods/fields (but not private ones)
- You can only reduce (not increase) visibility in the superclass from the subclass
Method Overriding
- You can change the behavior of a method in the superclass within the subclass
- Override a (non-final) method to change its behavior
- No need to change anything in the Parent class
- Simply write the new method in the subclass
Calling the Superclass's Constructors
- Just like an ordinary class, use the "new" keyword to create an instance of it
- Use "super" keyword to represent an instance of the direct superclass of the current object
- Can be used to explicitly call the parent's constructor from a subclass's constructor
- super must be the first statement in the constructor
- Can be used to explicitly call the parent's constructor from a subclass's constructor
Calling the Superclass's Hidden Members
- The "super" keyword can be used to call a hidden member or an overridden method in a superclass
Type Casting
- You can only cast an instance of a subclass to its parent class
- Upcasting - casting an object to a parent class
The instanceof Keyword
- Used to test if an object is of a specified type
- normally used in an if statement
Informações
- Descrição
- Mapa Mental sobre Inheritence, criado por Maggie Tieu em 21-11-2019.
- Criado por
- Maggie Tieu
- Visualizações
- 0
- Avaliação