Mapa mental
Inheritance
Conteúdo do mapa
Inheritance
Makes code extensible in any OOP language
- To extend a class, create a new class.
- Parent Class
- Child Class
- Also known as a subclass
- Can add new methods and new fields to subclass
- A subclass can override existing methods to change previous behaviors
- A subclass can be extended, however to make it inextensible, declare it as final
- Use "extend" keyword in class declaration to extend a class.
- EX: public class Volvo extends Car { }
- In this example, the class Volvo extends parent class Car.
- Characteristics
- The "Is-A" Relationship
- The type of relationship between a superclass (parent class) and subclass (child class)
- EX: public class Volvo extends Car { public int numberOfWheels = 4 public void drive() }
- A subclass inherits all public methods and field of its superclass, however, new methods and fields can be added to the subclass to increase functionality and behavior.
- In the example, Volvo is a subclass of the superclass Car. A Volvo "is a" car however a Car is not necessarily always a Volvo. So the "is-a" relationship only goes one way!
- EX: public class Volvo extends Car { public int numberOfWheels = 4 public void drive() }
- The type of relationship between a superclass (parent class) and subclass (child class)
- Accessibility
- From within a subclass, you can access its superclass's public and protected methods and fields, but not the superclass's private methods.
- If the subclass and the superclass are in the same package, you can also access the superclass's default methods and fields.
- Method Overriding
- Used to change the behavior of a method in a parent class.
- Method overriding happens when a method with the same signature as a method in the parent class is written in a subclass.
- To override a method, write the new method in the subclass, without having to change anything in the parent class.
- If the subclass and the superclass are in the same package, you can also override a method with the default access level.
- Cannot override a final method.
- Calling the Superclass's Constructors
- To explicitly call a parents constructors from a subclass's constructor, use the "super" keyword. (Must be the first statement in the constructor.)
- "Super" keyword is perfect if you want another constructor in the superclass to be invoked.
- To explicitly call a parents constructors from a subclass's constructor, use the "super" keyword. (Must be the first statement in the 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.
- "super.memberName" returns the specific member in the parent class.
- You can access any member in the superclass that is visible from the subclass using the "super" keyword.
- The "super" keyword can be used to call a hidden member or an overridden method in a superclass.
- Type Casting
- Can cast an instance of a subclass to its parent class.
- Upcasting: Casting an object to a parent class
- To upcast a child object, assign the object to a reference variable type parent.
- Can cast an instance of a subclass to its parent class.
- instanceof Keyword
- "instanceof" keyword used to test if an object is of a specified type.
- Used in an if statement: if (objectReference instanceof type)
- Applying instanceOf on a null reference variable returns false.
- "instanceof" keyword used to test if an object is of a specified type.
- The "Is-A" Relationship
- Characteristics
- A class can only extend one class in Java
- In this example, the class Volvo extends parent class Car.
- EX: public class Volvo extends Car { }
- Parent Class
- To extend a class, create a new class.
Informações
- Descrição
- Mapa Mental sobre Inheritance, criado por Radhika Kolambekar em 05-08-2019.
- Criado por
- Radhika Kolambekar · ago 2019
- Atualizado
- 5 ago 2019
- Visualizações
- 0
- Avaliação