Basic knowledge of JAVA (8)

Chapter VI Object Oriented (Advanced)

Goals:
1. Master the basic concepts and realization of inheritance.
2. Master the various limitations of inheritance realization.
3. Master the instantiation process of subclass objects.
4. Master the concept and realization of method override.
5. Master the role of the super keyword.
6. Grasp the basic concepts and practical applications of abstract classes and interfaces.
7. Grasp the role of object polymorphism.
8. Master common design patterns.
9. Grasp the role of the Object class and the role of its main methods.
10. Master the role of packaging, as well as the operation of automatic boxing and unpacking.
11. Master the use of anonymous inner classes.

6.1 Basic concepts of inheritance

Inherited format of format class

class 父类{
    
    }		//定义父类
class 子类 extends 父类{
    
    }//使用extends关键字实现继承

In Java, only single inheritance is allowed, and multiple inheritance cannot be used, that is, a subclass can only inherit one parent class. A subclass can have a parent class, and a parent class can also have a parent class.

Inherited subclasses are sometimes called derived classes.
The extends keyword can be used to achieve the relationship of inheritance, but the meaning of this keyword itself is "extended", more precisely, a class extends the function of an existing class. In other books, subclasses are often called derivations. class.

When using inheritance, it should also be noted that the child class cannot directly access the private members in the parent class, but the child class can call the non-private methods in the parent class, but cannot directly call the private members in the parent class.

6.2.1 The instantiation process of subclass objects

In the operation of inheritance, there are also requirements for the instantiation of subclass objects, that is, the subclass object must first call the construction method in the parent class before instantiating the subclass's own construction method.

Before instantiating the subclass object, you need to initialize the properties in the parent class. -

6.2.2 Method overriding

There is also the concept of method override in the inheritance relationship. The so-called method override means that the subclass defines a method with the same name as the parent class, but permissions must be taken into account, that is, the method overridden by the subclass cannot have more than the parent Stricter access permissions for class methods.

When the subclass defines a method with the same name as the parent class, but the access rights of this method in the subclass are expanded, conforming to the concept of overwrite. When the method is overwritten, the subclass object will call the overwritten method . If the method permissions of the subclass override are reduced, an error message will appear at compile time.

If you want to find the method of accessing the parent class in the method of the subclass now, use the super keyword.

If the subclass declares an attribute with the same name as the parent class, then the "nearest access principle" must be adopted when directly accessing in the subclass, that is, to find the attribute in this class first, if you want to call the attribute in the parent class at this time , Just use the "super." attribute format.

The difference between method overloading and overwriting

1 The word Overloading (overloading) Overriding (overriding)
2 The definition method name is the same, the parameter type or number is different, the method name, the parameter type, and the return value type are all the same.
3 There is no requirement for the permission to be overridden. Have stricter permissions
4 Scope occurs in a class occurs in an inherited class

6.2.3 The role of the super keyword

Use super to call the constructor, common methods, and attributes of the parent class from the subclass.

6.4 final keyword

The meaning of final in Java is the final meaning, and it can also be called the finalizer. You can use the final keyword to declare classes, attributes, and methods. Pay attention to the following points when declaring:
(1) The class declared using final cannot have subclasses;
(2) The method declared using final cannot be overridden by subclasses.
(3) Variables declared using final become constants, and constants cannot be modified.

Naming rules for final variables.
When using final to declare variables, all letters are required to be capitalized, for example: INFO, which is very important in development.

If a variable in a program is declared using public static final, this variable will be called a global constant.

6.5 Basic concepts of abstract classes

A preliminary explanation of class inheritance was given earlier. Through inheritance, new classes can be derived from the original classes. The original class is called the base class or parent class, and the new one is called a derived class or subclass. Through this mechanism, the derived new class can not only retain the functions of the original class, but also have more functions.
In addition to dealing with the above mechanism, in Java, a class can also be created specifically to be used as a parent. This class is called an "abstract class". The role of an abstract class is a bit similar to a "template", and its purpose is to allow the designer to modify and create a new class according to its format. But it is not possible to create objects directly from abstract classes, only new classes can be derived from abstract classes, and then objects can be created by them. However, there is also the limitation of single inheritance in the use of abstract classes, that is, a subclass can only inherit one abstract class.
The definition and usage rules of abstract classes are as follows:
(1) The class containing an abstract method must be an abstract class;
(2) Both abstract classes and abstract methods must be declared with the abstract keyword;
(3) Abstract methods only need to be declared but not required Realization;
(4) The abstract class must be inherited by the subclass, and the subclass (if not the abstract class) must override all abstract methods in the abstract class.

The definition format of the format abstract class

From the above format, it can be found that the definition of abstract class has some more abstract methods than ordinary class, and the composition of other places is basically the same as that of ordinary class.

abstract class 抽象类名称{
    
    
     属性;
     访问权限 返回值类型 方法名称(参数){
    
      //普通方法
           [return 返回值]}
     访问权限 abstract 返回值类型 方法名称(参数);//抽象方法
     //在抽象方法中是没有方法体的
}

From the above format, it can be found that the definition of abstract class has some more abstract methods than ordinary class, and the composition of other places is basically the same as that of ordinary class.

The biggest difference between abstract classes and ordinary classes is mandatory.
If a common class is used, then the subclass can selectively override some parent class methods according to its own needs, so the common class cannot restrict the subclass's overriding methods. However, abstract classes can force subclasses to override methods of the parent class. Because of this, in actual development, it is not recommended to let subclasses inherit ordinary classes, but only recommend that subclasses inherit abstract classes.

An abstract class cannot be declared using the final keyword.

Do not use private declarations for abstract methods.
When using the abstract keyword to modify the abstract method, you cannot use the private modification, because the abstract method must be overwritten by the subclass, and if the private declaration is used, the subclass cannot be overwritten.

Construction methods are allowed in an abstract class, because the abstract class still uses the inheritance relationship of the class, and there are various attributes in the abstract class, so the subclass must first instantiate the parent class before instantiating化的.

Abstract class and ordinary class.
In fact, the abstract class defines one more abstract method than the ordinary class, and there is no difference except that it cannot directly instantiate the object.

Guess you like

Origin blog.csdn.net/qq_43480434/article/details/113136533