Tip: the following is the main content of this article, and the Java series learning will be continuously updated
(1) Basic concepts
abstract class: a superset of ordinary classes, but with more abstract methods than ordinary classes. For a class decorated with the abstract keyword, an abstract class cannot instantiate an object.
public abstract class TestAbstract {} TestAbstract t = new TestAbstract(); (X)Cannot instantiate object
abstract method: refers to a method that cannot be implemented concretely, that is, it cannot have a method body {}, and is modified with the abstract keyword;
Access modifier abstract void show();
(2) Precautions
1. Abstract classes can have member variables, member methods and construction methods;
2. Abstract classes can have abstract methods or not; However, a class with abstract methods must be an abstract class and must be decorated with the abstract keyword;
3. The subclass of an abstract class can be a normal class (the subclass must override all abstract methods of the parent class) or an abstract class.
4. A subclass can only inherit one abstract class.
(3) Practical significance
the meaning of an abstract class is not to instantiate an object, but to be inherited. If a class inherits from an abstract class, the abstract method must be overridden. Otherwise, the class has to become an abstract class. Ensure the correct operation of polymorphism.
therefore, abstract classes are mandatory and normative for subclasses, which is called template design pattern.
Experience sharing:
in future development, it is recommended to use the object form of parent class reference pointing to child classes, because the method directly called by parent class reference must be the method owned by the parent class. When you need to change the object pointing to the subclass, you only need to change the type after new. Other codes do not need to be changed, so the maintainability and scalability of the code are improved.
The disadvantage of this method is that the parent class reference cannot directly call the methods unique to the child class. If it is called, forced type conversion is required.
(4) Case
① user defined CaffeineDrinks abstract class, including large methods for making drinks and small methods / abstract methods for each process.
② the custom Coffee, Tea and MilkTea classes inherit the CaffeineDrinks class to override the abstract methods in the parent class.
③ customize the TestCaffeineDrinks class and use polymorphic syntax in the main() method to create objects for testing.
CaffeineDrinks parent class
/** * Abstract: caffeine drinks */ public abstract class CaffeineDrinks { // The production process is standardized and encapsulated in the parent class // Subclasses only have usage rights and cannot be modified // The production process of drinks is modified with final, indicating that it cannot be overwritten public final void prepareDrinks() { //Boil water boilWater(); //Brew brew(); //Pour into a cup pourInCup(); if(isCustomerWantsCondiments()) { //Add seasoning addCondiments(); } } // For soaking and seasoning, the implementation details of different subclasses are different, and the implementation is delayed to subclasses /** Abstract method**/ //Different brewing methods public abstract void brew(); //Different seasonings added public abstract void addCondiments(); //Boil water public void boilWater() { System.out.println("boiling water"); } //Pour into a cup public void pourInCup() { System.out.println("pouring into cup"); } //Whether the customer adds seasoning. It is added by default public boolean isCustomerWantsCondiments() { return true; } }
Coffee subclass
/** * Starbucks coffee making */ import java.util.Scanner; public class Coffee extends CaffeineDrinks { Scanner input = new Scanner(System.in); //Override brewing methods in abstract classes @Override public void brew() { System.out.println("brew coffee bag"); } //Override the seasoning method in the abstract class @Override public void addCondiments() { System.out.println("add sugar and milk"); } //Override the seasoning method in the abstract class @Override public boolean isCustomerWantsCondiments() { System.out.print("Do you add sugar and milk to your coffee? y Means plus, n Means no:"); String str = input.nextLine(); if (str.equals("y")) { return true; } return false; } }
MilkTea subclass
/** * Milk tea making */ public class MilkTea extends CaffeineDrinks { //Override brewing methods in abstract classes @Override public void brew() { System.out.println("brew tea and coffee bag"); } //Override the seasoning method in the abstract class @Override public void addCondiments() { System.out.println("add milk and sugar"); } }
Tea subclass
/** * Starbucks tea making method */ public class Tea extends CaffeineDrinks { //Override brewing methods in abstract classes @Override public void brew() { System.out.println("steep tea bag"); } //Override the seasoning method in the abstract class @Override public void addCondiments() { System.out.println("add lemon"); } }
TestCaffeineDrinks test class
/** * Test class of abstract method */ public class TestCaffeineDrinks { public static void main(String[] args) { //Coffee making CaffeineDrinks coffee = new Coffee(); System.out.println("Coffee making:"); coffee.prepareDrinks(); System.out.println("--------------"); //Tea making CaffeineDrinks tea = new Tea(); System.out.println("Tea making:"); tea.prepareDrinks(); System.out.println("--------------"); //Making of milk tea CaffeineDrinks milkTea = new MilkTea(); System.out.println("Making of milk tea:"); milkTea.prepareDrinks(); } }
(5) Operation results
Summary:
Tip: here is a summary of the article:
The above is today's learning content. This paper continues the object-oriented learning of Java, learning abstract methods and abstract classes. The learning content will be updated continuously!!!