- Abstract classes, like normal classes, have Constructors. There are attributes in an abstract class that can be initialized with a constructor method. When a subclass object is instantiated, the construction of the abstract class is performed before the subclass Constructs.
- Abstract classes cannot be declared with final . Abstract classes cannot use the final definition because abstract classes must have subclass inheritance .
- an abstract class cannot be declared with static, but an inner class in an abstract class can use the static Declaration.
Abstract classa{Static Abstract classB { public Abstract voidPrint (); }}classXextendsa.b{ public voidprint () {System.out.println ( this. GetClass ()); }} public classhello{ public Static voidMain (string[] Args)throwsException {a.b AB=NewX (); Ab.print (); }}
Output: class X
4. Abstract classes, like normal classes, can have static methods and can be passed through the class name . method name is called in the way
Abstract class a{ publicstaticvoid test () { System.out.println (" static method in the abstract class ");} } public class hello{ publicstaticvoid main (string[] Args) throws Exception { a.test (); }}
5. Sometimes abstract classes require only a specific system subclass operation, which can hide the implementation of an abstract class Externally. Such a design is common in the System class library in order to hide the subclass implementations that we do not need.
Abstract classa{ public Abstract voidPrint (); Private Static classBextendsA { public voidprint () {System.out.println ("print message test"); } } public StaticA getinstance () {return NewB (); }} public classhello{ public Static voidMain (string[] Args)throwsException {a a=a.getinstance (); A.print (); }}
6. Observe the output below.
Abstract classa{ publicA () { this. Print (); } public Abstract voidPrint ();}classBextendsa{Private intnum = 100; publicBintNum) { Super(); this. num =num; } public voidprint () {System.out.println ( this. num); }} public classhello{ public Static voidMain (string[] Args)throwsException {b b=NewB (30); }}
Above code output: 0
Some summaries of abstract classes in Java