Basic Java learning notes 12 -- Inheritance and method rewriting

catalogue

1. Inheritance overview

2. Advantages and disadvantages of inheritance

2.1 benefits of inheritance

2.2 disadvantages of inheritance

3. Access characteristics of variables in inheritance

4. super keyword

5. Access characteristics of construction methods in inheritance

6. Access characteristics of member methods in inheritance

7. Method rewrite

7.1 overview of method Rewriting:

7.2 application scenario of method Rewriting:

7.3,@Override

7.4 precautions for method rewriting

8. Considerations for inheritance in methods

1. Inheritance overview

Inheritance is one of the three characteristics of object-oriented. It can make subclasses have the properties and methods of the parent class. It can also redefine and append properties and methods in subclasses.

Inherited format:

Inherited format
 Format: public class Subclass name extends Parent class name {}
example: public class Child extends Parents {}
Child   : Is a subclass, also known as a derived class
Parents : Is the parent class, also known as the base class and superclass;

Characteristics of inherited subclasses:

  • A subclass can have the content of a parent class;
  • Subclasses can also have their own unique content.

Code demonstration:

package ExPackage;

/*Parent class*/
public class Parents {
    public void showParent(){
        System.out.println("showParent Method called");
    }
}
package ExPackage;

/*Subclass*/
public class Child extends Parents{
    public void showChild(){
        System.out.println("showChild Method called");
    }
}
package ExPackage;

/*Test class*/
public class test {
    public static void main(String[] args) {
        Parents p = new Parents();
        p.showParent();

        Child c = new Child();
        c.showChild();

        c.showParent(); //A subclass calls a method in its parent class
    }
}
result:
showParent Method called
showChild Method called
showParent Method called

2. Advantages and disadvantages of inheritance

2.1 benefits of inheritance

  • It improves the reusability of code (the same members of multiple classes can be placed in the same class);
  • It improves the maintainability of the code (if the method code needs to be modified, modify one place).

2.2 disadvantages of inheritance

  • Inheritance creates a relationship between classes and enhances the coupling of classes. When the parent class changes, the child class has to change, weakening the independence of the child class;

3. Access characteristics of variables in inheritance

Access a variable in a subclass method:

  1. Find the local range of the subclass first;
  2. If it cannot be found, it can be found in the subclass member range;
  3. If it cannot be found, it can be found in the parent class member range.

Code demonstration:

package ExPackage;

/*Parent class*/
public class Parents {
    public int age = 40; //Age
}
package ExPackage;

/*Subclass*/
public class Child extends Parents{  
    public int height = 175; //height    
    public int age = 20; //Age

    public void show(){
        int age = 30;
        System.out.println(age);
        System.out.println(height);
        //An error is reported because weight does not exist in this method, subclass or parent class
        //System.out.println(weight);
    }
}
package ExPackage;

/*Test class*/
public class test {
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
    }
}
result:
30
175

4. super keyword

The usage of the super keyword is similar to that of the this keyword.

  • This: represents the reference of this class object;
  • super: represents the identification of the storage space of the parent class (which can be understood as the reference of the parent class object).
keywordAccessing member variablesAccess construction methodAccess member method
this

this. Member variable

Access the member variables of this class

this(...)

Access this class constructor

this. Member method (...)

Accessing member methods of this class

super

super. Member variable

Accessing parent class member variables

super(...)

Access parent class constructor

super. Member method (...)

Accessing parent class member methods

Code demonstration:

package ExPackage;

/*Parent class*/
public class Parents {
    public int age = 40; //Age
}
package ExPackage;

/*Subclass*/
public class Child extends Parents{
    public int age = 20; //Age

    public void show(){
        //Access the local variable age within the method
        int age = 0;
        System.out.println(age);

        //Access the member variable age of this class
        System.out.println(this.age);

        //Access the member variable age of the parent class
        System.out.println(super.age);
    }
}
package ExPackage;

/*Test class*/
public class test {
    public static void main(String[] args) {
        Child c = new Child();
        c.show(); //A subclass calls a method in its parent class
    }
}
result:
0
20
40

5. Access characteristics of construction methods in inheritance

