Mapa mental
Inheritance
Contenido del mapa
Inheritance
inheritance: the process of extending a class in OOP
- the original class is called the parent class, base class, or superclass
- the new class is called the child class, subclass, or derived class
- you extend a class by using the extends keywork in a class declaration, after the class name and before the parent class
- all Java classes automatically extend the java.lang.Object class
- a class can only extend one class
the is-a relationship
- when you create a new class by inheritance, the subclass and the superclass have an "is-a" relationship
- a subclass is a special type of its superclass
- the is-a relationship does not go the other way
- a subclass inherits all public methods and fields of its superclass
- it is legal to assign an instance of a subclass to a reference variable of the parent type
- a subclass is a special type of its superclass
- when you create a new class by inheritance, the subclass and the superclass have an "is-a" relationship
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
- you can change the behavior of a method in the parent class
- happens when you write in a subclass a method that has the same signature as a method in the parent class
- simply write the new method in the subclass
- you can override the superclass's public and protected methods, and if the subclass and superclass are in the same package, you can override a method with the default access level
- if only the name is the same but the list of arguments is not, then it is method overloading
- happens when you write in a subclass a method that has the same signature as a method in the parent class
- you cannot override a final method
- you can change the behavior of a method in the parent class
calling the superclass's constructors
- when you instantiate a child class by invoking one of its constructors, it calls the no-argument constructor of the direct parent class
- you can explicitly call the parent's constructor from a subclass's constructor by using the super keyword, but it must be the first statement in the constructor
- the super keyword can also be used to call a hidden member or an overridden method in a superclass
- you can explicitly call the parent's constructor from a subclass's constructor by using the super keyword, but it must be the first statement in the constructor
- when you instantiate a child class by invoking one of its constructors, it calls the no-argument constructor of the direct parent class
type casting
- you can only cast an instance of a subclass to its parent class - called upcasting
- downcasting to a sublcass is only allowed if the parent class reference is already pointing to an instance of the subclass
final classes
- you can prevent others from extending your class by making it final using the keyword final in the class declaration
the instanceof keyword
- can be used to test if an object is of a specified type
Información
- Descripción
- Mapa Mental sobre Inheritance, creado por Hadley Browder el 21/11/2019.
- Creado por
- Hadley Browder
- Visitas
- 0
- Valoración