java object oriented -- this and super keywords in inheritance

There are three ways to use this keyword:

this. Attributes

This (parameter list)

this. Method ()

this. Attributes

If there are many member attributes, this is required to achieve the principle of "seeing the name and knowing the meaning" attribute

For example:

Person(String nm , int a)
	{
	
		name=nm;
		age = a;
	}

Convert to:

Person(String nm , int a)
	{
	    //If you need to access a member variable, you only need to add this in front of the member variable
		this.name=name;
		this.age =age;
	}

This (parameter list)

class Person 
{
	// Member properties of Person
	private int age;
	private String name;
	
    
    //Parameterless constructor
	Person()
    {
	}
	//Constructor initialized for name
	Person(String nm)
	{
		name = nm;
	}
	//Constructor initialized for name and age
	Person(String nm , int a)
	{
		//Since there is already a constructor to initialize the name, name = nm;
        //So just call
		//To call other constructors, you need to call them through the this keyword
        // this -- points to the current object
		this(nm);// Equivalent to Person(nm)
		//Initialize age
		age = a;
	}
}

be careful:

This represents the object. This represents which object calls the function where this is located.

Statements that call other constructors must be defined on the first line of the constructor because the initialization action is executed first.

this. Method ()

public class demo {
    public static void main(String[] args) {
     
        Person  a2 =new Person("Wang Wu",55);
        a2.speak2();

    }
}


class Person
{
    private int age;
    private String name;
     //Constructor
    Person(String name , int age)
    {
      
        this.name = name;
        this.age = age;
    }
   

    //Member method
     public void speak()
    {
        System.out.println("My name is"+name);
    }
 
    public void speak2()
    {
        this.speak();//Invoke method in other member methods
        System.out.println("Age"+age);
    }
}

super() in inheritance

When creating a subclass object, the constructor of the parent class will be executed first, because the first line of all constructors in the subclass has a default implicit super() statement. This (argument list) statement is used to call the constructor in this class, and super (argument list) is used to call the constructor in the parent class.

class Fu {
    Fu() {
        //super();
        System.out.println("Parent class parameterless constructor execution");
    }
}

class Zi extends Fu
{
   Zi()
   {
        //super();
        System.out.println("Subclass parameterless constructor execution");
   }

}



class Demo
{
    public static void main(String[] args)
    {
        new Zi();
        
    }
}
//Execution results:
//Parent class parameterless constructor execution
//Subclass parameterless constructor execution
class Fu
{
    Fu(int x)
    {
        System.out.println("The parent class has a parameter constructor to execute"+x);
    }
}
class Zi extends Fu{
    //Zi(){ }
    //You cannot create a parameterless construct of a subclass because the parent class does not have a parameterless construct
    Zi(int x){
       // super(); Calling the parameterless construction of the parent class will report an error
        super(x);
        System.out.println("Subclass has parameter constructor execution"+x);
    }
}
class Demo1
{
    public static void main(String[] args)
    {
        new Zi(3);

    }
}
//Execution results:
//The parent class has a parameter constructor to execute 3
//Subclass has parameter constructor execution 3

1. Why does subclass object initialization need to access the constructor in the parent class?

Because the subclass inherits the content of the parent class, you must first see how the parent class initializes the content when creating an object.

2. Why does the constructor in the subclass have an implicit super()?

Reason: a subclass inherits the contents of the parent class, so when initializing a subclass, you must first perform the initialization action of the parent class in the parent class to make it easier to use the contents of the parent class.

When there is no empty parameter constructor in the parent class, the constructor of the child class must have a displayed super statement specifying the constructor in the parent class to be accessed.

application

Create a rectangular class, add methods for calculating area and perimeter, and then create a square class to inherit this rectangular class and its methods.

class rectangle{
    private int a;
    private int b;
    //The parent class has a parameter constructor
    public  rectangle(int a, int b){
        this.a=a;
        this.b=b;
   }

    public int getA() {
        return a;
    }


    public int getB() {
        return b;
    }


    public  int getCir(){
        return a*2+b*2;
    }
    public  int getArea(){
        return a*b;
    }
    
}
class square extends rectangle{
    //Subclass has parameter constructor
    public square(int c){
        super(c,c);//Call the parameterized constructor of the parent class and pass in the parameters
    }

}
 class test {
        System.out.println(new square(5).getArea());
        System.out.println(new square(5).getArea());
}
//result:
//20
//25

Keywords: Java

Added by smti on Mon, 20 Dec 2021 02:38:39 +0200