JAVA object-oriented inheritance super

1 succession

1.1 concept

Inheritance is one of the most prominent features of object-oriented
Inheritance is to derive a new class from an existing class. The new class can absorb the data properties and behaviors of the existing class and expand new capabilities
Java inheritance is a technology that builds new classes based on the definition of existing classes
The definition of a new class can add new data or new functions, or use the functions of the parent class, but it cannot selectively inherit the parent class (super class / base class)
This inheritance makes it easy to reuse the previous code, which can greatly shorten the development cycle and reduce the development cost

1.2 features

  1. Use the extends keyword to represent inheritance relationships
  2. It is equivalent to the subclass copying the function of the parent class
  3. Java only supports single inheritance
  4. Inheritance can be passed on (a relationship like grandpa / son / grandson)
  5. Cannot inherit private members of a parent class
  6. Inheritance is used to modify multiple functions. Subclasses can expand functions while having the functions of the parent class
    Like the relationship between is a nd a

2 supper

You can use the content of the parent class through this keyword. Super represents a reference object of the parent class
Note: in the constructor, the calling position must be the first line

3 Override of method

  1. After inheritance, the child class has the function of the parent class
  2. In a subclass, you can add functions specific to the subclass or modify the original functions of the parent class
  3. When the signature of the method in the subclass is exactly the same as that of the parent class, overwrite / replication will occur
  4. Format requirements: the method name and parameter list should be completely consistent, so that the method body is rewritten
    Note: the private method of the parent class cannot be overridden. When overriding the parent class method, the modifier
    When a subclass overrides a parent class method, the subclass modifier must be greater than or equal to the permission of the parent class modifier

4 usage of inheritance

4.1 use of member variables

package cn.oopextends;
/**This class is used to test the use of member variables in inheritance*/
public class TestExtends1 {
	//5. Create the entry function main of the program
	public static void main(String[] args) {
		//6. Create objects for testing
		Son s = new Son();
		s.eat();
	}
}
//1. Create parent class
class Father{ 
	//7. Define the attribute in the parent class
	int sum = 1000;
	int count = 888;
}
//2. Create subclass Son
class Son extends Father{
	//4.2 defining member variables
	int sum = 100;
	//3. Common methods for defining subclasses
	public void eat() {
		//4.1 defining local variables of subclasses in common methods
		int sum = 10;
		System.out.println(sum);//10. The principle of proximity of variables is to print local variables
		System.out.println(this.sum);//100, using the member variable sum specified by this
		
		/**To use the sum resource of the parent class in the subclass, you need to use super Specify
		 * super Is an object reference that represents a parent class
		 * It can be understood as Father super = new Father();
		 * */
		System.out.println(count);//Using the resources of the parent class
		System.out.println(super.sum);
		//1000, using the member variable sum of the parent class specified by super
	}
}

4.2 use of member variables

package cn.oopextends;
/*This class is used to test the use of member methods in inheritance*/
//Override: after inheritance, the child class can override if it is not satisfied with the function of the parent class
//Rewriting rules: two are the same, two are small and one is large
//The method name is the same, and the parameter list is the same
//Return value type of subclass method < = return value type of parent method
//Exception type thrown by subclass method < = exception type thrown by parent method
//Modifier of subclass method > = modifier of parent method
//TIPS: the subclass does not have permission to modify the private methods of the parent class
public class TestExtends2 {
	public static void main(String[] args) {
		//4. Create parent and child class objects for testing
		Father2 f = new Father2();
		Son2 s = new Son2();
		f.eat();
		/**1.After inheritance, subclasses can use all non private functions of the parent class*/
		s.eat();
		s.study();
	}
}
//1. Create parent class
class Father2{
	//3. Create methods in the parent class
	public void eat() {
		System.out.println("Dad loves meat~");
	}
	public void play() {
		System.out.println("I like flying kites~");
	}
}
//2. Create a subclass and establish an inheritance relationship with the parent class
class Son2 extends Father2{
	//5. Create sub class specific functions
	public void study() {
		System.out.println("It's almost the Dragon Boat Festival. Learn to make zongzi~");
	}
	
