I. Use of abstractions (abstract)
When certain methods of the parent class are not deterministic, the abstract keyword can be used to decorate the method [abstraction], and abstract to decorate the class [abstract class].
We all know that the parent class is to extract the properties and methods that the subclass has in common, some of which have already been explicitly implemented, some of which cannot be determined, then we can define them as abstractions and reuse them in the later days to materialize. In this way, the abstract class is born.
For example, the "animal" parent class is defined, where the "animal name" and "Animal age" properties have been clarified, but the "animal called" method is not clear, and "animal call" can be defined as an abstract method.
Therefore, abstract classes are designed to extract the same but indeterminate things for later reuse. The purpose of defining abstract classes is to implement abstract classes in subclasses.
PackageJavastudy; Public classAbstractDemo1 { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub }}//This is an abstract classAbstract classAnimal {String name; intAge ; //animals will call Public Abstract voidCry ();//Not sure what the animal called. Defined as an abstract method to resolve the uncertainty of the parent class method. Abstract methods cannot be implemented in the parent class, so there is no body of the function. However, in subsequent inheritance, this method is implemented specifically. }//abstract classes can be inherited//When the inherited parent class is an abstract class, all the abstract methods in the abstract class need to be fully implemented. classCatextendsAnimal {//implementing the Cry abstract method of the parent class Public voidcry () {System.out.println ("The Cat called:"); }}
The class that is modified with abstract, that is, abstract, is modified by the abstract method, that is, abstraction.
Abstract methods cannot have method bodies. The format is as follows:
Abstract void xxx ();
Abstract classes cannot be instantiated. Because the methods in the abstract class are not materialized, this is an incomplete class, so direct instantiation is meaningless.
The abstract class does not necessarily include the Abstrace method. That is, there can be no abstract method in abstraction.
Vi. once the class contains an abstract method, that class must be declared as an abstract class.
Java abstract class and abstract method (abstract)