Algorithms and Data Structures
 Quick Links What's New  Home  Contacts  Syllabus  Course Book  Java Book  JDK & JDSL  Slides  Notes  Demos  Exercises  Marks

Composition or Inheritance?

So far I've produced a list of integers, then made a generic list, implemented a ToDo list using this generic list, and then performed a retrofit so that I have a list of integers (implemented as a generic List of Integer objects). So, could I go a bit further and define my own class of integer list IntList with all the associated methods, and if so how should I do it?

See TIJ217 and DSA209 for a discussion on composition versus inheritance

The IntList
In List4/IntList.java I define an IntList using composition. I include some of that code below.
```public class IntList {
private List L;    // an instance variable, a List!

private List getList(){ return L; }

public IntList() { L = new List(); }

public IntList(int e) { L = new List (new Integer(e)); }

public IntList(int e, IntList tail) {
L = new List(new Integer(e), tail.getList());
}

public IntList(List l) { L = l; }

public IntList add(int e) { return new IntList(e,this); }

public String toString() { return L.toString(); }

public int head() { return ((Integer)L.head()).intValue(); }

public IntList tail() { return new IntList(L.tail()); }

public int cons() { return L.cons(); }

public int length() { return L.length(); }

public IntList reverse() throws ListException { return new IntList(L.reverse()); }

public IntList insert(int e) {
return new IntList(L.insert(new Integer(e), new IntComp()));
}
}```
You should notice that what I've done is introduce an instance variable L of class List, and redefined all the List methods to work on an IntList by merely accessing the instance variable L. It's that easy. But is it the right thing to do? I have overloaded the methods, so that we have a tail() method for a List that acts on a List and delivers a List, and a tail() method for an IntList that delivers an IntList. I have to redefine/overload each method that I want to allow (put into the api) for the IntList class. So, I could omit some! I could omit reverse() and add(), so that the only way to add anything to an IntList would be via insert(p). Now what would that imply?

All the code for the composition is here

Inheritance
See JIAN page 68 and TIJ page 181.

To quote from JIAN68

When a class defines a method using the same name, return type, and arguments as the method in its superclass, the method in the class overrides the method in the superclass. When the method is invoked for an object of the class, it is the new definition of the method that is called, not the superclass' old definition.
So, if we are wanting to produce a subclass IntList that extends List we will want to override methods, such as head and tail. When we do that, the method head should deliver as a result an integer. We have a problem! The above states that to override a method it must have the same return type. Consequently we would have to define a new method called intHead.