Object Oriented Feature -- inheritance [java]

Basic concept: when multiple classes have the same characteristic behavior, we can extract the same content from these classes to form a public class, and then derive a new class from the public class. The new class can inherit the data properties and behavior of the existing class and expand new capabilities.

java uses the extends keyword to represent inheritance

For example:

//The Teacher class inherits from the Person class
public class Teacher extends Person{
​
}

Here, Person is called parent class, base class and superclass, and Teacher is called child class, derived class and child class.

be careful:

1. Subclasses can inherit the member variables and member methods of the parent class. Private member variables can be inherited but cannot be used directly. Subclasses cannot inherit the private member methods and construction methods of the parent class.

Understanding: ① a subclass cannot inherit the construction method of the parent class, because the construction method name must be the same as the class name, and the class names of the subclass and the parent class are different, so the subclass cannot inherit the construction method of the parent class.

② private members (member = variable + method) can't inherit, but the private member variable here is a special case, which can be inherited, but the private member method can't be inherited.

2. When subclasses construct objects, the compiler will automatically add super() to the first sentence of the called subclass construction method; Therefore, if we call the constructor of the subclass to create an object, if we only define the parameterized constructor in the parent class and do not define the parameterless null constructor, the compiler will report an error at this time. Therefore, when using inheritance, we call the parameterless constructor of the parent class in the first line of the parameterless constructor of the subclass, Call the parameterized constructor of the parent class in the first line of the parameterized constructor of the child class.

Generally speaking, add super() to the first sentence of the parameterless construction method of the subclass, and add super(size,color) to the first sentence of the parameterless construction method of the subclass. Here, size and color are just an example.

3.JAVA does not support multiple inheritance. A class can only have one direct parent class. After inheritance, the subclass can use the non private members in the parent class, and the subclass can extend the subclass specific properties and methods.

4. After inheritance, the subclass can call all non private properties and non private methods of the parent class.

When to use inheritance?

Used when it satisfies the relationship of is-a (i.e. inheritance cannot be abused)

For example: dogs inherit animals, are the same kind, what is what.

Form of inheritance

Class parent class{
  Member variable, member method
}
Class subclass extends parent class{
  Class body
}

When a class does not inherit from a class, it inherits object by default. Object is the base class of all classes.

For example:

Define a parent class Animal:

//public class Animal extends Object{
public class Animal{
       //Member variable
       private String name;
       private int age;
     //Construction method
     public Animal(){
       super();
       System.out.println("Animal Parameterless construction method in class");
     }
     public Animal(String name){
       super();
       this.name = name;
       System.out.println("Animal Parameterized construction method in class");
     }
     //Member method is the function of class
     public void eat(){
       System.out.println("Animals eat");
     }
     //Operation entry provided for private property
     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;
     }
}

Define a subclass. Dog class inherits the parent class Animal:

public class Dog extends Animal{ 
       private String color;
   public Dog(){
       super();
       System.out.println("Nonparametric construction method in dogs");
   }
  
   public Dog(String name){
       super(name);
       System.out.println("Parametric construction method in dogs");
   }
   public void play(){
       System.out.println("Dogs can play");
   }  
   public String getColor() {
      return color;
   }
   public void setColor(String color) {
      this.color = color;
   }
}

Inheritance is transitive

If C inherits B and B inherits A, then class C has non private properties and methods in classes A and B.

super(): it means to call the parameterless construction in the parent class. It is stored in by default and must be placed in the first line

this() and super() cannot appear in the same constructor at the same time, because this() and super() must be in the first line of the constructor when calling the constructor.

For example: define a xiaotianquan class to inherit Dog class. Xiaotianquan class has both dog class and Animal class's non private properties and methods.

public class XiaoTianQuan extends Dog{ 
   public XiaoTianQuan(){
       //super() means that the parameterless construction method in the parent class is called. The default storage must be placed in the first line to avoid using the method of the parent class on the super statement, so we must ensure that the parent class is initialized first
       super();
       System.out.println("Nonparametric construction method in asthmatic dogs");
   }  
   public  XiaoTianQuan(String name){
       super(name);
       System.out.println("Parametric construction method in asthmatic dogs");
   } 
   //Xiaotian dog is a divine dog. Its eating and playing are different from those in the parent class. Eating and playing in the parent class can not meet the needs of Xiaotian dog
   /*Override (override) the methods in the parent class in the child class
     Specific syntax:
        The method name is the same, the parameter list is the same, and the return value type is the same
        The access permission modifier is equal to or greater than the permission of the parent class
        @Override This is an annotation tag provided in java (a kind of mark and mark),
        The tag with this annotation indicates that this method is overridden from the parent class, and its syntax will be verified
        Override(Override overload*/
   @Override
   public void eat(){
      System.out.println("The divine dog sat and ate");
   }  
   public void play(){
       System.out.println("The magic dog is flying");
       super.eat();
   }  
   public  void fly(){
       System.out.println("A howling dog can fly");
       //super.play();  // Myth: Super represents the parent object
   }
}

new XiaoTiaoQuan(); After creating a subclass object, when calling the constructor method, it calls up and down from the top and initializes the parent class information:

Construction method in inheritance:

1. The subclass constructor will call the parent constructor first

2. Use the super keyword to call any constructor of the parent class, which must be written in the first line of the constructor

3. If the base class construction method is not explicitly called in the subclass construction method, the system will call the base class nonparametric construction method by default.

Keywords: Java Back-end

Added by Indersingh on Mon, 28 Feb 2022 18:54:14 +0200