2022Java learning notes 33 (object-oriented enhancement: inheritance, @ Override rewrite annotation)

2022Java learning notes 33 (object-oriented enhancement: inheritance, @ Override rewrite annotation)

1, What is inheritance
1. Inheritance is a relationship between classes
2. Multiple classes inherit a single class, and multiple classes can use the properties and behaviors of a single class
3. Multiple classes are called subclasses (derived classes), and a single class is called a parent class (base class or superclass)
Benefits of inheritance: provides code reusability

2, Inherited format

public class Subclass name extends Parent class name{}

Example code
==Create parent class People==

package com.zcl.d6_exteds;
/*
  Parent class
 */
public class People {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Create student class and inherit People class

package com.zcl.d6_exteds;

public class Student extends People{

    /*
    Unique behavior
     */
    public void study(){
        System.out.println("Students' unique behavior, learn from teachers");
    }
}

Create test class

package com.zcl.d6_exteds;

public class Test {
    public static void main(String[] args) {
        // Create a subclass object to see if the properties and methods of the parent class can be used
        Student s = new Student();
        // Use member variables that inherit from the parent class
        s.setName("Zhang San");
        s.setAge(25);
        System.out.println(s.getName());
        System.out.println(s.getAge());
        s.study(); // Unique behavior
    }
}

Inherited design specifications:
The same characteristics (common attributes and common methods) of subclasses are defined in the parent class, and the unique attributes and behaviors of subclasses should be defined in the subclass itself
If the unique attributes and behaviors of subclasses are defined in the parent class, other subclasses will also obtain these attributes and behaviors, which is not in line with object-oriented logic

3, Inheritance specification cases
Create role parent

package com.zcl.d7_exteds_test;

/**
 Role class parent class
 */
public class Role {
    private String name;
    private int age;

    /**
     Joint behavior
     */
    public void queryCourse(){
        System.out.println(name+"Start viewing course information");
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Create student class

package com.zcl.d7_exteds_test;
// Inherit role parent
public class Student extends Role{
    // Unique properties
    private String ClassName;

    // Unique behavior
    public void writeInfo(){
        System.out.println(getName()+"Great study today");
    }

    public String getClassName() {
        return ClassName;
    }

    public void setClassName(String className) {
        ClassName = className;
    }
}

Create test class

package com.zcl.d7_exteds_test;

public class Test {
    public static void main(String[] args) {
        // Create student object
        Student s = new Student();
        s.setName("Zhang San");
        s.setAge(18);
        s.setClassName("Talent class");
        // output data
        System.out.println(s.getName());
        System.out.println(s.getAge());
        System.out.println(s.getClassName());
        // Parent class joint behavior
        s.queryCourse(); // Zhang San began to check the course information
        // Subclass unique behavior
        s.writeInfo(); // Zhang San studied very well today
    }
}

4, Characteristics of inheritance

  1. Subclasses can inherit the properties and behaviors of the parent class, but subclasses cannot inherit the constructor of the parent class
    Subclasses can inherit the private member variables of the parent class, but they cannot directly access dispute
    Can a subclass inherit the static members of the parent class? [dispute]
    1. Subclasses can directly use the static members (shared) of the parent class
    2. Subclasses cannot inherit static members of the parent class (sharing is not inheritance)
  2. java is a single inheritance mode: a class can only inherit one direct parent class
  3. java does not support multi inheritance, but supports multi-layer inheritance
  4. All classes in java are subclasses of the objeext class
    All classes in java either inherit object directly, or inherit object by default, or indirectly, [object is the ancestor class]

5, After inheritance: access characteristics of member variables and member methods
Accessing members (member variables and member methods) in subclass methods meets the proximity principle
1. Find the local range of subclasses first
2. Then the subclass member range is found
3. Then find the parent range. If the parent range has not been found, an error will be reported

package com.zcl.d8_method;

public class ExtendsDemo {
    public static void main(String[] args) {
        wolf w = new wolf();
        System.out.println(w.name); // Subclass
        w.showName();
    }

}
class Animal{
    public String name = "Parent animal";
}
class wolf extends Animal{
    public String name = "Subclass";
    public void showName(){
        String name = "Local name";
        System.out.println(name); // Local name
        System.out.println(this.name); // name of subclass
        System.out.println(super.name); // name of parent class
    }
}

If there is a member with the same name in the subclass, the subclass will be used first. At this time, if you must use the parent class in the subclass, you can specify the member accessing the parent class through the super keyword
Format: super Parent member variable / parent member method

6, Method override after inheritance

As like as two peas in the inheritance system, the subclass has the same method declaration as the parent class.
Application scenario:
1. When the subclass needs the function of the parent class, but the function of the parent class does not fully meet its own needs
2. Subclasses can override methods in the parent class

Sample code
Mobile phone parent class

package com.zcl.d9_methodoverride;

public class Phone {
    public void call(){
        System.out.println("Call to start");
    }
    public void sendMessage(){
        System.out.println("Send SMS function");
    }
}

New mobile phones

package com.zcl.d9_methodoverride;

public class NewPhone extends Phone{
    @Override
    public void call(){
        super.call();// Call the method of the parent class (call start)
        System.out.println("Support video");
    }
	@Override
    public void sendMessage(){
        super.sendMessage();//Send SMS function
        System.out.println("Support sending pictures");
    }
}

Test class

package com.zcl.d9_methodoverride;

public class Test {
    public static void main(String[] args) {
        NewPhone hw = new NewPhone();
        hw.call();
        // Call to start
		//Support video
    }
}

@Override override annotation
It is placed on the method after method rewriting as a verification annotation for whether the rewriting is correct
After adding this annotation, if there is an error in rewriting, an error will be prompted at the compilation stage
Notes for rewriting:
1. The name and parameter list of the overridden method must be consistent with the name and parameter list of the overridden method
2. Private methods cannot be overridden
3. When a subclass overrides a parent class method, the access permission must be greater than or equal to the parent class
4. A subclass cannot override the static method of its parent class. If it is overridden, an error will be reported

Keywords: Java JavaEE

Added by beachdaze on Sun, 20 Feb 2022 04:37:26 +0200