1. Basic concepts of abstract classes
A. Many objects with the same characteristics can be abstracted as a class, and many classes with the same characteristics and behaviors can be abstracted as an abstract class
B. Classes declared with the abstract keyword are abstracted classes
2. Rules for abstract classes
A. Abstract classes can have no abstract methods, classes with abstract methods must be abstract classes
B. Non-abstract class inheritance abstract classes must implement methods of all abstract classes
C. Abstract classes can have method implementations and properties
D. Abstract classes cannot be instantiated
E. Abstract classes cannot be declared as final: abstract classes can be inherited by subclasses but final classes cannot be inherited by subclasses
F. An abstract class can have a constructor that shows but does not instantiate the object because the child class invokes the constructor of the parent class when it is instantiated (calling a constructor method is not necessarily an instantiated object)
Example:
1 Public classAbstracttest {2 Public Static voidMain (string[] args) {3Goddess G =Newgoddess ();4G.setname ("Tong Tong");5 G.say ();6System.out.println ("--------------------------");7Uglywomen U =Newuglywomen ();8U.setname ("Small Buns");9 U.say ();Ten } One } A - //Women - //Abstract represents the declaration of an abstraction class the Abstract classwomen{ - PrivateString name; - PublicString GetName () { - returnname; + } - Public voidsetName (String name) { + This. Name =name; A } at - Public Abstract voidSay ();//methods of abstract classes are not specific (only declaration implementations) - Public Abstract voidmethod (); - } - - classGoddessextendswomen{//a class-inheriting abstract class must implement all methods in an abstract class in //abstract methods that implement the parent class do not have an abstract adornment - Public voidsay () { toSystem.out.println ("I am the goddess, I am" +getName ()); + } - Public voidMethod () {}//NULL Implementation the } * $ classUglywomenextendswomen{Panax Notoginseng //abstract methods that implement the parent class do not have an abstract adornment - Public voidsay () { theSystem.out.println ("I am an ugly girl, I am" +getName ()); + } A Public voidmethod () {} the}
Java abstract class