Detailed explanation of the Java inheritance example in the Java tutorial

  • 2020-04-01 03:15:29
  • OfStack

What is an inheritance (extends)?

Inheritance is the phenomenon of a newly defined class fetching properties and methods from an existing class. The existing class is called the parent class, and the class that gets the properties and methods from the parent class is called the subclass.

ExtendsDemo1. Java



public class ExtendsDemo1 {
    public static void main(String[] args) {
        Truck t = new Truck();
        t.size = 100;           //I don't recommend it. It is best to initialize a member variable using a constructor, or to provide set(), get() interfaces.
                                //An instance of the van class t inherits the size, color property from the car class.
                                //A truck has one more box than a car
    }   
}
class Car {                     //The car
    int size;                   //Car body size
    String color;               //color
}
class Truck extends Car {       //van
    String packingBox;          //Packing case
}

Inherited benefits

1. Improved code reuse

2. The relationship between classes creates conditions for polymorphism.

Extends format


class SubClass extends SuperClass {
 //Execute statement;
} 


The super keyword

1. The use of the 'super' keyword is the same as this

2. This represents the class reference, and super represents the parent class reference

3. When a subclass or parent class has a member with the same name, you can distinguish between super and this

SuperDemo. Java



public class SuperDemo {
    public static void main(String[] args) {
        new SubClass().showMessage();
    }   
}
class SuperClass {
    int i = 10; 
}
class SubClass extends SuperClass {
    int i = 20; 
    public void showMessage() {
        System.out.printf("super.i = %d, this.i = %dn", super.i, this.i);
    }   
}

Override a method inherited from a parent class

1. The Override operation occurs when the same method (return value, function name, parameter) appears in a subclass as the parent class.

OverrideDemo1. Java



public class OverrideDemo1 {
    public static void main(String[] args) {
        SubClass sc = new SubClass();
        sc.speak();
    }   
}
class SuperClass {
    public void speak() {
        System.out.println("SuperClass :  I am good~");
    }   
}
class SubClass extends SuperClass {
    @Override                   //Override indicates that the method shown below will be overridden, checked by the compiler, and will report an error if the Override condition is not met. This increases the security of the code to some extent
 And robustness 
    public void speak() {
        super.speak();
        System.out.println("SubClass :  I am excellent~~~");
    }   
}

2. When overriding a superclass method, subclasses are not allowed to have methods with the same name as the superclass but with different return types.

OverrideDemo2. Java



public class OverrideDemo2 {
    public static void main(String[] args) {
    }   
}
class SuperClass {
    public void f() {
    }   
}
class SubClass extends SuperClass {
    @Override
    public int f() {
        return 1;
    }   
}

3. The access permission of a method in a subclass overriding a parent class should be greater than or equal to the overridden method in the parent class

OverrideDemo3. Java



public class OverrideDemo3 {
    public static void main(String[] args) {
    }   
}
class SuperClass {
    public void f() {
    }   
}
class SubClass extends SuperClass {
    @Override
    protected void f() {            //Just change the access here to public
    }   
}

Access control character

The access control characters are: public, protected, default, private

Some require a little knowledge of packages, so wait until you get to them.


Related articles: