Inheritance, Object class, method overriding, subclass constructor, similar constructor

1. What is inheritance? What are the characteristics of inheritance?
·Inheritance is to use the extends keyword to establish a parent-child relationship between one class and another class. Subclasses can inherit non-private members of the parent class.
2. For a class with inheritance relationship, how does Java create its object? After the object is created, which members can be directly accessed?
For classes with inheritance relationships, Java will use the class and its parent class, and these multiple design drawings to create objects of the class together.
What members an object can directly access is determined by multiple design drawings of the child and parent classes. Which members are exposed by these multiple design drawings will be what the object can access.

3. What are the benefits of using inheritance?

.Reduce the writing of repeated code and improve the reusability of code.

4. Two things to note about inheritance?

Java is single inheritance: a class can only inherit one direct parent class; classes in Java do not support multiple inheritance, but support multi-level inheritance.

The Object class is the ancestor of all classes in Java.

5. What is method rewriting?
●When a subclass feels that a method in the parent class is not easy to use, or cannot meet its own needs, the subclass can rewrite a method with the same method name and parameter list to override the method of the parent class. This is the method Rewrite.
●Note: After rewriting, Java will follow the proximity principle when accessing methods.
6. Other considerations for method rewriting
·Rewriting tips: Using the Override annotation, he can specify the java compiler to check whether the format of our method rewrite is correct, and the code will be more readable.

·When a subclass overrides a parent class method, the access permission must be greater than or equal to the permission of the parent class method (public > protected > default).

·The return value type of the overridden method must be the same as the return value type of the overridden method, or the range must be smaller.
●Private methods and static methods cannot be overridden, and an error will be reported if overridden.

package com.xinbao.d7_extends;

public class A {
    public void print1(){
        System.out.println("111111");
    }
    public void print2(int a, int b){
        System.out.println("2222222");
    }
}
package com.xinbao.d7_extends;

public class B extends A{
    //Method override
    @Override//Safe and readable
    public void print1(){
        System.out.println("666");
    }
    @Override
    public void print2(int a, int b){
        System.out.println("666666");
    }
}
package com.xinbao.d7_extends;

public class Student extends Object{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = 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;
    }

    @Override
    public String toString(){
        return "Student{name=" + name + ",age=" + age + "]";
    }
}
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        //Goal: Recognize method rewriting
        B b = new B();
        b.print1();
        b.print2(1,3);

        System.out.println("-------------");
        Student s = new Student("Gongyuanzheng", 16);
        System.out.println(s.toString());
        //ToString() is called by default, the address is returned before rewriting, and the content can be returned after rewriting.
        System.out.println(s);

        System.out.println("--------------");
        ArrayList list = new ArrayList();
        list.add("java");
        System.out.println(list);//Output content [java] instead of address, indicating that toString() has been rewritten in the ArrayList class
    }
}
E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=26340:E:\JVsoft\IntelliJIDEA2021.1.1\bin - Dfile.encoding=UTF-8 -classpath E:\JVsoft\code\out\production\oop-app1 com.xinbao.d7_extends.Test
666
666666
-------------
Student{name=Gong Yuanzheng,age=16]
Student{name=Gong Yuanzheng,age=16]
---------------
[java]

Process finished with exit code 0

7. Accessing other members (member variables, member methods) in subclass methods follows the principle of proximity.
●Find the subcategory locally first.
·Then find the subclass member scope.
·Then search the parent class member scope. If the parent class scope has not been found, an error will be reported.
8. If there are members with the same name in the subclass, the subclass will be used first. What if the parent class must be used in the subclass?

●You can specify access to members of the parent class through the super keyword: super. Parent class member variables/parent class member methods

package com.xinbao.d13_extends_visit;

public class Test {
    public static void main(String[] args) {
        Z z = new Z();
        z.showName();
    }
}
package com.xinbao.d13_extends_visit;

public class F {
    String name = "Parent class name";

    public void print1(){
        System.out.println("==The print1 method of the parent class is executed");
    }
}
package com.xinbao.d13_extends_visit;

public class Z extends F {
    String name = "Subclass name";

    public void showName(){
        String name = "local name";
        System.out.println(name);//Local name (nearby access)
        System.out.println(this.name);//Subclass member variables
        System.out.println(super.name);//parent class member variables
    }
}
E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=27656:E:\JVsoft\IntelliJIDEA2021.1.1\bin - Dfile.encoding=UTF-8 -classpath E:\JVsoft\code\out\production\oop-app1 com.xinbao.d13_extends_visit.Test
local name
Subclass name
Parent class name