All constructors in the subclass will access the parameterless constructors in the parent class by default, because:

  • Subclasses inherit the data in the parent class and may use the data of the parent class. Therefore, before subclass initialization, you must complete the initialization of the parent class data;
  • The first statement of each subclass constructor defaults to: super() (that is, the parameterless constructor of the parent class).

If there are no parameterless constructors in the parent class and only parameterless constructors, you can:

  • By using the super keyword to explicitly call the parameterized constructor of the parent class;
  • Or provide a parameterless constructor in the parent class. (recommended)

Code demonstration:

package ExPackage;

/*Parent class*/
public class Parents {
//    public Parents(){
//        System.out.println("the parameterless construction method in the parent class is called");
//    }
    public Parents(int age){
        System.out.println("The construction method with parameters in the parent class is called");
    }
}
package ExPackage;

/*Subclass*/
public class Child extends Parents{
    public int age = 20; //Age

    public Child(){
        super(40); //Use the parameterized construction method of the parent class
        System.out.println("The parameterless constructor method in the subclass is called");
    }
    public Child(int age){
        super(40);
        System.out.println("The construction method with parameters in the subclass is called");
    }
}
package ExPackage;

/*Test class*/
public class test {
    public static void main(String[] args) {
        //create object
        Child c1 = new Child();
        Child c2 = new Child(20);
    }
}
result:
The construction method with parameters in the parent class is called
 The parameterless constructor method in the subclass is called
 The construction method with parameters in the parent class is called
 The construction method with parameters in the subclass is called

6. Access characteristics of member methods in inheritance

Access a method through a subclass object:

  1. First, find in the subclass member range;
  2. If it cannot be found, it can be found in the range of parent class members;
  3. If none, an error is reported (regardless of the parent class of the parent class).

Code demonstration:

package ExPackage;

/*Parent class*/
public class Parents {
    public void showParents(){
        System.out.println("Father middle showParents()Method called");
    }
}
package ExPackage;

/*Subclass*/
public class Child extends Parents{

    public void showChild(){
        System.out.println("Zizhong showChild()Method called");
    }
    public void showParents(){
        super.showParents();
        System.out.println("Zizhong showParents()Method called");
    }
}
package ExPackage;

/*Test class*/
public class test {
    public static void main(String[] args) {
        //Create an object and call a method
        Child c = new Child();
        c.showChild();
        c.showParents(); //The child calls the member methods of this class first

        //An error is reported because neither the subclass nor the parent class has this method
        //c.test();
    }
}
Zizhong showChild()Method called
 Father middle showParents()Method called
 Zizhong showParents()Method called

7. Method rewrite

7.1 overview of method Rewriting:

The subclass has the same method declaration as the parent class.

7.2 application scenario of method Rewriting:

When a subclass needs the function of the parent class and the subclass of the function subject has its own unique content, you can override the methods in the parent class. In this way, you can not only follow the function of the parent class, but also define the unique content of the subclass.

7.3,@Override

@Override is an annotation that helps us check the correctness of the override method declaration.

7.4 precautions for method rewriting

  • The private method in the parent class cannot be inherited or overridden by the child class;
  • Subclass method access permission cannot be lower (public > Default > private);

Code demonstration:

package phonePackage;

//Mobile phone class
public class Phone {
    public void call(String name){
        System.out.println("to"+name+"phone");
    }
}
package phonePackage;

//New mobile phone
public class NewPhone extends Phone {

    @Override //Annotation, indicating that the method overrides the parent method
    //Declaring a method with the same name as the parent class is called method override
    public void call(String name) {
        System.out.println("Turn on video function");
        super.call(name); //Use parent function
    }
}
package phonePackage;

public class Test {
    public static void main(String[] args) {
        //Create an object and call a method
        Phone p = new Phone();
        p.call("Zhang San");
        System.out.println();

        NewPhone np = new NewPhone();
        np.call("Zhang San");
    }
}

8. Considerations for inheritance in methods

  • Classes in Java only support single inheritance, not multiple inheritance;
  • Classes in Java support multi-layer inheritance.

Keywords: Java Back-end

Added by Stickdragon on Wed, 05 Jan 2022 01:32:48 +0200