Page 279 - DLIS402_INFORMATION_ANALYSIS_AND_REPACKAGING
P. 279
Information Analysis and Repackaging
Notes Abstract classes can be created, signified, or simulated in several ways:
By use of the explicit keyword abstract in the class definition, as in Java, D or C#.
By including, in the class definition, one or more methods (called pure virtual functions in C++),
which the class is declared to accept as part of its protocol, but for which no implementation is
provided.
By inheriting from an abstract type, and not overriding all missing features necessary to complete
the class definition.
In many dynamically typed languages such as Smalltalk, any class which sends a particular method
to this, but doesn’t implement that method, can be considered abstract. (However, in many such
languages, the error is not detected until the class is used, and the message returns results in an
exception error message such as “does Not Understand”).
Example: abstract class demo {abstract void sum(int x, int y); }
13.2 Use of Abstract Types
Abstract types are an important feature in statically typed OO languages. They do not occur in
languages without sub typing. Many dynamically typed languages have no equivalent feature
(although the use of duck typing makes abstract types unnecessary); however traits are found in
some modern dynamically-typed languages.
Some authors argue that classes should be leaf classes (have no subtypes), or else be abstract.
Abstract types are useful in that they can be used to define and enforce a protocol; a set of operations
which all objects that implement the protocol must support.
13.3 Types of Abstract Types
There are several mechanisms for creating abstract types, which vary based on their capability.
Full abstract base classes are classes either explicitly declared to be abstract, or which contain abstract
(unimplemented) methods. Except the instantiation capability, they have the same capabilities as a
concrete class or type. Full abstract types were present in the earliest versions of C++; and the
abstract base class remains the only language construct for generating abstract types in C++. A class
having only pure virtual methods is often called a pure virtual class; it is necessarily abstract.
Due to technical issues with multiple inheritance in C++ and other languages; many OO languages
sought to restrict inheritance to a single direct base class. In order to support multiple sub typing,
several languages added other features which can be used to create abstract types, but with less
power than full-blown classes.
Common Lisp Object System includes mixins, based on the Flavours system developed by David
Moon for Lisp Machine Lisp. (CLOS uses generic functions, defined apart from classes, rather than
member functions defined within the class).
Java includes interfaces, an abstract type which may contain method signatures and constants (final
variables), but no method implementations or non-final data members. Java classes may “implement”
multiple interfaces. An abstract class in Java may implement interfaces and define some method
signatures while keeping other methods abstract with the “abstract” keyword.
Traits are a more recent approach to the problem, found in Scala and Perl 6 (there known as roles),
and proposed as an extension to Smalltalk (wherein the original implementation was developed).
Traits are unrestricted in what they include in their definition, and multiple traits may be composed
into a class definition. However, the composition rules for traits differ from standard inheritance, to
avoid the semantic difficulties often associated with multiple inheritance.
274 LOVELY PROFESSIONAL UNIVERSITY