INTERFACESS

CONTENTS

1. Introduction
2. Clone example


1. INTRODUCTION

From previous work we have seen that classes are typically arranged in a class hierarchy (Figure 1) such that classes lower down the hierarchy inherit methods and attributes from their super classes. In Java inheritance is a one-to-one relationship, a class can only be a sub-class of (i.e. inherit from) one super class. Several sub-classes can inherit from one super class. Java does not support multiple inheritance (Figure 2).

HIERARCHY

Figure 1: Class hierarchy

MULTIPLE INHERITANCE

Figure 2: Multiple inheritance (not supported in Java)

However, although Java does not specifically support multiple-inheritance, something like it can be achieved using an interface (Figure 3). 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. (It is possible for an interface not to contain any methods or constants.)

HIERARCHY

Figure 3: Java interface to produce "multiple inheritance like" behaviour


1. CLONE EXAMPLE

We have seen the the class Object contains a method equals which returns true if the this object and its argument refer to the same object. In practice we wish to compare the values associated with such objects and thus override the equals method. Similarly the class Object contains a method clone which creates and returns a copy of the this argument. Again, for this to work correctly (i.e. to ensure that all the values associated with the this object are transfered) we must override the method. However, in this case, the sub-class in which the clone method is overriden must implement the cloneable interface.

The cloneable interface is a public interface found in java.lang which contains no methods. The interface is used to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Attempts to clone instances that do not implement the Cloneable interface result in the exception CloneNotSupportedException being thrown.

The code presented in Table 1 defines a class, ClassOne, which extends the class Object and implements the interface cloneable. The latteer is so that we can override the clonemethod contained in the object super-class.

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

class ClassOne extends Object implements Cloneable {    

    private int numX;
    
    // ---------- CONSTRCUTORS ---------- 	
    
    public ClassOne(int value) {
    	numX = value;
	}
		 
    // ----------- METHODS ---------- 
    
    /* Override equals method */
    
    public boolean equals(ClassOne obj) {
        return(this.numX == obj.numX);
        } 
	
    /* Overide clone method */
    
    public Object clone() {
        return(new ClassOne(this.numX));	
        }
    }

Table 1: Example class

Table 2 gives an application class to go with the above. Note the use of the cast used with the clone method. A class diagram illustrating the connectivity between these classes is presented in Figure 4.

// CLONEABLE APPLICATION
// Frans Coenen
// Wednesday 15 March 2000
// The University of Liverpool, UK

import ClassOne;
               
class CloneableApp {

    /* ------ MAIN ------ */

    public static void main(String[] args) {
    	int total = 0;
	
	// Create an instances of the the class ClassOne and make a copy
	
	ClassOne object1 = new ClassOne(1);
	ClassOne object2 = (ClassOne) object1.clone();

	// Compare these two objects
	
	if (object1.equals(object2)) 
		System.out.println("Object1 and Object2 are the same.");
	else System.out.println("Object1 and Object2 are NOT the same.");
	}	
    }

Table 2: Application class

CLASS DIAGRAM SHOWING INPUT CLASS

Figure 4: Class diagram.

Note: In the JDK documentation interfaces are indicated using itallic script.




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