Java Abstract methods and classes (translated from Java tutorials)

Source: Internet
Author: User


English from

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, as shown in.


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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: