ABSTRACT CLASSES AND METHODS

CONTENTS

1. Introduction
2. Abstract classes
3. Abstract methods
4. Abstract classes and the Java API


1. INTRODUCTION

Consider the class hierarchy given in Table 1. Here we have a TopClass which has a constructor and a field numX. This class has two subclasses, ClassOne and ClassTwo, both of which have a method called function1 whose signatures (heads) are identical but whose bodies differ --- one divides numX by 2 and the other by 3. Figure 1 presents a class diagram illustrating the class hierarchy associated with these classes.

ABSTRACT CLASS

Figure 1: Class hierarchy.

An application class which makes use of the classes in the hierarchy is presented in Table 2, together with some sample output in Table 3.

// Abstract Example application 
// Frans Coenen
// 9 March 2000
// Dept Computer Science, University of Liverpool

class AbstractExApp {
	
    // ------------------ METHODS ------------------------ 
    
    /* Main method */
    
    public static void main(String[] args) {
        
	// Create instance of ClassOne and ClassTwo
	
	TopClass topObject = new TopClass(24);
	ClassOne object1 = new ClassOne(12);
	ClassTwo object2 = new ClassTwo(6);
	
	// Output
	
	System.out.println("topObject.getNumX() = " + 
	    topObject.getNumX() + "\n");
	object1.function1(2);
	System.out.println("object1.getNumX() " + "
	    (after object1.function1(2) call)  = " + 
	    object1.getNumX() + "\n");
	object2.function1(2);
	System.out.println("object2.getNumX() " + 
	    "(after object2.function1(2) call) = " + 
	    object2.getNumX() + "\n");
	}
    }

Table 2: Application class

$ java AbstractExApp
topObject.getNumX() = 24

object1.getNumX() (after object1.function1(2) call)  = 14

object2.getNumX() (after object2.function1(2) call) = 12   

Table 3: Output

// Abstract Example Classes - ClassOne
// Frans Coenen
// 10 March 2000
// Dept Computer Science, University of Liverpool

class TopClass {    
    // ----------- FEILDS ----------
    
    protected int numX;
    
    // ----------- CONSTRCTURS ----------	 
    
    public TopClass(int value) {
    	numX = value;
	}
	
    // ----------- METHODS ---------- 
    
    /* Get numX */
    
    public int getNumX() {
        return(numX);
	}    	
    }

class ClassOne extends TopClass {    

    // ---------- CONSTRCUTORS ---------- 	
    
    public ClassOne(int value) {
    	super(value);
	}
		 
    // ----------- METHODS ---------- 
    
    /* Function 1 */
    
    public void function1(int numY) {
        numX = numX+numY;
	}    	
    }

class ClassTwo extends TopClass {

    // ---------- CONSTRCUTORS ---------- 	
    
    public ClassTwo(int value) {
    	super(value);
	}
		
    // ------------ METHODS -------------   
    
    /* Function 1 */
    
    public void function1(int numZ) {
        numX = numX*numZ;
	}    
    }

Table 1: Class hierarchy

Given this situation it would be nice if we could define the head of the function1 method (i.e. its signature) in the TopClass and its implementational detail (i.e. the body) in the sub-classes. Java provides a means of doing this by allowing us to define, in the TopClass, what is known as an Abstract method.



2. ABSTRACT METHODS

An abstract method is one that is defined by its signature only, i.e. it has no body. The implementational detail for the method, i.e. the body, are 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. Thus in the above example we could include, in the TopClass, the method:

abstract public void function1(int);

As shown in Table 4.

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. The implementational detail is included in the normal way so no changes need to be made to the definition of ClassOne or ClassTwo (Table 1) in our example. In effect the abstract method is overriden by the full method definitions contained in the sub-classes.

// Abstract Example Classes - ClassOne
// Frans Coenen
// 10 March 2000
// Dept Computer Science, University of Liverpool

class TopClass {    
    // ----------- FEILDS ----------
    
    protected int numX;
    
    // ----------- CONSTRCTURS ----------	 
    
    public TopClass(int value) {
    	numX = value;
	}
	
    // ----------- METHODS ---------- 
    
    /* Abstract method function1 */
    
    abstract public void function1(int dummy);
        
    /* Get numX */
    
    public int getNumX() {
        return(numX);
	}    	
    }

Table 4: Revision of TopClass definition.



3. ABSTRACT CLASSES

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 5.

abstract class ClassName {
    
    < FIELD DEFINITIONS >
    
    < METHOD DEFINITIONS >
    	
    }

Table 5: 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) thus the application class presented in Table 3 should be modified as shown in Table 6.

// Abstract Example application 
// Frans Coenen
// 9 March 2000
// Dept Computer Science, University of Liverpool

class AbstractExApp {
	
    // ------------------ METHODS ------------------------ 
    
    /* Main method */
    
    public static void main(String[] args) {
        
	// Create instance of ClassOne and ClassTwo
	
	ClassOne object1 = new ClassOne(12);
	ClassTwo object2 = new ClassTwo(6);
	
	// Output
	
	object1.function1(2);
	System.out.println("object1.getNumX() (after object1.function1(2) call)  = " + 
			object1.getNumX() + "\n");
	object2.function1(2);
	System.out.println("object2.getNumX() (after object2.function1(2) call) = " + 
			object2.getNumX() + "\n");
	}
    }

Table 6: Revised application class

Similarly an instance of a sub-class of an abstract class can only be created if any abstract methods in the super-class are overridden in the sub-class definition. If not the sub-class should also be defined as an abstract class.



4. ABSTRACT CLASSES AND THE JAVA API

It is quite possible to program in Java without resorting to abstract classes, however they are a feature of the Java API and thus any student of Java should be aware of the existence of such a thing!

An example of an abastract class in the Java API can be found in the java.io API. This contains the class InputStream, which is an abstract class and the super class of all input stream classes. InputStream proviodes abstract methods required by all the "input stream" subclasses, i.e. it provides templates for these methods. Each of the InputStreram abstract methods are then implemented in a way specific to each of its sub-classes.




Created and maintained by Frans Coenen. Last updated 15 July 2002