Java abstract class abstract method instructions

Knowledge points

  • What is abstract class
    Abstract classes are different from ordinary classes in two aspects:
    1. An abstract keyword is added after the modifier of the class
    2. Abstract classes are not allowed to be instantiated through new
    Because abstract classes cannot be instantiated through new, they are basically used as parent classes in inheritance.

  • What is abstract method
    Abstract keyword should be added to the definition of abstract method, and there should be no method body, ending with a seal, such as:
    public abstract void getOtherSalary();
    The concrete implementation of abstract methods needs to be implemented in subclasses. Note: static and final methods cannot be declared as abstract methods.

  • Abstract class and abstract method
    Abstract classes do not necessarily contain abstract methods, but if a class contains abstract methods, the class must be abstract, otherwise compilation fails.

  • When to use abstract methods
    Abstract methods are often used in parent classes. The parent class is usually used to define some public methods, public properties, etc. However, in the actual project, some methods may be defined in the parent class, but the parent class cannot provide specific implementation. This is the time for the abstract method to appear. For example, in the following example, the employee's income in the company includes basic salary and performance bonus. The basic salary is the same for everyone, but the performance bonus varies according to the position. Therefore, the performance bonus is only a method body in the employee category, which has no specific implementation.

  • Abstract methods and design patterns
    Abstract methods are more embodied in design patterns, especially for template method design patterns. See the following examples for details

Example

1. Use of abstract methods

Requirement Description: the employees in the company are divided into ordinary programmers and technical directors. The basic salary of all employees is 10000 yuan per month, the performance bonus of programmers is 3000 yuan per month, and the performance bonus of technical directors is 20000 yuan per month.

After understanding the requirements, we first define an Employee class, Employee, as follows:

/**
 * Define the category of employees. Each employee's salary consists of basic salary and performance bonus
 */
public abstract class Employee {

    /**
     * Post name
     */
    private String jobName;

    /**
     * All employees have the same basic monthly salary
     * @return
     */
    public int getBaseSalary(){
        return 10000;
    }

    /**
     * All employees have performance bonus, but ordinary programmers are different from technical directors,
     * So we can't give the concrete implementation here. We need to implement it in the subclass
     * @return
     */
    public abstract int getOtherSalary();

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }
}

Define the programmer class Coder, inherit the Employee class, and implement the getOtherSalary() method, as follows:

/**
 * Programmer class, inheriting Employee class
 */
public class Coder extends Employee{

    /**
     * Since the Employee class is inherited, the abstract method getOtherSalary() in the Employee class must be implemented
     * @return
     */
    @Override
    public int getOtherSalary() {
        return 3000;
    }
}

Define the technical director class Cto, inherit the Employee class, and implement the getOtherSalary() method, as follows:

/**
 * Technical director class, inheriting Employee class
 */
public class Cto extends Employee{

    /**
     * Since the Employee class is inherited, the abstract method getOtherSalary() in the Employee class must be implemented
     * @return
     */
    @Override
    public int getOtherSalary() {
        return 20000;
    }
}

Finally, print the monthly salary of programmers and technical directors:

public class AbstractMain {

    public static void main(String[] args){
        Coder coder = new Coder();
        coder.setJobName("Programmer");
        System.out.println(coder.getJobName() + "The monthly salary is:");
        System.out.println("Base salary:" + coder.getBaseSalary() + "element");
        System.out.println("Performance bonus:" + coder.getOtherSalary() + "element");

        Cto cto = new Cto();
        cto.setJobName("Technical director");
        System.out.println(cto.getJobName() + "The monthly salary is:");
        System.out.println("Base salary:" + cto.getBaseSalary() + "element");
        System.out.println("Performance bonus:" + cto.getOtherSalary() + "element");
    }
}

After executing the above procedures, the output is:

The monthly salary of programmers is:
Basic salary: 10000 yuan
 Performance bonus: 3000 yuan

The monthly salary of technical director is:
Basic salary: 10000 yuan
 Performance bonus: 20000 yuan

2. Design pattern of abstract method and template method

After the above requirements are completed, new requirements are put forward at this time. Each month's payslip shall be printed according to the following format, and the printing sequence must be printed according to the following format:

XXX's monthly salary is:
Basic salary: XXX yuan
 Performance bonus: XXX yuan
 Total salary of the month: XXX yuan

The implementation of the first example can not meet this requirement. The printing order of the first example is determined by the caller and cannot be controlled uniformly. At this time, it's time for the template method design pattern to debut. Based on the first example, change the Employee class, and add the method public final void printSalary() to print the payslip as follows:

/**
 * Define the category of employees. Each employee's salary consists of basic salary and performance bonus
 */
public abstract class Employee {

    /**
     * Post name
     */
    private String jobName;

    /**
     * All employees have the same basic monthly salary
     * @return
     */
    public int getBaseSalary(){
        return 10000;
    }

    /**
     * All employees have performance bonus, but ordinary programmers are different from technical directors,
     * So we can't give the concrete implementation here. We need to implement it in the subclass
     * @return
     */
    public abstract int getOtherSalary();

    /**
     * Get monthly payroll
     * @return
     */
    public int getTotalSalary(){

        return this.getBaseSalary() + this.getOtherSalary();
    }

    /**
     * Define execution sequence template. Some methods (abstract methods) in the template are to be implemented in subclasses
     * Print payslip, which is defined as final class, and cannot be overridden by subclass
     */
    public final void printSalary(){

        System.out.println(this.jobName + "The monthly salary is:");
        System.out.println("Base salary:" + this.getBaseSalary() + "element");
        System.out.println("Performance bonus:" + this.getOtherSalary() + "element");
        System.out.println("The total salary for the month is:" + getTotalSalary() + "element");
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }
}

Print monthly payslip:

public class AbstractMain {

    public static void main(String[] args){
        Coder coder = new Coder();
        coder.setJobName("Programmer");
        coder.printSalary();

        Cto cto = new Cto();
        cto.setJobName("Technical director");
        cto.printSalary();
    }
}

The above results are as follows:

The monthly salary of programmers is:
Basic salary: 10000 yuan
 Performance bonus: 3000 yuan
 Total salary of the month: 13000 yuan

The monthly salary of technical director is:
Basic salary: 10000 yuan
 Performance bonus: 20000 yuan
 Total salary of the month: 30000 yuan

Source code acquisition

All of the above examples are available through My GitHub Get the complete code, Click to get

Tags: Java github

Posted on Tue, 10 Mar 2020 23:04:38 -0400 by AceE