Is this code "proper" use of extend?
Thanks in advance for your responses.
I am relatively new to OOP and though I have muscled my way through
Java, I am still trying to correctly understand the concepts of OO and
how then are modeled into Java.
I am a thirty year procedural language developer, c and assembler, to
this (recent) technology called Object Oriented whatever is somewhat
alien to my experiences. I think I grasp the basic concepts of OO,
that being describing concrete "objects" as a series of refinements to
abstract "conceptual objects." (Please don't flame me if that
statement is incorrect, I am seeking to increase my understanding.)
The following Java 1.5 code I believe demonstrates the statement
above. I needed Vector container that would notify() all active
Objects it contains. My thinking was to simply extend Vector adding
the functionality I wanted.
However, a co-worker is rather irate at me for "doing such a silly
thing," preferring instead to Instantiate a Vector object with
SignaledVector, and implement the entire Vector contract method by
method. That just sounds contrary to principles OO.
Your thoughts?
import java.util.Collection;
import java.util.Vector;
public class SignaledVector<E> extends Vector<E>
{
public SignaledVector()
{
super();
} // public SignaledVector()
public SignaledVector(int initialCapacity)
{
super(initialCapacity);
} // public SignaledVector(int initialCapacity)
public SignaledVector(int initialCapacity,int capacityIncrement)
{
super(initialCapacity,capacityIncrement);
} // public SignaledVector(int initialCapacity,int
capacityIncrement)
public SignaledVector(Collection<? extends E> c)
{
super(c);
} // public SignaledVector(Collection<? extends E> c)
//public synchronized boolean add(E e) { return(super.add(e)); }
/**
* Sends a signal to the object that has posted a {@code
this.wait()}
*
* @param index the index in the element to signal
* @return the component at the specified index
* @throws ArrayIndexOutOfBoundsException
*
*/
public E signal(int index)
throws ArrayIndexOutOfBoundsException
{
E element = get(index);
synchronized(element) { element.notify(); }
return(element);
} // public final void signal<E>(int index)
/**
* Sends a signal to the object that has posted a {@code
this.wait()}
* This method sends a {@code synchronized} signal to all elements
* contained in the {@code SignaledVector}
*
* @throws ArrayIndexOutOfBoundsException
*
*/
public synchronized void signalAll()
throws ArrayIndexOutOfBoundsException,
NullPointerException
{
java.util.Enumeration<E> threads = elements();
while(threads.hasMoreElements())
{
E element = threads.nextElement();
if (null == element) continue;
synchronized(element) { element.notify(); }
} // while(threads.hasMoreElements())
} // public final void signalAll()
} // public class SignaledVector<E> extends Vector<E>
cj