MySQL

Page 301

284

OBJECT-ORIENTED FEATURES OF THE SQL: 1999 STANDARD

Such classes are known as abstract, or virtual, classes. In contrast, classes from which objects are created are known as concrete classes. Note: Many computer scientists use the verb "instantiate" to mean "creating an object from a class." For example, you could say that abstract classes are never instantiated. However, this author finds that term rather contrived (although not quite as bad as saying "we now will motivate the code" to mean "we will now explain the code") and prefers to use the more direct "create an object from a class." Inheriting Methods: Polymorphism. In general, methods are inherited by subclasses from their superclass. A subclass can use its base class's methods as its own. However, in some cases it may not be possible to write a generic method that can be used by all subclasses. For example, assume that the ski rental shop's MERCHANDISEITEM class has a method named PRINTCATALOGENTRY,the intent of which is to print a properly formatted catalog entry for each distinct type of merchandise item. The subclasses of MERGHANDISE ITEM, however, have attributes not shared by all subclasses, and the PRINTCATALOGENTRY method therefore must work somewhat differently for each subclass.

To solve the problem, the ski rental shop can take advantage of polymorphism, the ability to write different bodies for methods of the same name that belong to classes in the same inheritance hierarchy. The MERCHANDISE ITEM class includes a prototype for the PRINTCATALOGENTRYmethod, indicating just the method's public interface. There is no body for the method, no specifications of how the method is to perform its work (a virtual method). Each subclass then redefines the method, adding the program instructions necessary to execute the method. The beauty of polymorphism is that a programmer can expect methods of the same name and same type of output for all the subclasses of the same base class. However, each subclass can perform the method according to its own needs. Encapsulation hides the details from all objects outside the class hierarchy.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.