	/**OCP Principle: facing modification and closing, and facing expansion and opening up
	 *   Only function expansion is allowed, and the original code is not allowed to be modified*/
	//6. Modify the original function of the parent class -- it is not allowed to modify the original code in the parent class
	//Function modification -- Method rewriting
	/**Overridden rule:
	 * Consistent with the method signature of the parent class [method name & parameter list] 
	 * Modifier of subclass method > = modifier of parent method
	 * Return value type of subclass < = return value type of parent class
	 * Then modify the implementation of this method in the subclass, and the function of the parent class has not been changed
	 * When overriding, the subclass must have the permission to override, and the subclass has no right to modify the private method of the parent class*/
	@Override 
	/**This is an annotation, which can be understood as a label to mark whether this method is an rewritten method*/
	public void eat() {
		System.out.println("My son likes vegetables~");
	}
	public void play() {
		System.out.println("I like playing video games");
	}
}

4.3 use of construction method

package cn.oopextends;
/**This class is used to test the use of construction methods in inheritance*/
//Summary:
//1. When a subclass creates an object, it will access the parameterless structure of the parent class by default
//2. In the first line of the subclass construction method, there is a default statement super() -- calling the parameterless construction of the parent class
//3. When the parent class has no parameterless construction, you can call other construction methods of the parent class through super
//4. The construction method cannot be inherited!
public class TestExtends3 {
	//3. Create the entry function main of the program
	public static void main(String[] args) {
		//4. Create parent object
		//Father2 f = new Father2("braised meat");
		//6. Create subclass objects
		Son2 s = new Son2();
	}
}

//1. Create parent class
class Father2{
	/**0.Can construction methods be inherited-- may not!!!
	 * Requirements for syntax structure: the method name of the construction method needs to be consistent with the class name of this class, which naturally does not meet the requirements
	 * */
	//5. Manually add the parameterless structure of the parent class
//	public Father2() {
//		System.out.println("I am a parameterless construct of Father2");
//	}
	//8. Create a parameter containing construct of the parent class
	public Father2(String s) {
		System.out.println("I am Father2 Parametric structure of"+s);
	}
}
//2. Create subclasses
class Son2 extends Father2{
	//7. Construction method for creating subclasses
	public Son2() {
		/**1.super() exists by default in the subclass construction method, so when creating a subclass object, the parameterless construction of the parent class will be called by default*/
		/**2.When a subclass creates an object, the parameterless construction of the subclass is automatically called
		 * However, after inheritance, you will first call the parameterless construction of the parent class, and then perform the functions in your own construction methods
		 * */
		/**3.When there is no parameterless construct in the parent class, the parameterless construct of the parent class is called
		 * You must call the constructor of a parent class*/
		super("braised pork in brown sauce");
		System.out.println("I am Son2 Nonparametric structure of");
	}
}

5 expansion

5.1 difference between this and super

  1. This represents a reference to this class of objects
    Class cat {this. XXX} can be understood as: Cat this = new Cat();
  2. super represents the reference of the parent class object
    class Father{ }
    class Son extends Father{ super.XXX }
    It can be understood as: Father super = new Father();
  3. this can be used to distinguish between member variables and local variables when two variable names are the same
  4. This can be called between constructors of this class. The position must be the first statement. Note that they cannot call each other and will loop
  5. Super refers to a subclass that wants to use the function of the parent class after the inheritance relationship occurs. It can be called through super
  6. If you want to use the function of the parent class after rewriting, you need to use super to call
  7. When super calls the parent class construction method, it must appear in the first statement of the child class construction method. Moreover, if the parent class does not provide a parameterless construction, the child class can call other parameterless constructions of the parent class through super

5.2 difference between Overload and Override

  1. Overload: refers to the phenomenon in a class. It refers to that there are many methods with the same name in a class, but the parameter lists of methods are different
  2. Rewrite: refers to the subclass modifying the original function of the parent class after the inheritance relationship (two classes) occurs
    Syntax rule: the method signature (return value type, method name (parameter list)) of the subclass is consistent with that of the parent class
    Modifier of overriding method: subclass Permission > = permission of parent class
  3. The meaning of overloading: it is to facilitate the outside world to call methods. Any parameter program can find the corresponding method to execute, which reflects the flexibility of the program
  4. The meaning of Rewriting: it is to modify and expand the functions without modifying the source code
    (OCP principle: facing modification and closing, and facing expansion and opening)

Keywords: Java Programming Class

Added by mrjonnytou on Sun, 23 Jan 2022 00:37:17 +0200