——Java training, Android training, iOS training, .Net training, looking forward to communicating with you! ——-
7.1 Abstract class
/*
When the same function appears in multiple classes, but the functional subjects are different,
This can be extracted upward. At this time, only the function definition is extracted, but not the function body.
Abstract: Can’t understand.
Characteristics of abstract classes:
1. Abstract methods must be in abstract classes.
2. Both abstract methods and abstract classes must be modified with the abstract keyword.
3. Abstract classes cannot use new to create objects. Because there is no point in calling abstract methods.
4. To use abstract methods in abstract classes, all abstract methods must be overridden by subclasses and then called by subclass objects.
If a subclass only covers some abstract methods, then the subclass is still an abstract class.
Abstract classes are not much different from general classes.
You can describe things how you want to describe them, but there are some incomprehensible things about them.
These uncertain parts, which are also the function of the thing, need to appear explicitly. But the subject cannot be defined.
Represented through abstract methods.
Abstract classes have more abstract functions than general classes. That is, abstract methods can be defined in classes.
Abstract classes cannot be instantiated.
Special: Abstract methods do not need to be defined in an abstract class. This only prevents the class from creating objects.
Exercise:
abstract keyword, and which keywords cannot coexist.
final: A class modified by final cannot have subclasses. The class modified by abstract must be a parent class.
private: A private abstract method in an abstract class cannot be overridden unless it is known to subclasses.
What happens with abstract methods is that they need to be overridden.
static: If static can modify the abstract method, then even the object is omitted, and the class name can be called directly.
But it makes no sense to run abstract methods.
Is there a constructor in an abstract class?
Yes, the abstract class is a parent class and needs to provide instance initialization for subclasses.
*/
7.2 Inheritance
Inheritance:
1. Improved code reusability.
2. Create a relationship between classes. Only with this relationship can we have the characteristics of polymorphism.
Note: Never inherit in order to obtain the functions of other classes and simplify the code.
There must be a relationship between classes before they can be inherited. The affiliation is a.
In Java language: Java only supports single inheritance and does not support multiple inheritance.
Because multiple inheritance can easily bring security risks: when the same function is defined in multiple parent classes,
When the functional content is different, the subclass object is not sure which one to run.
But java retains this mechanism. And use another form of embodiment to complete the representation. Realize more.
Java supports multi-level inheritance. That is, an inheritance system
How to use functions in an inheritance system?
If you want to use the system, first check the description of the system’s parent class, because the parent class defines the common functions of the system.
By understanding the common functions, you can know the basic functions of the system.
Then this system is basically ready for use.
Then when making a specific call, an object of the most subclass must be created. Why?
Firstly, it is possible that the parent class cannot create objects,
The second is to create subclass objects to use more functions, including basic and unique ones.
A simple sentence: check the parent class function and create a subclass object to use the function.
7.3 Characteristics of variables, functions and constructors in child and parent classes
/*
After the sub-parent class appears, the characteristics of the class members:
Members of the class:
1, variable.
2, function.
3. Constructor.
1, variable
If a non-private member variable with the same name appears in a subclass,
To access variables in this class, subclasses use this
If a subclass wants to access a variable with the same name in the parent class, use super.
The use of super is almost the same as the use of this.
This represents a reference to an object of this class.
super represents a reference to the parent class object.
*/
/*
2, Functions in child and parent classes.
When the subclass has the same function as the parent class,
When the subclass object calls this function, the content of the subclass function will be run.
Just like the function of the parent class is overridden.
This situation is another characteristic of functions: rewriting (overwriting)
DangziA class inherits the parent class and inherits the functions of the parent class into the subclass.
However, although the subclass has this function, the content of the function is inconsistent with the parent class,
At this time, there is no need to define new functions. Instead, use override special, retain the function definition of the parent class, and rewrite the function content.
Coverage:
1. When a subclass overwrites a parent class, it must be ensured that the permissions of the subclass are greater than or equal to the permissions of the parent class before it can be overwritten, otherwise the compilation will fail.
2, static can only override static.
Remember everyone:
Overloading: only look at the parameter list of the function with the same name.
Rewriting: The methods of the child and parent classes must be exactly the same.
*/
/*
3. Constructor in child and parent classes.
When initializing a subclass object, the constructor of the parent class will also run,
That’s because the constructor of the subclass has an implicit statement super();
super(): will access the constructor of the parent class with empty parameters. Moreover, the default first line of all constructors in subclasses is super();
Why subclasses must access the constructor in the parent class.
Because the data subclasses in the parent class can be obtained directly. Therefore, when creating a subclass object, you need to first check how the parent class initializes these data.
So when a subclass initializes an object, it must first access the constructor in the parent class.
If you want to access the constructor specified in the parent class, you can specify it by manually defining the super statement.
Note: The super statement must be defined in the first line of the subclass constructor.
The instantiation process of subclasses.
Conclusion:
All constructors of subclasses will access the constructor with empty parameters in the parent class by default.
Because the first line in each constructor of the subclass has an implicit super();
When there is no constructor with empty parameters in the parent class, the subclass must manually specify to access the constructor in the parent class through the super statement.
Of course: the first line of the subclass’s constructor can also manually specify this statement to access the constructor in this class.
At least one constructor in the subclass will access the constructor in the parent class.
*/
7.4 final keyword
/*
final: final. As a modifier,
1. You can modify classes, functions, and variables.
2. Classes modified by final cannot be inherited. To avoid being inherited, functions are overridden by subclasses.
3. Methods modified by final cannot be overridden.
4. A variable modified by final is a constant that can only be assigned a value once. It can modify both member variables and local variables.
When describing things, the appearing values of some data are fixed, so in order to enhance readability, these values are given names. Easy to read.
And this value does not need to be changed, so add final modification. As a constant: The writing convention for constants is all capital letters if they consist of multiple words.
Words are connected by _.
5. When an inner class is defined at a local location in the class, it can only access the local variables modified by final.
*/
7.5 Interface
/*
Interface: initial understanding, it can be considered as a special abstract class
When the methods in an abstract class are abstract, then the class can be represented in the form of an interface.
class is used to define classes
interface is used to define the interface.
When defining an interface, format characteristics:
1. Common definitions in interfaces: constants, abstract methods.
2. Members in the interface have fixed modifiers.
Constants: public static final
Method: public abstract
Remember: all members of an interface are public.
Interface: It is not possible to create objects because there are abstract methods.
It needs to be implemented by a subclass. Only after the subclass has covered all the abstract methods in the interface can the subclass be instantiated.
Otherwise the subclass is an abstract class.
Interfaces can be implemented by multiple classes, which is also a conversion form that does not support multiple inheritance. Java supports multiple implementations.
*/