Java Programming: Building New Classes
- 0 Comments
Java Programming Tutorials: There are two fundamental mechanisms for building new classes from existing ones: inheritance and aggregation. It makes sense to inherit from an existing class Vehicle to define a class Car, since a car is a vehicle. The class Vehicle has several parts; therefore, it makes sense to define a composite object of class Vehicle that has constituent objects of such classes as Motor, Axle, and GearBox, which make up a vehicle.
Inheritance is illustrated by an example that implements a stack of characters that can print its elements on the terminal. This new stack has all the properties and behaviors of the CharStack class, but it also has the additional capability of printing its elements. Given that this printable stack is a stack of characters, it can be derived from the CharStack class. The class PrintableCharStack is called the subclass, and the class CharStack is called the superclass. The CharStack class is a generalization for all stacks of characters, whereas the class PrintableCharStack is a specialization of stacks of characters that can also print their elements.
In Java, deriving a new class from an existing class requires the use of the extends clause in the subclass definition. A subclass can extend only one superclass. The subclass inherits members of the superclass. The following code fragment implements the PrintableCharStack class:
class PrintableCharStack extends CharStack { // (1)
// Instance method
public void printStackElements() { // (2)
// … implementation of the method…
}
// The constructor calls the constructor of the superclass explicitly.
public PrintableCharStack(int capacity) { super(capacity); } // (3)
}
The PrintableCharStack class extends the CharStack class at (1). Implementing the printStackElements() method in the PrintableCharStack class requires access to the field stackArray from the superclass CharStack. However, this field is private and therefore not accessible in the subclass. The subclass can access these fields if the accessibility of the fields is changed to protected in the CharStack class.