13.4 Subtypes


13.4 Subtypes

Although an interface and an abstract class cannot be instantiated, both can be used as super types for object references. This implies that interfaces and abstract classes are useful for declaring object references.

For example, refer again to Figure 13.1. Class Gfigures is an abstract class and the other classes are subclasses. An object reference can be declared of type Gfigures.

       objects            object gen_figure of class Gfigures            ... 

In a similar manner, objects of the subclasses can be declared of each of their classes. The subclasses are considered subtypes of type Gfigures. For example, the following declaration defines three object references, triangle_obj, circle_obj, and rect_obj.

       objects           object triangle_obj of class Triangle           object circle_obj of class Circle           object rect_obj of class Rectangle       ... 

The types of these object references declared are considered subtypes in the problem domain because of the original class hierarchy represented in Figure 13.1. For this organizational structure of the problem, the object reference triangle_obj is declared of type Triangle, but is also of type Gfigures. In fact, any object reference of type Triangle is also of type Gfigures. The same principle applies to the object references declared with the types Circle and Rectangle. Of course, the opposite is not true; any object reference of type Gfigures is not also of type Rectangle, Circle, or Triangle.

An interface can also be used as a super type, and all the classes that implement the interface are considered subtypes.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net