The difference between inheriting super and combining Java inheritance and C++

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


Preface

Java inheritance is one of the three main features. Let me take a note here. Because there is a little difference from C++ inheritance, I sorted it out for the convenience of future learning. I hope you can point out the wrong places.

1. The basic concept of inheritance (is-a)

The purpose is for code reuse or class reuse. This is different from C++. The parent class can also be called a super class. The keyword is =>extends (the meaning of extension)

2. Code sample

Note: Unlike C++, inheritance in Java can only be single inheritance, that is, a subclass can only have one parent class, and a single parent class can have multiple subclasses.
Parent people

public class people {
    
    
    public int age;
    public String name;
}

Subclass student, there is an additional school

public class student extends people{
    
    
    public String school;
}

Main method

public class Main {
    
    
    public static void main(String[] args) {
    
    
        student s1=new student();

    }
}

Insert picture description here
As you can see, creating an instance of a subclass can also access the parent class, because my attributes are all public. If it is a private parent class, you can't see it.

1.Super and construction method

Every class has a constructor. If you do not explicitly create a constructor, the compiler will automatically generate a constructor with no parameters for this class.

When there is a construction method in the parent class, and the construction method has parameters, the compiler will no longer automatically generate a no-argument construction method. At this time, if you create a subclass instance, you need to explicitly call the construction of the parent class. Method ( super ), and pass the parameters, otherwise the parent class instance cannot be created, and a compilation error will occur.

Note: When creating a subclass instance, the parent class instance will be constructed first

public class student extends people{
    
    
    public String school;

    public student(int age, String name, String school) {
    
    
        super(age, name);
        this.school = school;
    }
}

2. Special knowledge points

When the subclass and the parent class have the same attributes, use the super keyword to get the attributes of the parent class;

public class student extends people{
    
    
    public String school;
    public int age=10;
    public void func(){
    
    
        System.out.println(age);
        System.out.println(super.age);
    }
 }

If the inheritance is too complicated, the maintenance cost will be very high. In order to prevent a class from being inherited, you can use the final keyword;

final class people {
    
    
    public int age=100;
    public String name;
 }

At this time, the student class cannot be inherited

3. Combination (has-a)

Composition is also for code reuse, which is also an important attribute of object-oriented. It can be understood that there are many other classes in a class

public class Homehvae {
    
    
    public Cat cat=new Cat();
    public Dog dog=new Dog();
    public student stu=new student();
}

There are cats, dogs and a student at home.

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/112899152