JAVA-----Detailed explanation of inheritance and super keyword

Inheritance can make subclasses have the properties and methods of the parent class or redefine or append properties and methods, etc.


Notes on inheritance :

  1. Private variables in the parent class cannot be inherited by subclasses
  2. The subclass overrides the parent class method, and the access authority cannot be lower than that of the parent class, public>default>private
  3. Subclasses can only inherit from one parent class, but there can be multiple inheritance

Let me first talk about the difference between super and this keywords in java:

Keyword Access member variables Access constructor Access member method
this this. member variable-access to this class member variable this(...) - access to the construction method of this class this. member method-access to the member method of this class
super super. member variable-access to the parent class member variable super(…) --Access to the parent class construction method super. member method-access to the parent class member method

The syntax of inheritance in java is:
public class subclass extends parent class {}

  • First look at a piece of code:
public class father {
    
    
	public int age=40;//年龄
	
	public father() {
    
    
		System.out.println("father中无参构造方法被调用");
	}
	
	public father(int age) {
    
    
		System.out.println("father中带参构造方法被调用");
	}
}

public class son extends father{
    
    
	public int age=20;
	
	public son() {
    
    
		System.out.println("son中无参构造方法被调用");
	}
	
	public son(int age) {
    
    
		System.out.println("son中带参构造方法被调用");
	}
	public void show() {
    
    
		int age=30;
		System.out.println(age);//30
		System.out.println(this.age);//20
		System.out.println(super.age);//40		
	}

}


public class Demo {
    
    
	public static void main(String[] args) {
    
    
		son s=new son();
		s.show();
		
		son s1=new son(32);
		s1.show();
	}

}

The son object is created in the main function, and the final output is:

father中无参构造方法被调用
son中无参构造方法被调用
30
20
40
father中无参构造方法被调用
son中带参构造方法被调用
30
20
40

Regardless of whether the subclass's parameterized construction or the parameterless construction is called, the parameterless construction of the parent class will be called. Why?

This is because :

  • All the constructors in the subclass will access the no-argument constructors in the parent class by default
  • Because the subclass will inherit the data in the parent class, and may also use the data in the parent class, so before the subclass is initialized, the parent class data initialization must be completed
  • The first statement of each subclass construction method is by default: super()

Therefore, every time a subclass is called, the system executes the super() statement first by default, and first calls the parameterless construction method of the parent class.


So how can we call the parent class constructor with parameters?


public class son extends father{
    
    
	public int age=20;
	
	public son() {
    
    
		super(20);
		System.out.println("son中无参构造方法被调用");
	}
	
	public son(int age) {
    
    
		super(20);
		System.out.println("son中带参构造方法被调用");
	}
	public void show() {
    
    
		int age=30;
		System.out.println(age);//30
		System.out.println(this.age);//20
		System.out.println(super.age);//40		
	}

}

Just add super(x) to the subclass constructor (x is a parameter)


  • Use super to implement subclass method rewriting

Look at the code directly

father:


public class Phone {
    
    
	public void call(String name) {
    
    
		System.out.println("给"+name+"打电话");
	}

}

Subclass:

public class newPhone extends Phone {
    
    
	@Override
	public void call(String name) {
    
    
		System.out.println("开启视频");
		super.call(name);
	}

}

Main function:

public class test {
    
    
	public static void main(String[] args) {
    
    
		newPhone p=new newPhone();
		p.call("小李");
	}

}

The super keyword can be used to directly call the parent class method, and then write the specific properties of this class in the subclass.
@Override is an annotation that can be used to check the correctness of the overriding method declaration

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113085718