Java inheritance (extends)

Table of Contents

1. Understanding of inheritance

2. Grammar format

Three, examples are used to understand

Fourth, subclasses extend attributes and methods based on parent classes

5. Override the methods inherited by the subclass from the parent class

6. Inheritance and Construction

7. final keyword

8. Abstract class (abstract modification)


One, the understanding of inheritance

First introduce two concepts, parent class and subclass. The subclass has all the attributes and methods of the parent class.

Inheritance operation is to connect two classes through the relationship between parent class and child class.

Two, syntax format

class parents{//This is the parent class
    int age;
    String name;
}
class son extends parents{//Subclass extends (inherit) parent class
    //At this time, there are no attributes and methods in the son class
    //But the object instantiated with the son class must have all the properties and methods of the parents class
}

Three, examples are used to understand

①Create an Animal class and a Dog class. After the Dog and Animal classes are connected with extends, the object d instantiated with the Dog class will have the attributes in the Animal class.

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.age = 12;
        d.name = "Sheepdog";
        System.out.println("Name is: " + d.name + ", age is: " + d.age);
    }
}
class Animal{//This is the parent class
    int age;
    String name;
}
class Dog extends Animal{//Subclass extends (inherits) parent class

}

② In subclasses, only direct access to non-private properties and methods in the parent class is allowed. In other words, the properties and methods modified by private in the parent class cannot be directly accessed in the subclass, but they can be accessed indirectly using getter and setter methods.

class Animal{//This is the parent class
    private int age;
    public int getAge(){
        return age;
    }
    String name;
}
class Dog extends Animal{//Subclass extends (inherits) parent class
    String color;
    void print(){
        System.out.println(age);//Directly access the private properties in the parent class, error!
        System.out.println(name);//name in the parent class has default permission and can be accessed
    }
}

Four, subclasses extend attributes and methods on the basis of the parent class

The subclass already has all the attributes and methods of the parent class. Extend the attributes and methods of the subclass to make the objects described by the subclass more specific.

as follows. The object instantiated by the Dog class will first have two attributes of the Animal class, and on this basis, one attribute will be expanded.

class Animal{//This is the parent class
    int age;
    String name;
}
class Dog extends Animal{//Subclass extends (inherits) parent class
    String color;
}

Five, override the methods that the subclass inherits from the parent class Override

In Java, subclasses can override methods of parent classes, but the following conditions must be met:

  1. The method name and parameter list must be exactly the same as the parent class method.
  2. The return type must be the same as the parent class method or its subclass.
  3. Access permissions cannot be more restrictive than parent class methods (for example, if a parent class method is public, then a method overridden by a child class must also be public).

① A subclass inherits the methods of the parent class and can override the inherited method. After rewriting, when the method is called with an object instantiated by the subclass, the method rewritten in the subclass will be used instead of the method in the parent class. Methods

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.shout();
    }
}
class Animal{//This is the parent class
    int age;
    String name;
    void shout(){
        System.out.println("A strange noise was made");
    }
}
class Dog extends Animal{//Subclass extends (inherits) parent class
    String color;
    void shout(){
        System.out.println("There was a barking sound");
    }
}

② When rewriting methods, please note that the permissions of the rewritten method in the subclass must not be lower than the permissions of the original method in the parent class! The following are error examples!

class Animal{//This is the parent class
    int age;
    String name;
    public void shout(){//This method of the parent class has public permissions
        System.out.println("A strange noise was made");
    }
}
class Dog extends Animal{//Subclass extends (inherits) parent class
    String color;
    private void shout(){//The permission of this method in the subclass is private, which is lower than the public in the parent class, error! ! !
        System.out.println("There was a barking sound");
    }
}

Six, Inheritance and Construction

① When instantiating an object using a subclass, the parameterless constructor in the parent class will be called by default (equivalent to an implicit super())

public class Main {
    public static void main(String[] args) {
        Son s = new Son();//The instantiated object of the subclass will definitely call the parameterless constructor in the parent class
    }
}
class Parent{//This is the parent class
    Parent(){
        System.out.println("The parameterless constructor in the parent class is called");
    }
}
class Son extends Parent{//Subclass extends (inherits) parent class

}

②super keyword

Use super(parameter 1, parameter 2, parameter 3…) to explicitly call the constructor in the parent class. The parameter list here corresponds to the parameter list of the constructor you want to call in the parent class.

Different from this(), this() is used to call the B constructor in the A constructor in the same class

class Parent{//This is the parent class
    int m_a;
    Parent(){
        System.out.println("The parameterless constructor in the parent class is called");
    }
    Parent(int m_a){
        System.out.println("The parameterized constructor in the parent class is called");
        this.m_a = m_a;
    }
}
class Son extends Parent{//Subclass extends (inherits) parent class
    Son(){
        super(1027);
    }
}

Seven, final keyword

  1. Modified variables: If a variable is declared final, then it cannot be reassigned (=). That is, it is a constant whose value cannot be changed once it is set.
  2. Modified class: If a class is declared as final, then this class cannot be inherited (extends). That is, it is a final class and cannot have subclasses.
  3. Modified methods: If a method is declared as final, then this method cannot be overridden. That is, it is a final method and cannot be overridden by methods in other classes.
  4. Modified attributes: If a class attribute is declared as final, then this attribute cannot be inherited or modified. That is, it is a final property and cannot be overridden or modified by properties in other classes.
class Animal{
    public final void shout(){

    }
}
class Dog extends Animal{
    public void shout(){//Override the final modified method in the parent class, error! ! !

    }
}

Eight, abstract class (abstract modification)

portal

Classes modified with abstract are abstract classes

An abstract class is a class that cannot be instantiated and is usually used as a base class for other subclasses. A class modified with the abstract keyword indicates that the class is an abstract class. Abstract classes can contain abstract methods, non-abstract methods and fields. An abstract method is a method without specific implementation, only method declaration but no method body.

A subclass of an abstract class must implement all abstract methods before it can be instantiated. If the subclass does not implement all abstract methods, then the subclass is still an abstract class and cannot instantiate objects!

be careful:

① A class containing an abstract method must be an abstract class. That is to say, only a class modified with abstract can have an abstract method modified by abstract.

②Abstract methods cannot have method bodies. That is to say, abstract methods of abstract classes can only be inherited to subclasses and implemented by subclasses.

③The subclass must implement all abstract methods of the parent class before it can instantiate the object, otherwise the subclass will still be an abstract class.

④Abstract classes must not be modified with final, because final-modified classes cannot be inherited, which conflicts with point ③

abstract class Animal{
    abstract void shout();//Abstract method
}
class Dog extends Animal{
    void shout(){//The subclass inherits the abstract method of the parent class and implements it
        System.out.println("woof woof");
    }
}