Home >Java >Javagetting Started >What is an abstract class in java

What is an abstract class in java

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-11-12 17:04:065661browse

What is an abstract class in java

1. What is an abstract class?

The method modified by abstract is called an abstract method; the class modified by abstract It's called an abstract class. An abstract class cannot be instantiated because it is not a concrete class, or such a class is not complete enough to directly use the new keyword to call its constructor to generate an object of this class. We can use abstract to define an abstract class and abstract method. The sample code is as follows:

abstract class 类名
{
abstract int 方法名(int x,int y);
}

Abstract methods have no method body. It should be noted that abstract classes can have both abstract methods and ordinary methods. Note that abstract methods do not have method bodies (that is, there are no curly braces after the methods). All entity subclasses that inherit this abstract class must implement this abstract method.

Let’s summarize the characteristics of abstract classes:

(1) Abstract classes cannot be instantiated;

(2) Constructors and static methods cannot be abstract;

(3) Abstract methods of parent classes are often implemented in subclasses;

(4) Abstract classes can have object references pointing to subclass objects.

2. Examples of abstract classes

Let’s first take a look at the actual examples to be completed:

1. Define the abstract class Employee;

(a) Protected fields: basic attributes such as name, gender, age, etc.

(b) Define the abstract method function getsalary() to represent the operation of collecting wages.

(c) Define the output of the ordinary function whoami(): I am the name

(d) A constructor with (name, gender) parameters.

Define a manager class Manager derived from employees;

(a) In addition to basic attributes such as employees, there are also position-level gree private attributes.

(b) The manager receives a salary of 7,000 yuan, prints and outputs the salary.

(c) Rewrite the whoami() of the parent class, call the whoami() method of the parent class, and then output: I am a manager.

Define an accounting class Accounting, derived from the employee class;

(a) In addition to basic attributes such as employees, there is also an accounting rating private attribute.

(b) The accountant receives a salary of 3,000 yuan, prints and outputs the salary.

(c) Rewrite the whoami() of the parent class, call the whoami() method of the parent class, and then output: I am an accountant.

/**
 * 定义类员工Employee,
 */
public abstract class Employee {
    protected String name;
    protected boolean gender;
    protected int age;
    public Employee(String name,boolean gender){
        this.name=name;
        this.gender=gender;
    }
    /**
    * 表示领工资的操作
    */
    public abstract void getsalary();
    public void whoami(){
        System.out.println("我是"+name);
    }
}

We create a new manager class Manager and inherit the Employee class. At this time, Eclipse prompts us that we must override the abstract method getsalary. The sample code is as follows:

/**
 * 经理类
 */
public class Manager extends Employee{
    private String gree;
    
    public Manager(String name,boolean gender){
        super(name,gender);
    }
    //重写父类的抽象方法
    public void getsalary(){
        System.out.println("经理领7000元工资");
    }
    public void whoami(){
        super.whoami();//显示调用父类的方法
        System.out.println("我是经理");
    }
}

Next is the accounting class. It’s almost the same as the manager class above.

/**
 * 会计
 */
public class Accounting extends Employee {
    private int rating;
    public Accounting(String name, boolean gender) {
        super(name, gender);
    }
    @Override
    public void getsalary() {
        System.out.println("会计发3000工资");
    }
    public void whoami() {
        super.whoami();// 显示调用父类的方法
        System.out.println("我是会计");
    }
}

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of What is an abstract class in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is mapping in javaNext article:What is mapping in java