The abstract class is declaredAbstractClass -- it can or does not contain abstract methods. Abstract classes cannot be instantiated, but they can be inherited.
An abstract class is a declared but not implemented method (without braces and trailing semicolons). For example:
abstract void moveTo(double deltaX, double deltaY);
If a class contains abstract methods, this class must be declaredAbstract, As follows:
public abstract class GraphicObject {
// declare fields
// declare non-abstract methods
abstract void draw();
}
When an abstract class is inherited, The subclass usually provides implementation for all abstract methods of the parent class. However, if not all implementations are implemented, the subclass must also be declaredAbstract.
Note: all methods of the interface are implicitly abstract, so abstract modifiers are not used in interface methods (optional or optional ).
Abstract classes and interfaces
Unlike interfaces, abstract classes can contain fields that are not static or final, and they can contain implemented methods. This abstract class is similar to an interface. The difference is that they provide partial implementation and leave it to the subclass to complete other implementations. If an abstract class only contains the declaration of abstract methods, it should be declared as an interface.
In the class hierarchy, the class can implement multiple interfaces, regardless of whether there is a connection between the two. For example, considerComparable andCloneable.
Through comparison and sharing, the abstract class is inherited most often. An abstract class is inherited by a subclass that has many similarities (partial implementation of an abstract class) but has some differences (abstract methods.
Abstract class example
Object-oriented drawing applicationProgramYou can draw a circle, a rectangle, a line, a bezr curve, and many other graphic objects. These objects all share certain states (such as position, direction, line color, fill color) and behaviors (such as moving, rotating, scaling, and painting ). Some statuses and behaviors of all graphic objects are the same, such as position, fill color, and movement. Others require different implementations, such as scaling or plotting. All graphical objects must know how to draw or scale themselves, they are different, and how they do this. This is a perfect abstract parent class. You can use similarity to declare that all graphical objects inherit from the same abstract parent object, for example, graphicobject.
Class rectangle, line, besell curve, and circle inherit graphicobject
First, you declare the abstract classGraphicobjectProvides member variables and methods, which are completely shared by all sub-classes, such as the current position and moving method.Graphicobject must also declare abstract methods, such as drawing or scaling, which must be implemented for sub-classes, but in different ways.The graphicobject class looks like this:
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
EachGraphicobjectNon-Abstract subclass of, suchCircle andRectangle, Must provideImplementation of the draw and resize methods:
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
class Rectangle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
When the abstract class implements the interface
In the interface section, all methods of interfaces must be implemented when classes implement interfaces. Even so, if the class is declared abstract, it does not need to implement all the methods of the interface. For example
abstract class X implements Y {
// implements all but one method of Y
}
class XX extends X {
// implements the remaining method in Y
}
In this case, Class X must be abstract because it does not fully implement y, while class XX implements all y.
Class Member
abstract classes can have static fields and static methods. You can access static members through class references-for example,abstractclass. staticmethod ()-Just like using other classes.