In a top-down inheritance hierarchy, classes on the upper level are more generic and may even be more abstract. From a certain point of view, the ancestor class is more general, it contains only some of the most basic members, people only use it as a base class to derive other classes, and not to create objects. Even, the definition of the method can be given without implementation, and the subclass is implemented according to the specific needs.
This method, which only gives the method definition but not the concrete implementation, is called abstract method, abstract method without method body (without {}), there is no "{}" in the expression of the code. A class that contains one or more abstract methods must also be declared as an abstract class. An abstract modifier is used to represent an abstraction method and an abstract class. Abstract classes can contain specific variables and specific methods in addition to abstract methods. A class can be declared as an abstract class, even if it does not contain an abstract method, to prevent it from being instantiated. Abstract classes cannot be instantiated, and abstract methods must be implemented in subclasses.
Example code:
Package abstract class; Public class abstractclass { Public Static voidMain (string[] args) {Teacher T =NewTeacher (); T.setname ("Liming"); T.work (); Student s =NewStudent (); S.setname ("Jim"); S.work (); }}//define an abstract classAbstract class people{ PrivateString name;//Use SHIFT + ALT + S to quickly generate a get, set method (right-click on source) PublicString GetName () {returnName } Public voidSetName (String name) { This. name = name; }//Define an abstract method Public Abstract voidWork ();} class Teacher extends people{ Public voidWork () {System.out.println ("I ' m teacher,name is"+ This. GetName () +"is teaching. "); }} class Student extends people{ Public voidWork () {System.out.println ("I ' m student, name is"+ This. GetName () +"is learning. "); }}
Abstract classes in Java