Process finished with exit code 0

9. Characteristics of subclass constructor:
All constructors of subclasses will first call the constructor of the parent class and then execute themselves.
10. How does the subclass constructor call the parent class constructor:
●By default, the first line of code in all constructors of a subclass is super () (either written or not), which will call the parameterless constructor of the parent class.

·If the parent class does not have a parameterless constructor, we must hand-write super (…) in the first line of the subclass constructor to specify to call the parent class’s parameterized constructor.

package com.xinbao.d14_extends_constructor;

class F{
    public F(){
        System.out.println("====The parameterless constructor of parent class F was executed===");
    }
}

class Z extends F{
    public Z(){
        //super();//Exists by default
        //super(3);//If the parent class does not have a parameterless constructor, the subclass constructor will report an error. In this case, you should specify to call the parent class parameterized constructor.
        System.out.println("===The no-argument constructor of subclass Z was executed===");
    }
    public Z(int a){
        //super();//Exists by default
        System.out.println("===The parameterized constructor of subclass Z was executed===");
    }
}

public class Test {
    public static void main(String[] args) {
        //Target: subclass constructor
        Z z = new Z();//All constructors of the subclass will first call the constructor of the parent class and then execute themselves
        Z z2 = new Z(3);
    }
}
E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=27714:E:\JVsoft\IntelliJIDEA2021.1.1\bin - Dfile.encoding=UTF-8 -classpath E:\JVsoft\code\out\production\oop-app1 com.xinbao.d14_extends_constructor.Test
===The parameterless constructor of parent class F is executed===
===The parameterless constructor of subclass Z is executed===
===The parameterless constructor of parent class F is executed===
===The parameterized constructor of subclass Z is executed===

Process finished with exit code 0

Example: Why does the subclass constructor call the parent class constructor?

Member objects are forced to be divided into parent class member objects and subclass member objects. Therefore, when the subclass constructor is called, the parent class constructor must be called first before the parent class member object can be assigned a value.

package com.xinbao.d14_extends_constructor;

public class Test2 {
    public static void main(String[] args) {
        //Goal: Understand why the subclass constructor calls the parent class constructor
        Teacher t = new Teacher("六六",36,"java");
        System.out.println();
    }
}

class Teacher extends People{
    private String skill;

    public Teacher(String name, int age, String skill){
        //this.name = name;//Error
        super(name,age);
        this.skill = skill;
    }

    public String getSkill() {
        return skill;
    }

    public void setSkill(String skill) {
        this.skill = skill;
    }
}

class People{
    private String name;
    private int age;

    public People() {
    }

    public People(String name, int age) {
        this.name = name;
        this.age = 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;
    }
}

11. What are the characteristics of subclass constructor?
●All constructors in subclasses must first call the constructor of the parent class before executing themselves.

12. What are the common application scenarios of super (…) calling the parameterized constructor of the parent class?
·Assign values to the member variables that contain the parent class part of the object.
13. What is the function of this (…)?
·Call other constructors of this class in the constructor.
14. What should we pay attention to when using this (…) and super (…)?
·They must be placed on the first line of the constructor. Both cannot appear at the same time

package com.xinbao.d14_extends_constructor;

public class Test3 {
    public static void main(String[] args) {
        Student s1 = new Student("woo oh woo oh",26,"Sichuan University");

        //Requirement: If the student does not fill in the school, the default is the University of Hong Kong.
        Student s2 = new Student("Huwahuwa",22);
        System.out.println(s2.getAge());
        System.out.println(s2.getName());
        System.out.println(s2.getSchoolName());
    }
}

class Student{
    private String name;
    private int age;
    private String schoolName;

    public Student() {
    }

    public Student(String name, int age) {
// this.name = name;
// this.age = age;
// this.schoolName = "University of Hong Kong";
        this(name, age,"Hong Kong University");//Call the sibling constructor
        //super()//Error this constructor and super constructor cannot appear at the same time (super constructor can only appear once)
        

    }

    public Student(String name, int age, String schoolName) {
        //super();//default, inherit Object class
        this.name = name;
        this.age = age;
        this.schoolName = schoolName;
    }

    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;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }
}
E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=59831:E:\JVsoft\IntelliJIDEA2021.1.1\bin - Dfile.encoding=UTF-8 -classpath E:\JVsoft\code\out\production\oop-app1 com.xinbao.d14_extends_constructor.Test3
twenty two
Whoa whoa whoa
Hong Kong university

Process finished with exit code 0