Abstract With keywordsThe modified class is an abstract class. It is a set of public attributes (data) and behavior methods of all sub-classes. The abstract class does not have a specific object and is a class that exists in the hierarchy of organizational concepts. The following example illustrates why abstract classes are required.
To describe various species of birds, such as pigeons, sparrows, peacocks, Phoenix, etc., from the perspective of concept and organizational hierarchy, A bird with no specific instance can be abstracted from these specific birds. Define the common characteristics of all specific birds. Then, by inheriting birds, you can derive sub-classes such as pigeons, Sparrow, peacock, and Phoenix. In this way, you only need to describe the characteristics of various birds, rather than the common characteristics of various specific birds, this hierarchical organization makes the various bird concepts clear, clear, simple, effective, and easy to expand. The bird here only exists as an abstract concept, representing the common attributes of all birds,Any specific bird is a subclass object formed by the characteristics of birds and birds.
The Methods Modified with the keyword abstract are abstract methods (without abstract variables). Abstract METHODS only have method headers, followed by a semicolon (;), and there is no method body, the method body of an abstract method can be implemented in its subclass according to the specific situation of the subclass. The abstract method only exists in the abstract class or interface.
// Figure. Java
Public abstract class figure
...{
Protected Double X;
Figure ()
...{}
Figure (double X1)
...{
X = x1;
}
Abstract Public double area ();
Public String tostring ();
...{
Return "x =" + X;
}
}
// Circle. Java
Public class circle extends figure // circle class
...{
Public circle (double X1)
...{
Super (X1 );
}
Public double area () // implements the abstract method of the parent class and calculates the circular area.
...{
Return 3.1415926 * x * X;
}
Public String tostring ()
...{
Return "circle:" + super. tostring () + "AEA" + area ();
}
}
// Rectangel. Java
Public class rectangle extends figure
...{
Protected Double Y;
Public rectange ()
...{}
Public rectange (doule A, double B)
...{
Super ();
Y = B;
}
Public double area () // implements the abstract method of the parent class and calculates the rectangular area.
...{
Return x * Y;
}
Public String tostring ()
...{
Return "rectangle:" + super. tostring () + "Y =" + Y + "area" + area ();
}
}
// Testfigure. Java
Public class testfigure
...{
Public static void main (string [] ARGs)
...{
Rectangle R1 = new rectangle (10.0, 20.0 );
Circle C1 = new circle (10.0 );
System. Out. println (r1.tostring ());
System. Out. println (c1.tostring ());
}
}
Declared as finalThe final class cannot be a parent class, that is, the final does not derive child classes, and can be used to modify variables and methods.
The final class is the underlying class of the class. If you do not need to dispatch a Child class, the final keyword is usually used to modify it, indicating that it is the final class, classes modified by final are usually fixed classes that complete certain standard functions, such as math in the Java library.
Abstract and final cannot be used at the same time. The keyword abstract cannot modify instance variables, and cannot simultaneously modify a member method with private, static, final, and so on. abstract classes must be in abstract classes. The static modifier can only modify static member variables, and the static modifier cannot reference this.