1 Basic introduction
When some methods of the parent class need to be declared, but you are not sure how to implement them, you can declare them as abstract methods, then this class is an abstract class
When some methods of the parent class cannot be determined, the abstract keyword can be used to modify the method, this method is an abstract method, and the class modified with abstract is an abstract class.
When an abstract method exists in a class, the class needs to be declared as abstract kind In general, abstract classes are inherited and have subclasses to implement abstract methods.
abstract class Animal { private String name; public Animal(String name) { this.name = name; } //Thinking: here eat here you achieve, it doesn't really make sense //Namely: The problem of the uncertainty of the parent class method //===> Consider designing this method as an abstract method //===> The so-called abstract method is a method that is not implemented //===> The so-called non-implementation means that there is no method body //===> When there is an abstract method in a class, the class needs to be declared as abstract class //===> In general, abstract classes are inherited and have subclasses to implement abstract methods. // public void eat() { // System.out.println("This is an animal, but I don't know what to eat.."); // } public abstract void eat() ; }
1) When a class is modified with the abstract keyword, the class is called an abstract class;
access-modifier abstract classname {
}
2) When a method is modified with the abstract keyword, the method is an abstract method;
access modifier abstract return type method name (parameter list); // no method body
3) The value of the abstract class is more in the design, after the designer has designed it, let the subclass inherit and implement the abstract class;
4) Abstract classes are the knowledge points that examiners like to ask, and are used more in frameworks and design patterns.
2 Usage details of abstract classes
1) Abstract classes cannot be instantiated;
2) Abstract classes do not necessarily have to contain abstract methods. That is, an abstract class can have no abstract method;
3) Once the class contains the abstract method, the class must be declared abstract;
- abstract can only modify classes and methods, not properties and others.
5) An abstract class can have any member [the essence of an abstract class or a class], such as: non-abstract methods, constructors, static properties, etc.;
6) An abstract method cannot have a body, that is, it cannot be implemented. As shown in the figure
7) If a class inherits the abstract class, it must implement all the abstract methods of the abstract class, unless it also declares itself as an abstract class.
8) Abstract methods cannot be modified with private, final (do not want to be rewritten by subclasses) and static, because these keywords are contrary to rewriting.
public class AbstractDetail01 { public static void main(String[] args) { //abstract class, cannot be instantiated //new A(); } } //An abstract class does not have to contain abstract methods. That is, an abstract class can have no abstract method //, there may also be a way to implement it. abstract class A { public void hi() { System.out.println("hi"); } } //Once a class contains an abstract method, the class must be declared abstract abstract class B { public abstract void hi(); } //abstract can only modify classes and methods, not properties and other class C { // public abstract int n1 = 1; }
public class AbstractDetail02 { public static void main(String[] args) { System.out.println("hello"); } } //Abstract methods cannot be modified with private, final and static, because these keywords are contrary to overriding abstract class H { public abstract void hi();//abstract method } //If a class inherits an abstract class, it must implement all abstract methods of the abstract class, unless it also declares itself as an abstract class abstract class E { public abstract void hi(); } abstract class F extends E { } class G extends E { @Override public void hi() { //This is equivalent to the G subclass implements the abstract method of the parent class E. The so-called implementation method means that there is a method body } } //The essence of an abstract class is still a class, so it can have various members of the class abstract class D { public int n1 = 10; public static String name = "Han Shunping Education"; public void hi() { System.out.println("hi"); } public abstract void hello(); public static void ok() { System.out.println("ok"); } }
3 Exercises
Write an Employee class, declared as an abstract class, containing the following three properties: name, id,salary. Provide the necessary constructor and abstract method: work(). For the Manager class, he is not only an employee, but also has the attribute of bonus. Please use the idea of inheritance, design CommonEmployee class and Manager class, require the class to provide necessary methods for attribute access, and implement work(). Prompt "Manager/Ordinary employee name at work..." OOP inheritance + abstract class.
package com.yt.abstract_; public class Test01 { public static void main(String[] args) { Manager jack = new Manager("jack", 99, 10000.0,20000); jack.work(); } } abstract class Employee { private String name; private int id; private double salary; public Employee(String name, int id, double salary) { this.name = name; this.id = id; this.salary = salary; } //Abstract method, subclasses must override public abstract void work(); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } class Manager extends Employee { private double bonus; public Manager(String name, int id, double salary, double bonus) { super(name, id, salary); this.bonus = bonus; } @Override public void work() { System.out.println("manager"+ getName()+ " at work. . . ."); } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } } class CommonEmpolyee extends Employee { public CommonEmpolyee(String name, int id, double salary) { super(name, id, salary); } @Override public void work() { System.out.println("General staff" +getName()+"at work. . ."); } }
4 Best Practices for Abstract Classes - Template Design Pattern
4.1 Basic introduction
The abstract class embodies the design of a template pattern. The abstract class is used as a general template for multiple subclasses. The subclasses are extended and transformed on the basis of the abstract class, but the subclasses generally retain the behavior of the abstract class.
1) When part of the implementation of the function is deterministic, part of the implementation is uncertain. At this time, you can expose the uncertain parts and let subclasses implement them.
2) Write an abstract parent class, the parent class provides common methods of multiple subclasses, and leaves one or more methods for its subclasses to implement, which is a template pattern.
4.2 Requirements
1. There are multiple classes to complete different tasks job
2. Require statistics to get the time to complete the task
3. Please implement programming
Emotional expression
1. First use the easiest way to think of - "code implementation
2. Analyze the problem and propose the use of template design patterns
Write a parent class, write an abstract class, and write an abstract method;
abstract public class Template { //Abstract Class - Template Design Pattern public abstract void job();//Abstract method; subclasses must override public void calculateTime() {//Implement the method, call the job method //get start time long start = System.currentTimeMillis(); job(); //Dynamic binding mechanism, first find the subclass method of the running type, if not found, find the method of the parent class //time to end long end = System.currentTimeMillis(); System.out.println("task execution time " + (end - start)); } }
Subclasses must override the classification abstract method;
public class AA extends Template { //computing task //1+....+ 800000 //There is a dynamic binding mechanism, and the running type will first find the method AA class @Override public void job() { //Implement the abstract method job of Template long num = 0; for (long i = 1; i <= 800000; i++) { num += i; } } // public void job2() { // //get start time // long start = System.currentTimeMillis(); // long num = 0; // for (long i = 1; i <= 200000; i++) { // num += i; // } // // get the end time // long end = System.currentTimeMillis(); // System.out.println("AA execution time " + (end - start)); // } }
public class BB extends Template{ public void job() {//Go here too, rewrite Template's job method long num = 0; for (long i = 1; i <= 80000; i++) { num *= i; } } }
public class TestTemplate { public static void main(String[] args) { AA aa = new AA();//Compile type AA; run memory is AA; first find the method in run type aa.calculateTime(); //There is still a need for a good OOP foundation here, for polymorphism BB bb = new BB(); bb.calculateTime(); } }