Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Sometimes, a class that you define represents an abstract concept and, as such, should not be instantiated. Take, for example, food in the real world. Have you ever seen an instance of food? No. What you see instead are instances of carrot, apple, and (our favorite) chocolate. Food represents the abstract concept of things that we all can eat. It doesn't make sense for an instance of food to exist.Similarly in object-oriented programming, you may want to model an abstract concept without being able to create an instance of it. For example, the
Number
class in thejava.lang
package represents the abstract concept of numbers. It makes sense to model numbers in a program, but it doesn't make sense to create a generic number object. Instead, theNumber
class makes sense only as a superclass to classes likeInteger
andFloat
, both of which implement specific kinds of numbers. A class such asNumber
, which represents an abstract concept and should not be instantiated, is called an abstract class. An abstract class is a class that can only be subclassed-- it cannot be instantiated.To declare that your class is an abstract class, use the keyword
abstract
before theclass
keyword in your class declaration:If you attempt to instantiate an abstract class, the compiler displays an error similar to the following and refuses to compile your program:abstract class Number { . . . }AbstractTest.java:6: class AbstractTest is an abstract class. It can't be instantiated. new AbstractTest(); ^ 1 error
An abstract class may contain abstract methods, that is, methods with no implementation. In this way, an abstract class can define a complete programming interface, thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface. However, the abstract class can leave some or all of the implementation details of those methods up to its subclasses.Let's look at an example of when you might want to create an abstract class with an abstract method in it. In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and so on. Each of these graphic objects share certain states (position, bounding box) and behavior (move, resize, draw). You can take advantage of these similarities and declare them all to inherit from the same parent object--
GraphicObject
.However, the graphic objects are also substantially different in many ways: drawing a circle is quite different from drawing a rectangle. The graphics objects cannot share these types of states or behavior. On the other hand, all GraphicObject
s must know how to draw themselves; they just differ in how they are drawn. This is a perfect situation for an abstract superclass.First you would declare an abstract class,
GraphicObject
, to provide member variables and methods that were wholly shared by all subclasses, such as the current position and themoveTo
method.GraphicObject
also declares abstract methods for methods, such asdraw
, that need to be implemented by all subclasses, but are implemented in entirely different ways (no default implementation in the superclass makes sense). TheGraphicObject
class would look something like this:Each non-abstract subclass ofabstract class GraphicObject { int x, y; . . . void moveTo(int newX, int newY) { . . . } abstract void draw(); }GraphicObject
, such asCircle
andRectangle
, would have to provide an implementation for thedraw
method.An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class.class Circle extends GraphicObject { void draw() { . . . } } class Rectangle extends GraphicObject { void draw() { . . . } }
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.