extends ArrayList to manage a particular class

E

edi

Hi,

I want to create my ArrayList which contains only objects from a
paricular class, MyClass. I extends ArrayList and override only method
"add(int,object)" as follows:

public void add(int index, Object m) {
if ( !(m instanceof MyClass)){
return;//here I want to throw an exception
}
super.add(index,m);
}

but I want to throw an exception if the object is not from my class.
super.add(...) throws only IndexOutOfBoundsException. What can I do in
this situation? Are there any other possibilities to cope with this
situation?
 
S

Steve W. Jackson

edi said:
Hi,

I want to create my ArrayList which contains only objects from a
paricular class, MyClass. I extends ArrayList and override only method
"add(int,object)" as follows:

public void add(int index, Object m) {
if ( !(m instanceof MyClass)){
return;//here I want to throw an exception
}
super.add(index,m);
}

but I want to throw an exception if the object is not from my class.
super.add(...) throws only IndexOutOfBoundsException. What can I do in
this situation? Are there any other possibilities to cope with this
situation?

Try using the new Java 1.5 (aka Java 5) with its generics. That's one
of the biggest things added in this new release is the ability to do
that with essentially any Collection, List, etc.

= Steve =
 
L

Lasse Reichstein Nielsen

edi said:
I extends ArrayList and override only method "add(int,object)" as
follows:

public void add(int index, Object m) {
if ( !(m instanceof MyClass)){
return;//here I want to throw an exception ....
but I want to throw an exception if the object is not from my class.
super.add(...) throws only IndexOutOfBoundsException.

As Steve W. Jackson suggested, using Java Tiger removes the problem,
since you can make a list for only MyClass objects.

Otherwise, you could do what you do, and just throw an
IllegalArgumentException. It is an unchecked exception, so there is
no need to declare it in the method signature.

However, your approach has the problem that it doesn't handle all
ways of adding elements to the list. You also need to change
addAll(Collection) and add(Object).

You might know that all you ever do is add, remove and get. In that
case, you could just make your own "MyClassListlikeThingie" class
with only those three operations, and then, internally, it could use
a normal ArrayList to store the values.

/L
 
N

none

edi said:
I want to create my ArrayList which contains only objects from a
paricular class, MyClass. I extends ArrayList and override only method
"add(int,object)" as follows:

Wouldn't this be considered invalid OOP since it violates the
substitution principle?

-Mike
 
M

Marcin Grunwald

edi said:
Hi,

I want to create my ArrayList which contains only objects from a
paricular class, MyClass. I extends ArrayList and override only method
"add(int,object)" as follows:

public void add(int index, Object m) {
if ( !(m instanceof MyClass)){
return;//here I want to throw an exception
}
super.add(index,m);
}

but I want to throw an exception if the object is not from my class.
super.add(...) throws only IndexOutOfBoundsException. What can I do in
this situation? Are there any other possibilities to cope with this
situation?

As others said - the best solution is to use JDK1.5 and generics.
If you can't use JDK1.5 than use delegate instead of inheritance.

class YourClass {
private ArrayList list = new ArrayList();

public void add(int index, MyClass obj) {
list.add(index, obj);
}

...
}

This method checks if the object is not from your class, during compilation.
It's much better than checking during runtime.
 
B

bugbear

edi said:
Hi,

I want to create my ArrayList which contains only objects from a
paricular class, MyClass. I extends ArrayList and override only method
"add(int,object)" as follows:

public void add(int index, Object m) {
if ( !(m instanceof MyClass)){
return;//here I want to throw an exception
}
super.add(index,m);
}

but I want to throw an exception if the object is not from my class.
super.add(...) throws only IndexOutOfBoundsException. What can I do in
this situation? Are there any other possibilities to cope with this
situation?

Yep. Try the PredicatedList class from jakarta commons collections,
which can do what you want as a subset of its functionality.

Run time type checking is very useful when you're using
sophisticated patterns and extensive polymoprophism.

In these circumstances, types are not even known
at compile time (not by any current compiler anyway ;-)

BugBear
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top