Java learning 3: object-oriented inheritance (explain the super keyword)

Java object-oriented inheritance

1. Talk about the basic theory

What is inheritance?

Inheritance is to let one type of object have all the non-private properties and methods of another type of object !

In inheritance, there are two concepts: parent class and child class!

The subclass inherits the parent class, which is a universal relationship in the natural world! Parents and children can be different, but they must have many similarities, such as carnivores and herbivores in the animal world, they are all animals; for parents and children, children must look more like their parents and are related by blood Will also follow the relevant laws, this is the connection! Generally speaking, the attributes and methods of the parent class are more general, and are often used in the subclasses ; the methods of the subclasses are often more specific, which is mutation and evolution from the perspective of the natural world; and expansion and enhancement from the perspective of software development!

Inherited related syntax templates:

Insert picture description here
This grammar rule is much simpler than C++! ! ! From the perspective of this grammar module, it does not have public, private, or protected inheritance, just a bare inheritance... It's really easy!

Some inherited features:
the first:

The child class has all the non-private properties and methods of the parent class

second:

Java does not allow multiple inheritance , that is, if A inherits B, A can no longer inherit C

third:

Java allows multi-level inheritance. This is a feature, that is to say, A inherits B, B inherits C, then A indirectly inherits C.
This is actually not good, because it improves the coupling of the code, is prone to errors, and is not easy to expand.

fourth:

Subclasses can have their own attributes and methods, that is, expand on the basis of the parent class

Second, the instantiation of subclass objects

Program display:
import java.util.Scanner;

public class Main6 {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		
		String name = sc.next();
		int age = sc.nextInt();
		String school = sc.next();
		
		Student st = new Student(name, age, school);
		st.Show();
		
		sc.close();
	}
}

class Person {
    
    
	protected String name;
	protected int age;
	protected Person(String name, int age) {
    
    
		this.name = name;
		this.age = age;
		System.out.println("Person is constructed!");
	}
}

class Student extends Person {
    
    
	private String school;
	public Student(String name, int age, String school) {
    
    
		super(name, age);
		this.school = school;
		System.out.println("Student is constructed!");
	}
	public void Show() {
    
    
		System.out.println("The student's information is:");
		System.out.println("Name is:" + this.name + "  Age is:" + this.age + "  School is:" + this.school);
	}
}
Show results:

Insert picture description here

What have we learned?

First: the order of construction of parent and child classes

According to the laws of the natural world, there are chickens before eggs! Only the existing father (ancestor) has children!
So when we instantiate an object of a subclass, we must first instantiate the ancestor, and the system must know the parent class before instantiating the subclass. So it can be seen from the above program case that the parent class is constructed first, and the subclass is constructed later.

Second: The parameterless structure and the parameterized structure of the subclass

In fact, whether it is a parameterized structure or a parameterless structure, you need to call super(); but this keyword is mainly used in the case of parameterized construction, similar to this(); such a usage, you can call the structure of the parent class Method, pass the actual parameters of the subclass to the parent class, and then instantiate the subclass after completing the instantiation of the parent class! So the most important thing is this super();

Three, the learning of super keywords

What is the super keyword?

In the above program, we have seen the super keyword, knowing that it should be the first sentence of the construction method of the subclass , and then use this super keyword to reference the construction method of the parent class!
In detail: when
we create a subclass object, we will first create the parent class object in the subclass’s construction method. You must wait until the parent class is instantiated before you can instantiate the subclass. That’s why The super keyword must be used in the first sentence of the subclass's construction method!
It is concluded that the main function of super is to complete the subclass calling the content in the parent class, that is, to call the properties or methods in the parent class.
Actually : even in the parameterless constructor, even if our programmers did not write the super keyword, in fact this super(); still exists !

Usage of the super keyword: pay attention to the word "direct"!

Insert picture description here

Experiment with the above three usages:

public class Main6 {
    
    
	public static void main(String[] args) {
    
    
		Cat c = new Cat("小花猫", 18, "大草鱼");
		c.Work();
		c.Show();
	}
}

class Animal {
    
    
	protected String name;
	protected int age;
	
	protected Animal(String name, int age) {
    
    
		this.name = name;
		this.age = age;
	}
	
	public void Work() {
    
    
		System.out.println("他们会为了生存去吃东西、交配……");
	}
	
	public void Show() {
    
    
		System.out.println(this.name + "今年已经:" + this.age + "岁了!");
	}
}

class Cat extends Animal {
    
    
	private String Food;
	public Cat(String name, int age, String Food) {
    
    
		super(name, age);
		this.Food = Food;
	}
	
	public void Work() {
    
    
		super.Work();
		System.out.println(this.name + "比较喜欢吃的东西是" + this.Food);
	}
	
	public void Show() {
    
    
		super.Show();
	}
}

Result display:

Insert picture description here

What have we learned?

Without the parentheses, the super keyword can be used as a reference to the parent class object. This object is a virtual anonymous object, at the object level, not at the class level. We can use this super reference object to access some of the more common properties or methods of the parent class! Can greatly reduce the amount of code and achieve code reuse!

What is the key difference between the super keyword and this?

The super keyword looks very similar to this keyword! What is the difference between them?

The first difference:

The super keyword is actually the reference of the object of the subclass to the parent class, and the this keyword is the reference of the object that the current method acts on!

The second difference:

The super keyword is to access the properties and methods of the parent class of the current subclass object,
while the this keyword is to access the properties and methods of the current object

The third is not a difference, but a similarity:

Both the this keyword and the super keyword must be placed in the first line of the method!

Guess you like

Origin blog.csdn.net/qq_44274276/article/details/104876869