|
In addition to the member variables and method declarations, a class declaration may also dictate:
What is the superclass of that class
The list of interfaces that class implements
Modifiers to specify the class access
Modifiers to specify special class behavior such as final
(unchangeable class) or abstract (generalized class)
The general format of the class declaration is as shown below:
[accessModifier] [classModifiers] class ClassName [extends SuperClassName] [implements InterfaceList]
{
[classbody]
}
The elements between the pair of square brackets [] are optional. The access modifier specifies who can access this class. The class modifiers specify the behavioral restrictions on this class. For instance, The class modifier abstract indicates that you cannot create objects from this class. We will discuss all kinds of modifiers in the third chapter. Let us see how the class declares what is its superclass and what interfaces it implements and also what does that imply.
Sometimes, you need some functionality in your newly declared Java class that is already there in some other Java class. In that case, you need not rewrite the same code in your new class, instead you can reuse the code of that Java class. One way of such code reuse is the inheritance where a Java class can extend another Java class to reuse its functionality.
|
|
Another way of code reuse is using composition which is explained when we discuss IS-A and HAS-A relationships among java objects in chapter 11. |
The class from which the newly declared class extends is the superclass. By default, the java.lang.Object class is the superclass of all Java classes. For example, in the code listing 1.1, the Account class does not explicitly specify any superclass. Therefore, its superclass is java.lang.Object.
But if a Java class needs to extend another Java class besides java.lang.Object, it can explicitly specify its superclass by using the extends keyword. For instance, imagine that our banking application needs to have a class for the checking accounts. Since the checking accounts are usually a special kind of bank accounts, it is better to define it in-terms-of Account class. Therefore you are likely to define the CheckingAccount class by extending the Account class as:
class CheckingAccount extends Account {
// Class body
}
Defining CheckingAccount like this implies that it is a subclass of Account. Since a Java subclass that extends a class automatically inherits its implementation, CheckingAccount inherits the variables and methods from its superclass Account. Note that this relationship between the CheckingAccount and Account exhibits the inheritance principle of object-oriented programming.
|
|
Since all the classes inherit from java.lang.Object, they have the methods of Object class available to them. Even when a class does not directly inherit from an Object class, its superclass or its superclass’s superclass may be inheriting from the java.lang.Object. Therefore, bear in mind that all Java classes understand the Object class’s methods. That simply means you can invoke java.lang.Object class’s methods on the object of any Java class. Also, note that a Java class can specify at most one superclass. Java does not allow extending from multiple classes, as it does not support multiple-inheritance. |
While a Java class can have only one superclass, it may implement multiple interfaces. Remember that it is always a ‘class’ that implements an interface, because interfaces as such do not provide any implementation. A Java interface only describes a common behavior. The classes that implement the interface must implement the behavior described in the interface. For instance, let us say we have two classes CheckingAccount and SavingAccount and both these classes are auditable meaning we can invoke auditing on them. In that case, we can define (we will see how to define interface in next section) an interface named Auditable with an audit() method in it. The interface will not implement this method. It only ascertain that every class that implements the Auditable interface must have the audit()method containing the code that does the auditing. In that case you may define the auditable CheckingAccount class as:
class CheckingAccount extends Account implements Auditable{
// Class body
public void audit() {
// checking-account specific auditing
}
}
Thus specifying interfaces in class declaration ensures that the interface is implemented in the class body.