A concise tutorial on Java extensions

Inheritance is one of the three characteristics of object-oriented. The similarity between inheritance and "inheritance" in real life is to retain some characteristics of parents, so as to reduce code redundancy and improve program efficiency.

Inheritance in Java is to extend existing classes to generate new classes. Existing classes are called parent classes, base classes, or superclasses, while newly generated classes are called subclasses or derived classes. In a subclass, you can not only include the properties and methods of the parent class, but also add new properties and methods.

The syntax format of Java subclass inheriting parent class is as follows:
Modifier class_ name extends extend_ class {
    // Body of class
}
Where class_name represents the name of the subclass (derived class); extend_class represents the name of the parent class (base class); the extends keyword directly follows the subclass name, followed by the name of the parent class to be inherited by the class. For example:

public class Student extends Person{}

Example 1
Teachers and students belong to people. They share the same attributes: name, age, gender and ID number. Students also have two attributes, student ID and professional knowledge. Teachers also have two attributes of teaching age and teaching profession. The following is the Java code to make teachers (Teacher) and students (Student) inherit from people (People). Class. The specific implementation steps are as follows.

1) Create human People and define name, age, sex and sn attributes. The code is as follows:

public class People {
    public String name; // full name
    public int age; // Age
    public String sex; // Gender
    public String sn; // ID number

    public People(String name, int age, String sex, String sn) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.sn = sn;
    }

    public String toString() {
        return "full name:" + name + "\n Age:" + age + "\n Gender:" + sex + "\n ID number:" + sn;
    }
}

As in the above code, the People class contains four public attributes, a constructor and a toString() method.

2) Create a subclass Student class of People class and define stuNo and department attributes. The code is as follows:

public class Student extends People {
    private String stuNo; // Student number
    private String department; // Major studied

    public Student(String name, int age, String sex, String sn, String stuno, String department) {
        super(name, age, sex, sn); // Call the constructor in the parent class
        this.stuNo = stuno;
        this.department = department;
    }

    public String toString() {
        return "full name:" + name + "\n Age:" + age + "\n Gender:" + sex + "\n ID number:" + sn + "\n Student No.:" + stuNo + "\n Major:" + department;
    }
}

Since the Student class inherits from the People class, the Student class also has the properties and methods of the People class. Here, the toString() method in the parent class is overridden.

Note: if there are parameterized constructors in the parent class and no overloaded parameterless constructors, there must be parameterized constructors in the subclass, because if there are no constructors in the subclass, the parameterless constructors in the parent class will be called by default, and there are no parameterless constructors in the parent class, so an error will occur.

3) Create another subclass Teacher of People class and define tYear and tDept attributes. The code is as follows:

public class Teacher extends People {
    private int tYear; // Teaching age
    private String tDept; // Major taught

    public Teacher(String name, int age, String sex, String sn, int tYear, String tDept) {
        super(name, age, sex, sn); // Call the constructor in the parent class
        this.tYear = tYear;
        this.tDept = tDept;
    }

    public String toString() {
        return "full name:" + name + "\n Age:" + age + "\n Gender:" + sex + "\n ID number:" + sn + "\n Teaching age:" + tYear + "\n Major taught:" + tDept;
    }
}

The Teacher class is similar to the Student class in that it also overrides the toString() method in the parent class.

4) Write the test class PeopleTest, create different objects of the People class in this class, call their toString() methods respectively, and output different information. The specific code is as follows:

public class PeopleTest {
    public static void main(String[] args) {
        // Create Student class object
        People stuPeople = new Student("Wang Lili", 23, "female", "410521198902145589", "00001", "Computer application and technology");
        System.out.println("----------------Student information---------------------");
        System.out.println(stuPeople);

        // Create Teacher class object
        People teaPeople = new Teacher("Zhang Wen", 30, "male", "410521198203128847", 5, "Computer application and technology");
        System.out.println("----------------Teacher information----------------------");
        System.out.println(teaPeople);
    }
} 

Single inheritance

The Java language discards the incomprehensible multi inheritance feature in C + +, that is   Java does not support multiple inheritance. Only one class is allowed to directly inherit another class, that is, a subclass can only have one direct parent class, and only one class name can be followed by the extends keyword. For example, the following code will cause compilation errors:

 

Advantages and disadvantages of inheritance


All things in nature have both advantages and disadvantages, and the inherited disadvantages are as follows:

  1. class Student extends Person,Person1,Person2{...}
  2. class Student extends Person,extends Person1,extends Person2{...}
  3. When introducing the single inheritance of Java in many places, it may be said that a Java class can only have one parent. Strictly speaking, this statement is wrong. It should be that a class can only have one direct parent, but it can have multiple indirect parents. For example, the Student class inherits the Person class, the Person class inherits the Person1 class, the Person1 class inherits the Person2 class, then Person1 and persono N2 class is the indirect parent of Student class. Figure 1 shows the relationship of single inheritance.
     


    Figure 1   Relationship between graphic classes


    As can be seen from Figure 1, the direct parent class of triangle, quadrilateral and Pentagon is polygon class, and their indirect parent class is graph class. Graph class, polygon class, triangle, quadrilateral and Pentagon form an inherited branch. On this branch, the subclass at the lower level will inherit the properties and methods of all direct or indirect parent classes at the upper level. If the two classes are different On the same branch of the inheritance tree, there will be no inheritance relationship, such as polygon class and line.

    If the direct parent of a Java class is not explicitly specified when defining it, the class inherits the java.lang.Object class by default. Therefore, the java.lang.Object class is the parent of all classes, either its direct parent or its indirect parent. Therefore, all Java objects can call the instance methods defined by the java.lang.Object class.

    Notes on using inheritance:
  4. Subclasses generally contain more properties and methods than parent classes.
  5. private members in the parent class are invisible in subclasses, so they cannot be used directly in subclasses.
  6. There must be a "is a" or "is-a" relationship between the parent class and its subclasses, otherwise inheritance cannot be used. However, not all classes comply with "is-a" For example, a square is a rectangle, but you can't let the square class inherit the rectangle class, because the square can't get anything from the rectangle. The correct inheritance relationship is that the square class inherits the graphic class.
  7. Java only allows single inheritance (that is, a subclass can only have one direct parent), and C + + can inherit multiple (that is, a subclass has multiple direct parents).
  8. In object-oriented languages, inheritance is an essential and excellent language mechanism. It has the following advantages:
  9. Realize code sharing, reduce the workload of creating classes, and enable subclasses to have methods and properties of parent classes.
  10. Improve code maintainability and reusability.
  11. Improve the code scalability and better implement the method of parent class.
  12. Inheritance is intrusive. As long as you inherit, you must have the properties and methods of the parent class.
  13. Reduce code flexibility. Subclasses have more constraints after having the properties and methods of the parent class.
  14. Enhance code coupling (the principle of development projects is high cohesion and low coupling). When the constants, variables and methods of the parent class are modified, the modification of subclasses needs to be considered, which may lead to the reconstruction of large pieces of code.

 

Keywords: Java

Added by arjuna on Tue, 28 Sep 2021 23:23:15 +0300