Introduction to the Java language Tutorial (15): Abstract classes and abstract methods

Source: Internet
Author: User
Tags abstract define abstract

In the tutorial (12), we have this assumption. Suppose there is such a simple requirement (in fact not a requirement, just for beginners to understand and fabricated): a training center to develop an internal staff management system. The training centre currently has two departments, the technical resources department and the operations department. The staff of the Technical Resources department are responsible for lectures, called Lecturers. Business Department staff is responsible for contacting business, known as sales. In a management system, you need to manage the names, salaries, and technical orientation of all employees, and the amount of tasks you sell. .....

Based on this assumption, we designed three classes: Parent employee, Sub class sales and trainer. If we have the following statement:

Employee E=new employee ();

is completely grammatically compliant. However, from the point of view of object-oriented programming, the objects in the system must find the corresponding entities in reality. But in reality, there is no Employee class object, only sales, or trainer. So we conclude that the employee class should not be instantiated.

How do I ensure that a class is not instantiated? Just declare the class as an abstract class. You can use the abstract keyword as follows:

Public abstract class Employee {
    private String name;
    private double salary;
    TBC
}

When an employee class is declared abstract, it cannot be instantiated if the following statement appears: Employee E=new employee (); A compilation error will occur.

However, abstract classes can still be used as types, such as: Employee e=new Sales ();(the usage is described in subsequent articles.

Beginners can temporarily define abstract classes as: Classes that cannot be instantiated, and abstract classes.

Next we'll consider another question.

A method is defined in the parent class employee:

public void Setsalary (double salary,double rate) {
       this.salary = salary* (1+rate);
    }

But if the subclass sales and trainer need to implement this business logic, but the implementation is different, such as sales as follows:

public void Setsalary (double salary, double rate) {
       this.salary = salary* (1+2*rate);
    }

The trainer classes are as follows:

public void Setsalary (double salary, double rate) {
       this.salary = salary* (1+0.5*rate);
    }

So what do you do with the setsalary (double salary,double rate) in the employee class? Beginners tend to think that since subclasses do not use the parent's method body, then the declaration of the method is canceled directly. This is not feasible, the parent class is a special embodiment of the subclass, the subclass does need to implement setsalary (double salary,double rate) the logic, but the implementation is different. That is, we need a solution that embodies the common characteristics of subclasses without having to write useless method bodies, which is the abstract method. Simply declare the setsalary (double salary,double rate) method of the parent class as follows:

public abstract void Setsalary (double salary,double rate);

It should be noted that the abstract method is not a method body, the declaration of direct use;

The relationship between abstract methods and abstract classes is:

1 There is not necessarily an abstract method in an abstract class

2 classes that have abstract methods must be abstract classes

Therefore, if there is an abstract method in the parent class that inherits from a class, the subclass must implement its abstract method, or the subclass must also be declared as an abstract class.

Many beginners will not understand the role of abstract classes, especially abstract methods, feel that since even the implementation of the logic is not, why declare? Abstract classes, abstract methods are very important concepts in Java, abstract classes are the embodiment of the common characteristics of many subcategories, abstract methods are the norms of the common behavior of subclasses.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.