Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
Benefits:
1: Improved code reusability.
2: The relationship between classes is generated, providing another premise of characteristic polymorphism.
The origin of the parent class: in fact, it is by a number of classes continue to extract the common content.
Java supports only single inheritance for inheritance. Java does not support multiple inheritance directly, but retains this mechanism for improvement.
Single inheritance: A class can have only one parent.
Multiple inheritance: A class can have more than one parent class.
Why not support multiple inheritance?
Because when a class inherits from two parent classes, both of which parent classes have the same functionality, which one does the subclass object run when it calls that functionality? Because there is a method body in a method in a parent class. * * * *
But Java supports multiple inheritance. A inherits B B inherits C C inherits D
With the emergence of multiple inheritance, there is an inheritance system. The top parent class in the system is extracted upwards. It defines the function of the most basic and common content of the system.
Therefore, in order for a system to be used, you can directly refer to the function of the parent class in the system to know the basic usage of the system. So when you want to use a system, you need to create objects. It is recommended to create the subclass object, because the subclass can not only use the functionality in the parent class. You can also use subclass-specific functionality.
To put it simply: For the use of an inheritance system, look up the contents of the top parent class and create the objects of the bottom child class.
After the appearance of the child and parent classes, the members of the class have what characteristics:
1: member variable.
When an object of the subclass type calls the property when the same property occurs in the subclass, the value is the property value of the subclass.
If you want to call an attribute value in a parent class, you need to use a keyword: super
This: represents an object reference of This class type.
** Super** : represents a memory space reference in the parent class to which the subclass belongs.
Note: there is no member variable with the same name in the parent class, because once defined in the parent class, the child class does not need to define it, and can inherit it directly.
2: member function.
When an identical method appears in a child class, the subclass object runs the method in the child class. As if the methods in the parent class were overwritten. So in this case, there’s another property of the function: overwrite.
When do you use overrides? When the functional content of a class needs to be changed, it can be done through overwriting.
3: constructor.
When the subclass constructor is found to be running, the superclass constructor is run first. Why is that?
The reason: The first line of all constructors of subclasses actually has an invisible super() statement;
Super (): represents the constructor of the parent class and calls the constructor in the parent class corresponding to the argument. Super (), on the other hand, is the constructor that calls the hollow arguments of the parent class.
Why do subclass objects always need to call functions in their parent class when they are initialized? (Why add super() in the first line of the subclass constructor?)
Because a subclass inherits data from its parent class, it must see how the parent class initializes its own data. So when a subclass initializes an object, it first calls the constructor of the parent class, which is the instantiation process of a subclass.
Note that all constructors in a subclass access the empty argument constructor in the parent class by default, because the first line of each subclass constructor has the default super() statement;
If there is no null argument constructor in the parent class, then the super statement must be used to specify the constructor in the parent class to access. * * * *
If a subclass constructor uses this to specify that the subclass’s own constructor is called, then the called constructor will also access the constructor in the parent class.
Question: Can super() and this() occur together in constructors?
Only one of two statements can be defined on the first line, so only one of them can appear.
Super () or this(): Why must it be defined in the first line?
Since both super() and this() call constructors, which are used for initialization, the initialization action is done first.
Details of inheritance:
When to use inheritance?
Inheritance is only possible when there is an ownership relationship between classes. A is a kind of B. A inherits B. The Wolf is a member of the canine family.
In the English book, ownership: “Is a”
Note: Do not inherit just to get existing members from other classes.
Therefore, to judge the ownership relationship, we can simply see that if after inheritance, the functions in the inherited class can be provided by the subclass, then inheritance is valid. If not, it cannot be inherited.
Details of the two:
Two points to note when overwriting methods: ****
1: When a subclass overwrites its parent class, it must ensure that the permission of the subclass method is greater than or equal to that of the parent class method to achieve inheritance. Otherwise, the compilation fails.
2: When overwriting, either all static or none static. (Static can only overwrite or be overwritten by static)
One drawback of inheritance is that it breaks encapsulation. Some classes, or their functions, need to be inherited or overwritten.
How do you solve the problem? Introduce a keyword, final: final.
Final features:
1: This keyword is a modifier that modifies classes, methods, and variables.
2: A final class is a final class and cannot be inherited.
3: A final method is a final method and cannot be overridden.
4: A final variable is a constant and can only be assigned once.
In fact, the reason for this is to give some fixed data a more readable name.
Can’t you use it without final? So this value is a variable that can be changed. Add final, the program is more rigorous. When constant names are defined, they have specifications, all letters are capitalized, and if they consist of more than one word, they are joined by an _.
Abstract class: abstract
Abstract: not concrete, not understand. Abstract class representation.
In the process of continuous extraction, the method declaration in the common content is extracted, but the methods are different and there is no extraction. In this case, the extracted method is not specific and needs to be marked by the specified keyword abstract and declared as abstract method.
The class of the abstract method must be marked as abstract, that is, the class must be decorated with the abstract keyword.
Abstract class features:
1: Abstract methods can only be defined in abstract classes. Abstract classes and methods must be modified by the abstract keyword (classes and methods can be described, not variables).
2: Abstract methods define only method declarations, not method implementations.
3: Abstract classes cannot be created objects (instantiated).
4: A subclass can only be instantiated if it inherits an abstract class and overwrites all the abstract methods in the abstract class. Otherwise, the subclass is an abstract class. * * * *
Details of the abstract class:
1: Is there a constructor in the abstract class? Yes, used to initialize subclass objects.
2: Can non-abstract methods be defined in abstract classes?
* * * *. In fact, there is no big difference between abstract class and general class, they are describing things, but when abstract class describes things, some functions are not concrete. So both abstract classes and general classes, by definition, need to define properties and behaviors. However, there is one more abstract function than the normal class. And there is one less section for creating objects than normal classes.
3: Abstract keyword and which can not coexist? final , private , static
4: Can abstract classes not define abstract methods? You can. The purpose of the abstract method is simply to prevent the class from creating objects.