Extending Vector

I am using 5.0 and am extending a Vector and want to override the add() method.

but of cause, I cant just override add(Object object) due to generics. How can I overide this method?

this is what i have:

publicvoid add(Object object){

super.add(object);

//my code

}

thanks

[471 byte] By [j__fa] at [2007-9-23]
# 1
The Java Collections API is fairly complete; why are you extending Vector? Chances are good (to excellent) that you shouldn't be extending this class...
yawmarka at 2007-7-11 > top of java,Core,Core APIs...
# 2

Well its defined in the Collections API as:

boolean add(E o)

So you need to implement it with the same signature to override it.

But I do agree with yawmark. You shouldn't have to extend Vector.

What is it you are trying to accomplish by overriding Vector, with your own add method?

evnafetsa at 2007-7-11 > top of java,Core,Core APIs...
# 3

My class is going to be used similar to a Vector, so i want all the functions of the Vector, but when i add an item to my list, i want it to notify some listeners...

and just thinking about it, i need to override remove();

thanks for pointing out that it returns boolean, not void. Ive fixed that now, but i still cant figure this out :s

Last resort is to just have a Vector object in my class, rather then extending it.

j__fa at 2007-7-11 > top of java,Core,Core APIs...
# 4

import java.stuff.morestuff;

class MyVector<T> extends Vector<T> {

public boolean add(T t) {

super.add(t);

Listeners.getSomeListener().beNotifiedAbout(this, t);

}

}

~Cheers

p.s. I agree with the other guys about this being a smell that should make you reconsider it. But if you don't care anyway... Meh.

Adeodatusa at 2007-7-11 > top of java,Core,Core APIs...
# 5
well im not ignoring you're advice, but either way im still curious as to how i can override these methods...and ah, i see, i should declare the generics in my class... thats what I thought I might have to do.cheers for the tips guys, question answered :)
j__fa at 2007-7-11 > top of java,Core,Core APIs...
# 6

usually, you don't extends a utility class, except for very good reason. your reason here is not a good one.

What i think you should do is use composition

example

public MyVector<T> {

private Vector<T> v = new Vector<T>();

public boolean add(T t) {

v.add(t);

Listeners.getSomeListener().beNotifiedAbout(this, t);

}

}

rather, i would use a List instead of a Vector..and use Collections utility class to make the List synchronized 9if needed). I would probably implements the List interface (if i need the class for polymorphism)

example

public class MyList<T> implements List<T>{

List<T> list = new ArrayList<T>();

public boolean add(T t) {

v.add(t);

Listeners.getSomeListener().beNotifiedAbout(this, t);

}

}

tnguyen1973a at 2007-7-11 > top of java,Core,Core APIs...
# 7

thanks,

Ive actually ended up doing your first suggestion. Except my class doesnt define the generics, instead ive defined it when instanating the Vector. and then ive just overidden the methods I wanted buy calling super.method()

Vector vs ArrayList.

A question ive been meaning to ask; whats the difference and how to i choose? Ive just always used Vector, and I use LinkedList when I want to use the removeFirst() and removeLast() methods.

j__fa at 2007-7-11 > top of java,Core,Core APIs...
# 8

You could leverage a list model:

package com.jshift.util;

import java.util.AbstractList;

import javax.swing.ListModel;

public abstract class ListModelList<T> extends AbstractList<T> {

private final ListModel mModel;

/** List model that provides data for list. */

public ListModel getModel() {return mModel;}

public ListModelList(ListModel pModel) {mModel = pModel;}

@Override public T get(int pIdx) {return (T) getModel().getElementAt(pIdx);}

@Override public int size() {return getModel().getSize();}

}

I dunno why ListModel doesn't implement List in the first place. You might want to override some of the bulk modification methods so as to minimize the number of events.

hwaitea at 2007-7-11 > top of java,Core,Core APIs...
# 9

> A question ive been meaning to ask; whats the

> difference and how to i choose? Ive just always used

> Vector, and I use LinkedList when I want to use the

> removeFirst() and removeLast() methods.

Don't use Vector, use ArrayList. If your application requires a synchronized List, use Collections.synchronizedList(new ArrayList()). In the least case, always declare your List as "List whatever."

In general, ArrayList is faster than LinkedList. You can get performance data on almost all the collections classes by searching google.

~Cheers

Adeodatusa at 2007-7-11 > top of java,Core,Core APIs...
# 10
> Last resort is to just have a Vector object in my> class, rather then extending it.Word of advice: That should be your first resort.Prefer composition to inheritance.
yawmarka at 2007-7-11 > top of java,Core,Core APIs...
# 11
Thanks for the tips guys. I found a good read on the differences and choosing which to use. Cant believe I was still using Vector's and Hashtables :S
j__fa at 2007-7-11 > top of java,Core,Core APIs...
# 12
yes, Vector, Hashtable, and Enumartor are legacy class.The prefers class are ArrayList, HashMap, and Iterator.Of course, sometime, you will still have to use Vector, and Enumerator (this is ussally when the Java API returns an object of that type...like the TreeNode)
tnguyen1973a at 2007-7-11 > top of java,Core,Core APIs...
# 13

i don't think the second suggesting (implementing a List) has any problem

after all..you're implementing to an interface..not extending it...and you still use composition...It's flexiable in term that you can write a method tat will takes a List (this method does not care if it's a LinkedList, ArrayList, or even your MyList)

only problem with this is...if you decided MyList should be a Map or a Set instead.

tnguyen1973a at 2007-7-11 > top of java,Core,Core APIs...