Question about Implementing Generics

D

David

To All,

Is there a way when I am creating my own generic class that I can define
the (for lack of a better term) highest super class allowed as the
generic?

To be more specific, for example, if you use define a Vector and don't
utilize the generics, an Object will be returned via the elementAt()
method. However, let's say I have written a MyObject class that extends
Object and I have a MyVector that extends Vector and I want to define
within MyVector that it only allows MyObject or other extensions of it
so that if a MyVector is instantiated with the generics definition, a
MyObject is returned, not an Object.

Is that clear?

Sniplets that may help or hinder......

class MyObject extends Object {
public void someMethod() { // do something }
}

class MyVector<V> extends Vector {
/* I want to force V to be an instance of MyObject */
public V elementAt(int index) { return super.elementAt(index); }
}

class Tester {
public static void main(String[] args) {

Vector v = new Vector();
v.add("A");
v.elementAt(0); // returns an Object.

Vector<String> v2 = new Vector<String>();
v.add("A");
v.elementAt(0); // returns a String.

MyVector mv = new MyVector();
mv.add(new MyObject());
/* The next line will fail because the default is to return an object
but I want it to be a MyObject. */
mv.elementAt(0).someMethod();
 
D

Daniel Dyer

class MyVector<V> extends Vector {
/* I want to force V to be an instance of MyObject */
public V elementAt(int index) { return super.elementAt(index); }
}

If I understand you correctly, the following may be sufficient:

class MyVector extends Vector<MyObject> {

That's if you don't want to specify the type of your collection when you
declare a field/variable of type MyVector.

private MyVector myField;

Otherwise, this should work:

class MyVector<T> extends Vector<T> {

Then you declare your field like this:

private MyVector<MyObject> myField;

In both cases myField.elementAt() should return a MyObject instance with
no need for casting. No need in either case to over-ride the elementAt
method.

Dan.
 
D

Daniel Dyer

If I understand you correctly, the following may be sufficient:

class MyVector extends Vector<MyObject> {

That's if you don't want to specify the type of your collection when you
declare a field/variable of type MyVector.

private MyVector myField;

Otherwise, this should work:

class MyVector<T> extends Vector<T> {

Then you declare your field like this:

private MyVector<MyObject> myField;

In both cases myField.elementAt() should return a MyObject instance with
no need for casting. No need in either case to over-ride the elementAt
method.

Having read your post again, I realise that my second solution doesn't
solve your problem. The first one may still be sufficient, if not this
should do the trick:

class MyVector<T extends MyObject> extends Vector<MyObject> {

Dan.
 
C

Chris Smith

Daniel Dyer said:
Having read your post again, I realise that my second solution doesn't
solve your problem. The first one may still be sufficient, if not this
should do the trick:

class MyVector<T extends MyObject> extends Vector<MyObject> {

I think you meant:

class MyVector<T extends MyObject> extends Vector<T>

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

Daniel Dyer

I think you meant:

class MyVector<T extends MyObject> extends Vector<T>

Only if you want type-safety :)

My version is effectively the same as the first suggestion I posted, but
with an unenforced suggestion as to the type of the contents. It's past
my bedtime.

Thanks,

Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Implementing Many Stacks in the Same Program 1
generics puzzle 57
Generics ? 14
Hairy generics question 29
Vectors and accessing elementCount - Newbie Question 7
Vector class 5
can this be done with generics? 32
Generics 12

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top