- 20 -
A constructor allows a new object to be initialized in a convenient way. Without the
constructor in this program, you would have needed an additional call to deposit() to
put the opening balance in the account.
Public and Private
Notice the keywords public and private in the BankAccount class. These keywords
are access modifiers and determine what methods can access a method or field. The
balance field is preceded by private. A field or method that is private can only be
accessed by methods that are part of the same class. Thus, balance cannot be
accessed by statements in main(), because main() is not a method in BankAccount.
However, all the methods in BankAccount have the access modifier public, so they
can be accessed by methods in other classes. That's why statements in main() can call
deposit(), withdrawal(), and display().
Data fields in a class are typically made private and methods are made public. This
protects the data; it can't be accidentally modified by methods of other classes. Any
outside entity that needs to access data in a class must do so using a method of the
same class. Data is like a queen bee, kept hidden in the middle of the hive, fed and cared
for by worker-bee methods.
Inheritance and Polymorphism
We'll briefly mention two other key features of object-oriented programming: inheritance
and polymorphism.
Inheritance is the creation of one class, called the extended or derived class, from
another class called the base class. The extended class has all the features of the base
class, plus some additional features. For example, a secretary class might be derived
from a more general employee class, and include a field called typingSpeed that the
employee class lacked.
In Java, inheritance is also called subclassing. The base class may be called the
superclass, and the extended class may be called the subclass.
Inheritance makes it easy to add features to an existing class and is an important aid in
the design of programs with many related classes. Inheritance thus makes it easy to
reuse classes for a slightly different purpose, a key benefit of OOP.
Polymorphism involves treating objects of different classes in the same way. For
polymorphism to work, these different classes must be derived from the same base class.
In practice, polymorphism usually involves a method call that actually executes different
methods for objects of different classes.
For example, a call to display() for a secretary object would invoke a display
method in the secretary class, while the exact same call for a manager object would
invoke a different display method in the manager class. Polymorphism simplifies and
clarifies program design and coding.
For those not familiar with them, inheritance and polymorphism involve significant
additional complexity. To keep the focus on data structures and algorithms, we have
avoided these features in our example programs. Inheritance and polymorphism are
important and powerful aspects of OOP but are not necessary for the explanation of data
structures and algorithms.
Software Engineering