Java programs use abstract classes to implement abstract concepts in nature. The function of an abstract class is to organize many related classes together to provide a common class, that is, an abstract class, and the specific classes organized by it will be derived from it as its subclasses. An abstract class characterizes public behavior and transfers it to its derived classes through the inheritance mechanism. Methods defined in abstract classes are called abstract methods. These methods only have the declaration of the method header, and use a semicolon to replace the definition of the method body, that is, only the interface form of the member method is defined without specific operations. Only the redefinition of abstract member methods by a derived class actually implements operations related to the derived class.
After each subclass inherits the abstract method of the parent class, it redefines it with different statements and method bodies, forming several subclasses with the same name, the same return value, and the same parameter list, with the same purpose but certain differences in specific implementations. method. The purpose of defining abstract methods in an abstract class is to implement an interface, that is, all subclasses present a method with the same name to the outside world. An abstract class is a collection of public attributes of all its subclasses and a class that contains one or more abstract methods. One of the great advantages of using abstract classes is that you can make full use of these public properties to improve the efficiency of developing and maintaining programs. The restrictions on abstract classes and abstract methods are as follows:
(1) Any class modified with the abstract modifier is called an abstract class. Any member method modified with the abstract modifier is called an abstract method.
(2) An abstract class can have zero or more abstract methods, or it can also contain non-abstract methods.
(3) There is no need for abstract methods in abstract classes, but classes with abstract methods must be abstract classes.
(4) For abstract methods, only the method name and its type are specified in the abstract class without writing its implementation code.
(5) An abstract class can derive subclasses, and all abstract methods defined in the abstract class must be implemented in the subclasses derived from the abstract class.
(6) Abstract classes cannot create objects. The work of creating objects is implemented by subclasses derived from abstract classes.
(7) If there is an abstract method with the same name in the parent class, there cannot be another abstract method with the same name in the subclass.
(8) abstract cannot modify the same class in parallel with final.
(9) abstract cannot be used to modify the same method in parallel with private, static, final or native.
The Java language stipulates that when a class has an abstract method, the class must be declared as an abstract class.
When a subclass inherits from a parent class, if there are abstract methods in the parent class, and the subclass feels that it can implement all the abstract methods of the parent class, then the subclass must implement all the abstract methods of the parent class, such as:
/** * The subclass Dog inherits the abstract class Animal and implements the abstract method enjoy * @author gacl * */class Dog extends Animal { /** * The Dog class adds its own unique attributes*/ public String furColor; public Dog( String n, String c) { super(n);//Call the constructor of the parent class Animal this.furColor = c; } @Override public void enjoy() { System.out.println("The dog barks...." ); }}
If the abstract method in the parent class cannot be implemented by the subclass, then declare the subclass as an abstract class, such as:
/** * The subclass Cat here inherits from the abstract class Animal, and naturally inherits the abstract method enjoy() declared in the Animal class. * However, the subclass Cat feels that it is not appropriate to implement this enjoy() method by itself, so It declares itself as an abstract class, * Then, whoever implements this abstract enjoy method, and whoever inherits a subclass, then whoever implements this abstract method enjoy(). * @author gacl * */abstract class Cat extends Animal { /** * Cat adds its own unique attributes*/ public String eyeColor; public Cat(String n, String c) { super(n);//Call the parent class Animal's construction method this.eyeColor = c; }}
The subclass Cat here inherits from the abstract class Animal, and naturally inherits the abstract method enjoy() declared in the Animal class. However, the subclass Cat feels that it is not appropriate to implement this enjoy() method on its own, so it calls itself Declared as an abstract class, then whoever implements the abstract enjoy method, and whoever inherits the subclass, then implements the abstract method enjoy(). like:
/** * The subclass BlueCat inherits the abstract class Cat and implements the abstract method enjoy inherited from the parent class Cat * @author gacl * */class BlueCat extends Cat { public BlueCat(String n, String c) { super(n , c); } /** * Implements the abstract method enjoy */ @Override public void enjoy() { System.out.println("The blue cat meows..."); } }
The complete test code is as follows:
package javastudy.summary;/** * Parent class Animal * Add abstract in front of class, that is, declare it like this: abstract class Animal * In this way, the Animal class becomes an abstract class */abstract class Animal { public String name; public Animal(String name) { this.name = name; } /** * Abstract method * There is only the definition of the method, but no implementation of the method. */ public abstract void enjoy(); }/** * The subclass Cat here inherits from the abstract class Animal, and naturally inherits the abstract method enjoy() declared in the Animal class, * but the subclass Cat feels that it must be implemented by itself This enjoy() method is also inappropriate, so it declares itself as an abstract class. * Then, whoever implements this abstract enjoy method, and whoever inherits a subclass, then whoever implements this abstract method enjoy( ). * @author gacl * */abstract class Cat extends Animal { /** * Cat adds its own unique attributes*/ public String eyeColor; public Cat(String n, String c) { super(n);//Call the parent class Animal's construction method this.eyeColor = c; }}/** * The subclass BlueCat inherits the abstract class Cat, and implements the abstract method enjoy inherited from the parent class Cat * @author gacl * */class BlueCat extends Cat { public BlueCat(String n, String c) { super(n, c); } /** * Implements the abstract method enjoy */ @Override public void enjoy() { System.out.println("Blue Cat meows. .."); } }/** * The subclass Dog inherits the abstract class Animal and implements the abstract method enjoy * @author gacl * */class Dog extends Animal { /** * The Dog class adds its own unique attributes */ public String furColor; public Dog(String n, String c) { super(n);//Call the constructor of the parent class Animal this.furColor = c; } @Override public void enjoy() { System.out.println("Dog barks ...."); }} public class TestAbstract { /** * @param args */ public static void main(String[] args) { /** * After the Cat class is declared as an abstract class, the Cat class cannot be instantiated. * Because the abstract class is incomplete and missing arms and legs, the abstract class cannot be instantiated. */ //Cat c = new Cat("Catname","blue"); Dog d = new Dog("dogname","black"); d.enjoy();//Call the enjoy method BlueCat you implemented c = new BlueCat("BlueCatname","blue"); c.enjoy();//Call the enjoy method you implemented}}