|
|
|
Example includes a method that returns an instance.
|
|
The concept of inheritance was introduced earlier in this sequence of WWW pages. Inheritance allows classes to "inherit" class members from other classes. The advantages are:
Some definitions (Figure 1):
|
Figure 1: Inheritance |
2. CREATING A CLASS HIERARCHY IN JAVA |
|
In all object oriented programming languages, when creating a sub-class we must somehow indicate to the compiler the super-class from which the sub class is derived. In Java this is done using the keyword extends. Consider the example code fragment given in Table 1. Here we have created a class called SuperClass and a sub-class of this class called SubClass. We have indicated to the Java compiler that SubClass is derived from the class SuperClass using the extends keyword:
class SubClass extends SuperClass {
...
}
A class diagram for the code is given in Figure 2. Notice that the super class has a data member which is defined as being protected. The reason for this is that:
To avoid the need for this additional effort fields that are to be inherited by sub-classes as defined as being protected. Thus: |
Figure 2: SperClass Class Diagram Therefore in Figure 2 the SuperClass class has one protected instance data member (dataItem1), a constructor, and a public instance method (outputData). The data member is inherited by the sub-class (the constructor and methods are public and so accessible from anywhere including the sub class). The SubClass class has a private data member (dataItem12), a constructor, and an instance method (outputDataItems). Instances of the class superClass therefore have one data member, while instances of the class subClass have two data members (one inherited). |
// SUPER CLASS
// Frans Coenen
// Created Tuesday 16 March 1999
// Revised Monday 15 July 2002
// The University of Liverpool, UK
class SuperClass {
// ------------------- FIELDS ---------------------
protected int dataItem1;
// ----------------- CONSTRUCTORS -----------------
/* Constructor */
public SuperClass(int input) {
dataItem1 = input;
}
// ------------------ METHODS ---------------------
/* Ouput data */
public void outputData() {
System.out.println("OUTPUT ONE DATA ITEM");
System.out.println("\tdataItem1 = " + dataItem1);
}
}
/****************************************************/
/* */
/* SUB CLASS */
/* */
/****************************************************/
class SubClass extends SuperClass {
// ------------------- FIELDS ---------------------
private int dataItem2 = 2;
// ----------------- CONSTRUCTORS -----------------
public SubClass(int input1, int input2) {
super(input1);
dataItem2 = input2;
}
// ------------------ METHODS ---------------------
/* Output dataItem1 and dataItem2 */
public void outputDataItems() {
System.out.println("OUTPUT TWO DATA ITEMS");
System.out.println("\tdataItem1 = " + dataItem1);
System.out.println("\tdataItem2 = " + dataItem2);
}
}
|
Table 1: Class hierarchy definition (SuperClass and SubClass)
// INHERITANCE EXAMPLE APPLICATION
// Frans Coenen
// Created Monday 15 July 2002
// The University of Liverpool, UK
class ExampleApp {
// ------------------- FIELDS ---------------------
/* None */
// ----------------- CONSTRUCTORS -----------------
/* None */
// ------------------ METHODS ---------------------
/* Main method */
public static void main(String[] args) {
// Create instances
SuperClass instance1 = new SuperClass(1);
SubClass instance2 = new SubClass(2,3);
System.out.println("INSTANCE 1\n==========");
instance1.outputData();
System.out.println("INSTANCE 2\n==========");
instance2.outputData();
instance2.outputDataItems();
}
}
|
Table 2: Example application class
$ java ExampleApp
INSTANCE 1
==========
OUTPUT ONE DATA ITEM
dataItem1 = 1
INSTANCE 2
==========
OUTPUT ONE DATA ITEM
dataItem1 = 2
OUTPUT TWO DATA ITEMS
dataItem1 = 2
dataItem2 = 3
|
Table 3: Example output generated from code presented in Tables 1 and 2.
If we wish to create an instance of a sub-class we (obviously) must use the constructor for this class. When invoked Java will also call the constructors for the super-classes starting with the base-class constructor and working down to the sub-class in question. This works fine provided that we only wish to invoke zero-argument (default) constructors. However, in the above example the SuperClass constructor has an argument! The question is then:
The answer is that we use the super keyword as illustrated in the above code. The key word super thus allows us to make use of the constructor defined in the super class, and thus (in the above case) we can cause a value to be assigned to the dataItem1 data member defined in the super-class.
Note: when using the reserved word super it must always be the first statement in the constructor body.
3. EXAMPLE PROBLEM --- CYLINDER CALCULATIONExtends the circle class created previously.
3.2 AnalysisA class diagram for the proposed solution is given in Figure 4. Note that: (1) we have "extended" the circle class introduced previously by adding a subclass cylinder; and (2) inheritance is indicated by an "open" arrow, and aggregation by a "closed" arrow (remember that inheritance and aggregation are two very different things). 3.3 DesignFrom Figure 3 the design comprises three programmer defined classes; the existing Circle class, plus new Cylinder and CylinderApp classes. 3.3.1 Cylinder Sub-Class
Nassi-Shneiderman charts for all the above methods are presented in Figure 5.
Figure 5: Nassi-Shneiderman charts for Cylinder class methods 3.3.2 CylinderApp Class
Nassi-Shneiderman charts for the above methods are presented in Figure 5.
Fig 5: Nassi-Shneiderman charts for CylinderApp class methods 3.4. Implementation3.4.2 Cylinder classWe will add this to the source file that contains the circle class. We can do this becuase cylinder is a sub-class of circle.
Table 4: Cylinder class hierarchy Notes on the above:
3.4.3 CylinderApp class
Table 5: Cylinder application program
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Given a class hierarchy, a sub-class will inherit the "protected" methods in the super-class. However, if the sub-class includes a protected method with the same "signature" (i.e. the same name and parameter list) as a method in the super-class the super-class will not be inherited. We say that the sub-class method overrides the super-class method.
Note that overriding should not be confused with overloading. Overloading involves providing several methods with the same name, but having different parameter lists; overriding involves providing several protected methods with the same name and the same parameter list, but declared in classes which are in a sub-class/super-class relationship.
|
An abstract method is a method defined by only its signature, i.e. it has no body. The implementational detail for the method, i.e. the body, is supplied by the sub-classes of the class in which the method is defined according to the nature of each sub-class. We indicate such a method to the compiler using the keyword abstract:
abstract public void function1(int);:
An abstract method can be thought of as a "template" or "blueprint" for a method whose implementational details are contained in the sub-classes of the class in which the abstract method is defined.
An abstract class is one that is identified by the keyword abstract. An abstract class does not have to contain an abstract method, however, a class that does contain at least one abstract method is considered to be an abstract class and must therefore be identified using the keyword abstract (otherwise it will not compile). Similarly an abstract class can contain methods that are not abstract. A frame work for a Java abstract class is presented in Table 6.
abstract class ClassName {
< FIELD DEFINITIONS >
< METHOD DEFINITIONS >
}
|
Table 6: Framework for abstract class
It is not possible to create instances of an abstract class as there will be no means of implementing the abstract methods it may contain --- instead we extend the abstract class and provide the body for the abstract methods contained in the abstract class in this sub-class.
|
We have seen that Java does not specifically support multiple-inheritance, however something like it can be achieved using an interface. An interface is a class that must contain only abstract methods and/or constants. Thus the interface supplies a specification of methods which must be implemented by a sub-class of the interface, we say that the subclass implements the interface.
Thus we can define a class, SubClass, which extends a super class (SuperClass) while at the same time implementing (say) two interface calsses --- Interface1 and Interface2:
class SubClass extends SuperClass implements Interface1, Interface2 {
< FIELD DEFINITIONS >
< METHOD DEFINITIONS >
}
Created and maintained by Frans Coenen. Last updated 26 July 2005