Abstract class in JAVA

/*
 * When the same function appears in multiple classes, but the function body is different
 * At this time, upward extraction can be performed. At this time, only the function definition is extracted, but the function body is not extracted.
 *
 * Features of abstract classes:
 * 1. The abstract method must be defined in the abstract class
 * 2. Both abstract methods and abstract classes must be modified by the abstract keyword
 * 3. An abstract class cannot create an object with new, because it is meaningless to call an abstract method
 * 4. To use the methods in the abstract class, the subclass must overwrite all its abstract methods, and then create the subclass object to use
 * If the subclass only overrides some abstract methods, then the subclass is still an abstract class
 *
 * Abstract classes are not much different from general classes
 * Describe things how you want to describe things, but there is something uncertain in things
 * These indeterminate parts, that is, the function of the thing, need to appear explicitly, but the subject cannot be defined
 * Represented by abstract methods
 *
 * Abstract classes have more abstract functions than general classes, that is, abstract methods can be defined in classes
 * Abstract classes cannot be instantiated
 *
 * Special; abstract methods may not be defined in an abstract class, it just prevents the class from creating objects
 *
 * */

abstract class AllStudent {
	abstract void study();
	void sleep(){
		System.out.println("sleep");
	}
}  

class BaseStudent extends AllStudent{
	void study() {
		System.out.println("base study");
	}
}

class AdvStudent extends AllStudent{
	void study() {
		System.out.println("adv study");
	}
}

In the above code, both BaseStudent and AdvStudent implement the AllStudent interface, overriding the stduy() method in the interface

Here is an example;

/*
 * Modeling of employees is required when developing a system.
 * Employee contains 3 attributes: name, job number, salary.
 * Managers are also employees, in addition to the attributes of employees, there is also a bonus attribute
 * Please use the idea of ​​inheritance to design employee classes and manager classes, and require the classes to provide the necessary methods for attribute access
 * */

abstract class Employee {
	private String emp_name; // employee name
	private int emp_no, // employee number
			emp_salary; // employee salary

	Employee(String name, int no, int salary) {
		this.emp_name = name;
		this.emp_no = no;
		this.emp_salary = salary;
	}

	public abstract void work();
}

class NomalEmployee extends Employee {
	NomalEmployee(String name, int no, int salary) {
		super(name, no, salary);
	}

	public void work() {
		System.out.println("Ordinary employees work");
	}
}

class Manager extends Employee {
	int bonus; // manager bonus

	Manager(String name, int no, int salary, int bonus) {
		super(name, no, salary);
		this.bonus = bonus;
	}

	public void work() {
		System.out.println("Manager work");
	}
}

If you don't abstract the Employee class, declare the employee class directly, and then let the manager inherit the employee class and then add the bonus attribute, there is a problem. If this is inherited, it means that the manager class will have the work() method in the employee class. However, in real life, managers only need to assign tasks to employees, and they don't need to know how to complete them, and they don't need to complete them by themselves. Therefore, we need to extract an abstract employee class from managers and employees. Both managers and employees are Employees, they all have the attributes of name, job number, salary, and work method (because the specific content of the work is different, it is defined as an abstract method, and other subclasses are implemented).


                                                                                               --------------------By chick

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325593292&siteId=291194637