## CSDN博客

### abstract类的定义及final的用法！

abstract

• C explicitly contains a declaration of an abstract method.

类C中的确包含一个已申明为虚函数的函数。

• Any of C's superclasses declares an abstract method that has not been implemented  in C or any of its superclasses.

类C的任何一个超类定义了一个虚函数且这个虚函数没有被类C和其任何一个超类所具体化。

• A direct superinterface  of  C declares or inherits a method (which is therefore necessarily abstract) and C neither declares nor inherits a method that implements it.

类C的直接超接口或继承的函数(类C当然应该是abstract) 且类C既没定义也没继承并使之具体化。  如下例中的  ColoredPoint

In the example:

abstract class Point { int x = 1, y = 1; void move(int dx, int dy) { x += dx; y += dy; alert(); } abstract void alert(); } abstract class ColoredPoint extends Point { int color; } class SimplePoint extends Point { void alert() { } }
a class Point is declared that must be declared abstract, because it contains a declaration of an abstract method named alert. The subclass of Point named ColoredPoint inherits the abstract method alert, so it must also be declared abstract. On the other hand, the subclass of Point named SimplePoint provides an implementation of alert, so it need not be abstract.

A compile-time error occurs if an attempt is made to create an instance of an abstract class using a class instance creation expression .

Thus, continuing the example just shown, the statement:

 Point p = new Point();
would result in a compile-time error; the class Point cannot be instantiated because it is abstract. However, a Point variable could correctly be initialized with a reference to any subclass of Point, and the class SimplePoint is not abstract, so the statement:

 Point p = new SimplePoint();

would be correct.

final

A class can be declared final if its definition is complete and no subclasses are desired or required. A compile-time error occurs if the name of a final class appears in the extends clause  of another class declaration; this implies that a final class cannot have any subclasses. A compile-time error occurs if a class is declared both final and abstract, because the implementation of such a class could never be completed .

Because a final class never has any subclasses, the methods of a final class are never overridden

——在自java文档http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#35962

0 0