Java Reference
In-Depth Information
public void abstract print();
public abstract object larger(object, object);
void abstract insert( int insertItem);
An abstract class is a class that is declared with the reserved word abstract in its
heading. Following are some facts about abstract classes:
￿ An abstract class can contain instance variables, constructors, the finalizer,
and nonabstract methods.
￿ An abstract class can contain an abstract method(s).
￿ If a class contains an abstract method, then the class must be declared
abstract.
￿ You cannot instantiate an object of an abstract class. You can only declare
a reference variable of an abstract class type.
￿ You can instantiate an object of a subclass of an abstract class, but only if the
subclass gives the definitions of all the abstract methods of the superclass.
The following is an example of an abstract class:
public abstract class AbstractClassExample
{
protected int x;
public abstract void print();
public void setX( int a)
{
x = a;
}
1
0
public AbstractClassExample()
{
x = 0;
}
}
Abstract classes are used as superclasses from which other subclasses within the same
context can be derived. They serve as placeholders to store members common to all
subclasses. They can be used to force subclasses to provide certain methods, as illustrated
in Example 10-7.
EXAMPLE 10-7
Banks offer various types of accounts, such as savings, checking, certificate of deposits,
and money market, to attract customers as well as to meet their specific needs. In this
example, we illustrate how to use abstract classes and polymorphism for processing
different kinds of bank accounts.
Two of the most commonly used accounts are savings and checking. Each of these accounts
has various options. For example, you may have a savings account that requires no minimum
Search WWH ::




Custom Search