Java object oriented inheritance

Keywords: Java Back-end

Java object oriented inheritance

Inheritance describes the ability to use all the functions of an existing class and extend the functions without rewriting the original class.

To use inheritance, the is-a principle (code reuse) must be met. The defined syntax is: subclass extends parent class.

Subclass: also known as a derived class, a subclass must have all the attributes and behaviors of the parent class, and have more attributes, richer behaviors and smaller representation range.

Parent class: also known as superclass.

Basic implementation of inheritance:

class Person {
    private String name;
    private int age;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
}

class Student extends Person {
}

public class JiCheng {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("Zhang San");
        stu.setAge(20);
        System.out.println("The name is:"+stu.getName()+", Age is:"+stu.getAge());
    }
}


Through the above code, it can be found that when the class inheritance relationship occurs, the subclass can directly inherit the operation of the parent class, which can realize the reuse of code. The child class also maintains the same function as the parent class. Subclasses can also expand functions.

Extended properties and methods:

class Person {
    private String name;
    private int age;
    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
}

class Student extends Person {
    // Expand new properties
    private String school;
    
    // Expand new methods
    public void setSchool(String school) {
        this.school = school;
    }
    public String getSchool() {
        return school;
    }
}

public class JiCheng2 {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("Zhang San");
        stu.setAge(20);
        stu.setSchool("Tsinghua University");
        System.out.println("The name is:"+stu.getName()+", Age is:"+stu.getAge()+", The school is:"+stu.getSchool());
    }
}

Usage principle of inheritance:
① A subclass object must instantiate the parent object before instantiation. Only a parent object can have a subclass object, that is, call the construction method of the parent class before calling the construction method of the subclass.

class Person {
    public Person() {
        System.out.println("1.Person Object generation of class");
    }
}

class Student extends Person {
    public Student() {
        //super();
        System.out.println("2.Student Object generation of class");
    }
}

public class JiCheng3 {
    public static void main(String[] args) {
        new Student();
    }
}


In the above code, we found that no statement calls the parent class constructor. Therefore, the parent object must be instantiated before the child object is instantiated.

Note: in fact, a statement super(); is implied in the subclass construction method;, At the same time, it should be noted that if no parameter free construction is provided in the parent class, you must use super() to specify the parent class construction method you want to call

② Single inheritance is limited. Multiple inheritance is not allowed in Java, but multiple inheritance is allowed.
Bad inheritance:

class A{}
class B{}
class C extends A, B{}

Correct inheritance:

class A{}
class B extends A{}
class C extends B{}

This number of layers is not recommended. Class has at most 3 levels of inheritance relationship!

③ During inheritance, the subclass will inherit all the structures of the parent class (including private properties, construction methods and common methods), but at this time, it should be noted that all non private operations belong to display inheritance (subclass objects can be called directly), and all private operations belong to implicit inheritance (called through other forms, such as setter or getter).

class Person {
    private String name;

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

class Student extends Person {
    public void fun() {
        System.out.println(getName());
    }
}

public class JiCheng4 {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("Zhang San");
        System.out.println(stu.getName());
        stu.fun();
    }
}


At this time, the attributes in the parent class are indeed inherited by the child class, but it is found that the child class can use all non private operations, and all private operations cannot be used directly, so it is called implicit inheritance.

Posted by phpstuck on Wed, 20 Oct 2021 22:06:01 -0700