The definition of the Abstract class and the usage of Final!

zhaozj2021-02-16  58

Abstract

The following three situations should be stated about Abstrat

C Explicitly Contains a Declaration of an abstract method.

Class C does include a function that has been declared as a virtual function.

.

Any hypole of class C defines a virtual function and this virtual function is not particularized by class C and any of its superclars.

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.

The direct super interface or inheritance of class C (class C must of course be Abstract) and class C neither does not inherit and make it specific. ColoredPoint in the following example

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 can not 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 can not 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

- in Java document http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#35962

转载请注明原文地址:https://www.9cbs.com/read-18626.html

New Post(0)