[Java note 8] object oriented 3 (inheritance)

catalogue

Characteristics of inheritance

After inheritance:

Access characteristics of member variables and member methods

Method rewrite

Characteristics of subclass constructors

A subclass constructor accesses a parameterized constructor of the parent class

Use of this and super

Video tutorial portal - >   https://www.bilibili.com/video/BV1Cv411372m?p=100

Inheritance is that Java allows the extension keyword to establish a parent-child relationship between one class and another.
Benefits: improve code reusability, reduce code redundancy, and enhance the functional scalability of classes.
eg:
public class Student extends People {}

Inherited design specifications:
-Subclasses have the same characteristics (common attributes, common methods) defined in the parent class
-The properties and behaviors unique to the subclass should be defined in the subclass itself

Characteristics of inheritance

Subclasses can inherit the properties and behaviors of the parent class, but subclasses cannot inherit the constructor of the parent class.
Java is a single inheritance mode: a class can only inherit one direct parent class.
All classes in Java are subclasses of the Object class.

1. Can a subclass inherit the constructor of the parent class?
No, the subclass has its own constructor, and the parent constructor is used to initialize the parent object.

2. Can a subclass inherit private members of the parent class? (disputed)
Yes, but you can't access it directly.

3. Can a subclass inherit the static members of the parent class? (disputed)
Can use (share), but not get.

After inheritance:

Access characteristics of member variables and member methods

Accessing members (member variables and member methods) in subclass methods meets the proximity principle
Subclass local range finding - > subclass member range finding - > parent member range finding - > error if not found

If there are members with duplicate names in the child parent class, the child class will be used first. If you must use the members of the parent class in the child class? - > Super keyword
Format: super. Parent member variable / parent member method

[example] Wolf - >   Animal

package com.test.d10_extends_field_method;

public class ExtendsDemo {
    public static void main(String[] args) {
        Wolf w = new Wolf();
        System.out.println(w.name); // Subclass
        w.showName();
    }
}

class Animal{
    public String name = "Parent animal";
}

class Wolf extends Animal{
    public String name = "Subclass";

    public void showName(){
        String name = "Local name";
        System.out.println(name); // Local
        System.out.println(this.name); // Subclass name
        System.out.println(super.name); // Parent class name
    }
}

Output is:

Subclass
Local name
Subclass
Parent animal

Method rewrite

A subclass writes the same method as the parent class declaration to override the parent class's method.

Application scenarios for method Rewriting:
When a child class needs the function of the parent class, but the function of the parent class does not fully meet its own needs.

The @ Override annotation is recommended for method rewriting to verify whether the rewriting is correct and readable.

[example] define a Voice class, then a Voicetext subclass, and override the method sendvoice()

Voice.java

package com.test.d11_extends_methodoverride;

public class Voice {
    public void sendvoice(){
        System.out.println("Send voice!@#$%^&*");
    }
}

Voicetext.java

package com.test.d11_extends_methodoverride;

public class Voicetext extends Voice{
    /**
     Method override
     */
    @Override
    public void sendvoice(){
        super.sendvoice();
        System.out.println("Language to text OO");
    }
}

Execute the test class VoiceDemo.java

package com.test.d11_extends_methodoverride;

public class VoiceDemo {
    public static void main(String[] args) {
        Voicetext message = new Voicetext();
        message.sendvoice();
    }
}

Output is:

Send voice! @# $%^&*
Language to text OO

Method rewrite considerations:
The name and parameter list of the overridden method must be consistent with the name and parameter list of the overridden method
When a subclass overrides a parent class method, the access permission must be greater than or equal to the parent class (default < protected < public)
Subclasses cannot override private or static methods of the parent class

Characteristics of subclass constructors

By default, all constructors in the subclass will access the parameterless constructor in the parent class before executing their own.
The first line of the subclass constructor defaults to: super(), which exists even if it is not written.

[example] shark - > animal

Animal.java

package com.test.d12_extends_constructor;

public class Animal {
    public Animal(){
        System.out.println("==Parent class Animal The parameterless constructor is executed===");
    }
}

Shark.java

package com.test.d12_extends_constructor;

public class Shark extends Animal{
    public Shark(){
        super(); // By default, you can write or not. The default is to find the parent class nonparametric constructor
        System.out.println("==Subclass Shark The parameterless constructor is executed===");
    }

    public Shark(String n){
        super(); // By default, you can write or not. The default is to find the parent class nonparametric constructor
        System.out.println("==Subclass Shark"+"/"+n+"/"+"A parameter constructor is executed===");
    }
}

Execute test class Test.java

package com.test.d12_extends_constructor;

public class Test {
    public static void main(String[] args) {
        Shark c = new Shark();
        System.out.println("//");
        Shark c1 = new Shark("BLAHAJ");
    }
}

From the output, we can see that the subclass will access the parameterless constructor of the parent class by default

A subclass constructor accesses a parameterized constructor of the parent class

super calling parent class has the function of parameter constructor = >   Initializes data inherited from the parent class.

If there is no parameterless constructor in the parent class and only a parameterless constructor, an error will be reported. Because the subclass calls the parent class parameterless constructor by default. How to solve? = >
In the subclass constructor, you can manually call the parameterized constructor of the parent class by writing super(...)

[example] shark - > animal calls the parameterized constructor of the parent class

Animal.java

package com.test.d13_extends_constructor2;

public class Animal {
    private String name;
    private int age;

    public Animal() {
    }

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

    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;
    }
}

Shark.java

package com.test.d13_extends_constructor2;

public class Shark extends Animal {
    private String oceanName;

    public Shark(){
    }

    public Shark(String name, int age, String oceanName) {
        super(name, age);
        this.oceanName = oceanName;
    }

    public String getOceanName() {
        return oceanName;
    }

    public void setOceansName(String oceanName) {
        this.oceanName = oceanName;
    }
}

Execute test class Test.java

package com.test.d13_extends_constructor2;

public class Test {
    public static void main(String[] args) {
        Shark s = new Shark("Shark shark", 3, "Atlantic");
        System.out.println(s.getName()+s.getAge()+"Years old~stay"+s.getOceanName()+"Swim and swim happily~");
    }
}

Output: shark is 3 years old ~ swimming happily in the Atlantic~

Use of this and super

this  -> References to objects of this class
Super - > identification of the storage space of the parent class

[example] if Shark gives only one parameter, use this() to pass the default value of another parameter

Shark.java

package com.test.d14_this;

public class Shark {
    private String name;
    private String oceanName;

    public Shark() {
    }

    public Shark(String name) {
        // Borrow brother constructor!
        this(name, "arctic ocean");
    }


    public Shark(String name, String oceanName) {
        this.name = name;
        this.oceanName = oceanName;
    }

    public String getName() {
        return name;
    }

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

    public String getOceanName() {
        return oceanName;
    }

    public void setOceanName(String oceanName) {
        this.oceanName = oceanName;
    }
}

Test.java

package com.test.d14_this;

public class Test {
    public static void main(String[] args) {
        Shark s1 = new Shark("Shark 1", "the pacific ocean");
        System.out.println(s1.getName()+"come from"+s1.getOceanName());

        Shark s2 = new Shark("Shark 2");
        System.out.println(s2.getName()+"come from"+s2.getOceanName());
    }
}

Output:

Shark 1 is from the Pacific Ocean
Shark 2 is from the Arctic Ocean

Keywords: Java

Added by horizontal on Sun, 21 Nov 2021 07:13:24 +0200