[java foundation] inheritance (extends)

basic introduction

Inheritance in java is simply an extension of one class to another class, which is an is-a relationship. Using Employee (employee class) and Manager (manager class) as an example, Manager inherits Employee, which is an extension of Employee. Manager is a special Employee, but Employee is not Manager. They directly have an is-a relationship.
The keyword for inheritance is extends

public class Employee {

public class Manager extends Employee{


For the Employee class, we generally call it the super class, parent class or base class.
For the Manager class, we generally call it a subclass, derived class or child class
Through inheritance, the subclass will have all the fields, methods, and constructors of the parent class except private.

quick use


We define an employee class (Employee) which has name and salary fields

public class Employee {
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;

    public Employee() {

    public double getSalary() {
        return salary;

We define a manager class (Manager) to inherit the employee class (Employee), which has bonuses in addition to the basic salary, so define a bonus field

public class Manager extends Employee {
    private double bonus;
    public void setBonus(double bonus) {
        this.bonus = bonus;

override method

We can find that there is a getSalary method in the parent class, which returns the salary

    public double getSalary() {
        return salary;

It is correct for Employee, but the composition of Manager's salary is indeed a basic structure of salary + bonus. If you use Manager to call this method, it is not correct, so we have to rewrite this method and add the following content to Manager

    public double getSalary() {
        return super.getSalary() + bonus;

@Override in the above code is an annotation, which is used to check whether the coverage is successful, and it can be omitted (recommended). The super keyword represents the parent class, and super.getSalary() calls the getSalary method of the parent class. We override the getSalary method of the parent class in the subclass to return the basic salary and wages.
After writing, if we use Manager to call getSalary call, it will return the value of basic salary + salary
We can add fields, add methods, and override methods in subclasses, but we cannot delete any fields and methods! ! !

subclass constructor

We changed the getSalary method of the subclass above, and it seems to be correct. In fact, there is another problem. We cannot specify name and salary in Manager, because these two fields are private in the parent class, and no corresponding set method is provided. There is only one constructor to initialize name and salary.
Some people may think, since the parent class already has a constructor, shouldn't it be enough to use it directly? For example the following code

Manager ttpfx = new Manager("ttpfx",12000);

But the compiler will report an error when written in this way, because the Manager class does not provide a constructor for name and salary.
We need to add it in Manager.

    public Manager(String name, double salary) {
        super.name = name;
        super.salary = salary;

Do you think the code above is correct? Of course it is wrong, because both name and salary are private fields, so we can only use the constructor of the parent class to initialize

     public Manager(String name, double salary) {

super and this are actually very similar, except that one represents the parent class and the other represents the current class. Both this and super can only be used as the first statement when calling the constructor, and calling the constructor can only be used in the constructor

For the constructor, there are some instructions as follows
If the subclass constructor does not explicitly call the parent class constructor, then the parent class's no-argument constructor will be called by default, and if the parent class does not have a no-argument constructor, an error will be reported


Now we can use

    public static void main(String[] args) {
        Employee tom = new Employee("tom", 10000);
        Manager ttpfx = new Manager("ttpfx", 12000);

The output is as follows


Tags: Java jvm programming language

Posted by Solar on Fri, 03 Mar 2023 22:32:18 +0530