The subclass and inheritance of Java program development and learning

(Study reference book: Java2 practical tutorial fifth edition)

One, subclass and parent class

Inheritance is a mechanism for creating new classes from existing classes. Using inheritance, you can first define a general class with common attributes. According to the general class, define a subclass with special attributes. The subclass inherits the attributes and behaviors of the general class, and adds its own new attributes and behaviors as needed. The inherited class is called the child class, and the inherited class is called the parent class (super class). Java does not support multiple inheritance, that is, subclasses can only have one parent class.

Second, the definition of subcategories

Use the keyword extends to define a subclass of a class. The format is as follows:

class 子类名 extends 父类名{
    
    
	...}

Three, class tree structure

The ancestor of all classes in Java is the Object class . Each class (except the Object class) has one and only one parent class, but there can be 0 or more subclasses. If a class declaration does not have the extends keyword, the class is considered a subclass of the Object class by the system. That is, the declaration "class A" and "class A extends Object" are the same.

Fourth, the inheritance of subclasses

(1) If the subclass and the parent class are in the same package, the subclass naturally inherits variables and methods that are not private in the parent class, and the access rights of inherited member variables and methods remain unchanged.

(2) If the subclass and the parent class are not in the same package, the subclass will not inherit private and friendly member variables and methods, but will only inherit shared and protected member variables and methods.

Five, the object of the subclass

When an object is created using the subclass's construction method, not only the member variables declared in the subclass are allocated memory space, but also the member variables inherited by the subclass in the parent class are also allocated memory space.

Six, instanceof operator

For the binary operator, the object is on the left and the class on the right. When the object on the left is an object created by the class on the right or its subclasses, the return value is true, otherwise it is false.

Seven, member variable hiding and method rewriting

(1) Hiding of member variables
When the member variable declared in the subclass has the same name as the member variable inherited from the parent class, the subclass will hide the inherited member variable. The characteristics of member variables inherited by subclasses are as follows:

  • Subclass objects and methods defined by the subclass themselves, operating on the member variable with the same name as the parent class refers to the member variable redeclared by the subclass.
  • The subclass object can call the method inherited from the parent class to operate the member variable hidden by the subclass, that is, the member variable operated by the method inherited by the subclass must be the member variable inherited or hidden by the subclass.
  • The newly defined method of the subclass cannot manipulate the hidden member variables of the subclass (you need to use the super keyword)

(2) Method rewriting

  • Grammar rule : If the subclass can inherit a certain method of the parent class, then the subclass has the right to override this method. The so-called method rewriting refers to the definition of a method in the subclass. The type of this method is consistent with the type of the method of the parent class or the subtype of the method of the parent class, and the name of the method, the number of parameters, and the type of the parameters The method is exactly the same as the parent class.
  • Rewriting purpose : subclasses can hide inherited methods through method rewriting, and subclasses can change the state and behavior of the parent class to their own state and behavior through method rewriting. The overridden method can operate not only the inherited member variables and methods, but also the newly declared member variables and new methods defined by the subclass. However, the member variables and methods hidden by the subclass cannot be manipulated.
  • Note for overriding: When overriding the method of the parent class, it is not allowed to reduce the access authority of the method, but the access authority can be increased. (Order of permissions: public>protected>friendly>private)

Eight, super keyword

(1) The role and use of the super keyword:
Once the subclass hides the inherited member variable or method, the object created by the subclass no longer owns the variable or method, and the variable is owned by the keyword super. as follows:

super.x//操作被隐藏的成员变量x
super.play()//调用被隐藏的方法

(2) Use the super keyword to call the construction method of the parent class.
When the construction method of the subclass creates an object of the subclass, the construction method of the subclass always first calls a certain construction method of the parent class. If you don't clearly indicate which constructor of the parent class to use, call the parent class without parameters.
Since the subclass cannot inherit the construction method of the parent class, it is necessary to use the super keyword to call the construction method of the parent class when writing the construction method of the subclass, and super must be the first sentence of the construction method of the subclass. If you do not write the super statement, then the default is: super ();
due to Java's parameterless constructor, when multiple constructors are defined in the parent class, a constructor without parameters should be included to prevent errors when the subclass omits super.

class 父类{
    
    
	父类(构造方法){
    
    
		...;
	}
	...;
}

class 子类 extends 父类{
    
    
	子类(构造参数){
    
    
		super(构造参数);	//父类的构造方法
		...;			//构造方法
	}
}

Nine, final keywords

The final keyword can modify local variables in classes, member variables, and methods.
(1) Final class The final class
can be declared with final, and final cannot be inherited. as follows:

final class A{
    
    
	...;
}

(2) Final method
If a method of the parent class is modified with final, then this method is not allowed to be rewritten by the subclass. (Honestly inherit, no tampering is allowed)

(3) Final constants
The member variables and local variables modified with final are constants, and certain values ​​must be given when they are declared.

10. Up-transformation of the object

Define a parent class object and a subclass object. Put the reference of the subclass object in the parent class object, then the parent class object is called the upper transformation object of the subclass object . It has the following characteristics:

  • The upper transformation object cannot manipulate the new member variables of the subclass (losing this part of the attributes), and cannot call the new methods of the subclass (losing some behaviors)
  • The upper transformation object accesses the member variables and methods inherited or hidden by subclasses. If the subclass rewrites an instance method of the parent class, when the upper transformation object calls this method, it must call the rewritten instance method of the subclass.
  • If the subclass overrides the class method of the parent class, then the upper cast object of the subclass object cannot call the class method rewritten by the subclass. Only call methods of the parent class.
  • Object created by the parent class! =Upcast object of subclass object

Eleven, abstract class and abstract method

The classes and methods modified with the keyword abstract are called abstract classes and abstract methods. For abstract methods, they can only be declared but cannot be implemented (that is, there is no method body ), and it is not allowed to use final and abstract to modify the same method or class. It is also not allowed to use static to modify the abstract method, that is, the abstract method must be an instance method.

  • An abstract class can have abstract methods or no abstract methods. But non-abstract classes cannot have abstract methods.
  • Abstract classes cannot create objects with the new operator. If a non-abstract class is a subclass of an abstract class, then it must override the abstract method of the parent class and give the method body. If an abstract class is a subclass of another abstract class, it can either override the abstract methods of the parent class or inherit the abstract methods of the parent class.
  • You can use an abstract class to declare an object. Although you cannot use the new operator to create the object, you can make the object a subclass of the object, then the object can call the subclass override method.

Guess you like

Origin blog.csdn.net/YCF8746/article/details/112758171