Abstract methods and abstract classes must be defined using the abstract modifier, which can only be defined as abstract classes, and abstract classes may have no abstract methods.
The rules for abstract methods and abstract classes are as follows:
Abstract class must be defined using the abstract modifier, and the abstract method must be decorated with the abstract modifier, which cannot have a method body.
The abstract method cannot be instantiated, and the instantiated work should be done by its subclasses, which only need a reference.
Abstract classes can contain member variables, methods (both common and abstract), constructors, first-class blocks, inner classes (interfaces, enumerations), 5 of components. The constructor of an abstract class cannot be used to create an instance, primarily for invocation by its subclasses.
A class that contains an abstract method, which contains a direct definition of an abstract method, or inherits an abstract parent class, but does not fully implement the abstract method that the parent class contains, or implements an interface, but does not fully implement the abstract method that the interface contains three cases can only be defined as abstract classes.
"Abstract cannot be modified with private, static, final, or native side by side with the same method
"Abstract cannot be modified in a class with final
The abstract method in the subclass cannot have the same name as the abstract method of the parent class.
"Abstract methods must be overridden by subclasses, otherwise there is no meaning.
"Parent class
Public Abstract classShape {PrivateString color; //define an abstract method for calculating perimeter Public Abstract DoubleCalperimeter ();//abstract method, cannot have method body//defines an abstract method that returns a shape Public AbstractString GetType (); //defines a shape's constructor, which is not used to create a Shape object//Instead, it is used to call the quilt class PublicShape () {} PublicShape (String color) {System. out. println ("to execute the Shape's constructor"); This. color=color; } PublicString GetColor () {returncolor; } Public voidsetcolor (String color) { This. color =color; } }
"Sub-category
Public classTriangle extends Shape {Private DoubleA; Private Doubleb; Private DoubleC; PublicTriangle (String color,DoubleADoubleBDoublec) {super (color); This. Setsides (A, B, c); } Public voidSetsides (DoubleADoubleBDoublec) { if(A>=b+c | | b>=a+c | | c>= A +b) {System. out. println ("the sum of the sides of the triangle must be greater than the third side"); return; } This. a=A; This. b=b; This. c=C; } //overriding the parent class method@Override Public DoubleCalperimeter () {returna+b+C; } @Override PublicString GetType () {return "Triangles"; }}
Public classCircle extends Shape {Private Doubleradius; PublicCircle (String color,Doubleradius) {super (color); This. Radius =radius; } Public voidSetradius (Doubleradius) { This. radius=radius; } //overriding the calculation perimeter method of the parent class@Override Public DoubleCalperimeter () {return 2* Math.PI *radius; } @Override PublicString GetType () {returnGetColor () +"round"; } Public DoubleGetradius () {returnradius; }}
"Test class
Public Static voidMain (string[] args) {Shape S1=NewTriangle ("Black",3,4,5); Shape S2=NewCircle ("Yellow",3); System. out. println (S1.gettype ()); System. out. println (S1.calperimeter ()); System. out. println (S2.gettype ()); System. out. println (S2.calperimeter ()); }
Note: Abstract cannot be used to modify a member variable, not to modify a local variable, that is, there is no abstract variable, no abstract member variable, and so on; Abstract can not be used to decorate the constructor, there is no abstract constructor, the constructor defined inside the abstraction class can only be the ordinary constructor.
java--abstract classes and abstract methods