[JAVA Learning - Inheritance and Polymorphism] Inheritance

1. Overview of inheritance The three major characteristics of object- oriented : encapsulation , inheritance
, and polymorphism
Inheritance is the premise of polymorphism, that is, if there is no inheritance, there will be no
polymorphism . That is, subclasses can be treated as parent classes. For example, if the parent class is an employee and the child class is a lecturer, then "a lecturer is an employee". Relationship: is-a.
insert image description here



Define the format of the parent class: (a common class definition)
public class parent class name { // ... }

Format for defining subclasses:
public class subclass name extends parent class name { // ... } */ 3. Access characteristics of member variables in inheritance



/*
In the inheritance relationship between parent and child classes, if the member variable has the same name, when creating a subclass object, there are two ways to access it:

Access member variables directly through subclass objects:
whoever is on the left of the equal sign will be used first, and if there is no one, look up.
Indirect access to member variables through member methods:
whoever the method belongs to will be used first, and if there is no method, look up.
*/

Fourth, distinguish the three variables with the same name in the subclass method

/*
Local variable: Directly write member variable name
Member variable of this class: this.Member variable name
Parent class member variable: super.Member variable name
*/

5. Access characteristics of member methods in inheritance
/*
In the inheritance relationship between parent and child classes, create subclass objects and access member methods:
whoever created the object will be used first, and if there is no object, look up.

Note:
Whether it is a member method or a member variable, if there is no member method, it will look up the parent class, and it will never look down for the subclass.

Override
concept: In the inheritance relationship, the name of the method is the same, and the parameter list is the same.

Override (Override): The name of the method is the same, and the parameter list [is the same]. Overwrite, overwrite.
Overload (Overload): The name of the method is the same, but the parameter list is [different].

Overriding and overriding features of the method: when creating a subclass object, the subclass method is preferred.
*/

Guess you like

Origin blog.csdn.net/weixin_45329445/article/details/111117237