Java Reference
In-Depth Information
public class Computer {
private CPU cpu = new CPU();
// CPU is an inner class of Computer
private class CPU {
// Code goes here
}
// Other code goes here for Computer class
}
Compare this implementation of composition between a computer and a CPU with the previous one. When you
use an inner class, an object of the CPU class cannot exist without an object of the Computer class. This restriction may
be problematic when the object of the same class, say CPU , is part of another object in a composition relationship.
Composition also denotes owner-owned relationship. A computer is an owner and a CPU is owned by the
computer. The owned object cannot exist without the owner object. Typically, but not necessarily, the owned object
is destroyed when the owner object is destroyed. Sometimes, when the owner object is being destroyed, it passes
the reference of the owned object to another owner. In such cases, the owned object survives the death of its current
owner. The point to note is that the owned object always has an owner.
Sometimes programmers get confused between the choice of using inheritance and composition and they use
inheritance instead of composition. You can find this kind of mistake in the Java class library where the java.util.
Stack class is inherited from java.util.Vector class. A Vector is a list of objects. A Stack is also a list of objects,
but is not simply a list of object as Vector is. A Stack is supposed to allow you to add an object to its top and remove
an object from its top. However, a Vector allows you to add/remove an object at any position. Since the Stack class
inherits from the Vector class, it also inherits methods that will let you add/remove an object at any position, which
are simply wrong operations for a stack. The Stack class should have used composition to use a Vector object as its
internal representation rather than inheriting from it. The following code snippet shows the correct use of a “has-a”
relationship between the Stack and Vector classes:
public class Stack {
// Stack has-a Vector
private Vector items = new Vector();
// Other code goes here
}
Whenever you are in doubt in choosing between composition and inheritance, give preference to composition.
Both let you share the code. however, inheritance forces your class to be in a specific class hierarchy. Inheritance also
creates a subtype, whereas composition is used to create a new type.
Tip
No Multiple Inheritance of Classes
Typically, a class signifies implementation. Java does not support multiple inheritance of implementation. That
is, a class in Java cannot have more than one superclass. Inheritance lets a class inherit implementation and/or
interface from its superclass. In the case of implementation inheritance, the superclass provides implementation
for functionality that its subclass inherits and reuses. For example, the Employee class has implemented the
getName() and setName() methods, which are inherited by the Manager class. In the case of interface inheritance,
the superclass provides specification for functionality that its subclass inherits and implements. Note that declaring
 
 
Search WWH ::




Custom Search