Java---inheritance, abstraction (super, override, abstract)

inherit:

Let the relationship between the class and the class. Subclasses can directly use the non-private members of the parent class

When repetitive content appears in many types, extract the repetitive content and make it into a parent class, so that the child class inherits the parent class.

Benefits of inheritance:

Improved code reusability

Improved code maintainability

Let the relationship between the class and the class is the premise of polymorphism

Disadvantages of inheritance:

Inheritance is invasive

Reduced code flexibility

Enhance the coupling of the code

Inherited characteristics:

Java only supports single inheritance, does not support multiple inheritance, supports multiple inheritance

Features of inherited member variable access:

Access a variable in a subclass method

Find the local scope of the subclass, find the member scope of the subclass, find the member scope of the parent class

Note: If a member variable with the same name appears in the child parent class, through the principle of proximity, the child class will be used first

​ If you must use the parent class, you can use the super keyword to distinguish.

The usage of the super keyword is similar to the usage of this keyword

this: represents a reference to this class of object

super: represents the identity of the parent class storage space (can be understood as a parent class object reference)

Case:

public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        Dog d = new Dog();
        d.getName();
    }
}
class SuperAnimal{
    
    
    String name = "SuperAnimal";
    public void getName(){
    
    
        System.out.println(name);
    }
}
class Animal extends SuperAnimal{
    
    
    String name = "小花";
}
class Dog extends Animal{
    
    
    String name = "小黑";
    public void getName(){
    
    
        String name = "小黄";

        System.out.println(name);//局部变量
        System.out.println(this.name);//成员变量
        System.out.println(super.name);//父类成员变量
    }
    
}

运行结果:
小黄
小黑
小花
Member method:

super.method name()

Method override: override

In the inheritance relationship, the same method appears

The difference between method rewriting and overloading:

Method override: Override

When it occurs in the inheritance system, the subclass has exactly the same methods as the parent class. The relationship between these two methods is called "method overriding"

Features: In the child and parent classes, the method life is exactly the same

Method overload: Overload

Occurs in the same class, there are "same name with different parameters" methods, then these methods constitute method overload

Note for method rewriting:

Private methods in the parent class cannot be overridden

Parent class static method, subclass must be rewritten by static method, parent class is non-static method, subclass must be rewritten by non-static method

When the subclass overrides the parent class method, the access permissions must be greater than the parent class

Permission modifier:

private: private, out of this class, no other classes can be accessed

Write nothing: only in the same package

protected: access to subclasses of different packages

public: the most powerful, public, accessible from anywhere

Access characteristics of members in inheritance:

Member variables: the principle of proximity
Member method: principle of proximity

Access characteristics of subclass construction method

The subclass executes the construction method, and the parent class construction method is executed first

The subclass executes the parent class's no parameters by default. If there is no no parameter, it will not be executed.

If the parent class does not have no parameters, there are parameters, and you want to execute with parameters

1. The subclass manually invokes the parameter construction method of the parent class through super ///super (parameter)

2. The subclass calls the construction method of this class through this, and finally calls the parent class construction method

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Cat c1 =new Cat();
        c1.setName("加菲");
        c1.setColor("花色");

        Cat c2 = new Cat("英短","黑白");

        System.out.println(c1.getName()+c1.getColor());
        System.out.println(c2.getName()+c2.getColor());
    }
}
class Cat extends Animal{
    
    
    public Cat() {
    
    
    }

    public Cat(String name, String color) {
    
    
        super(name, color);
    }
}
class Animal {
    
    
    String name;
    String color;

    public Animal() {
    
    
    }

    public Animal(String name, String color) {
    
    
        this.name = name;
        this.color = color;
    }

    public String getName() {
    
    
        return name;
    }

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

    public String getColor() {
    
    
        return color;
    }

    public void setColor(String color) {
    
    
        this.color = color;
    }
}

Abstract class (abstract):

Abstract method: After extracting the common behavior (method) to the parent class, it is found that the implementation logic of the method cannot be specified in Frey, and the method can be defined as an abstract method.

Abstract class: If there are abstract methods in a class, the name of the class must be declared as an abstract class.

public class TestDemo {
    
    
    public static void main(String[] args) {
    
    
        Cat c = new Cat();
        c.eat();
        
        Dog d = new Dog();
        d.eat();
    }
}
class Cat extends  Animal{
    
    
  public void eat(){
    
    
        System.out.println("猫吃鱼");
    }
}
class Dog extends  Animal{
    
    
    public void eat(){
    
    
        System.out.println("狗吃肉");
    }
}
abstract class  Animal{
    
    
    String name;
    int age;

    public void drink(){
    
    
        System.out.println("喝水");
    }
    //抽象方法特点
    //1.抽象方法没有方法体
    //2.抽象方法所在的类必须是抽象类
    //3.抽象方法的子类必须重写子类的所有抽象方法
    public abstract void eat();
}

Precautions:

1. Abstract classes are not allowed to be instantiated (create objects)

2. There are construction methods in abstract classes

3. Subclass of abstract class: all abstract methods in the parent class must be rewritten

4. The method in the abstract class can have no abstract method, but the class with abstract method must be an abstract class

Guess you like

Origin blog.csdn.net/qq_42073385/article/details/108017849