Way to convert List object to class object

J

joetharayil

hi
I was trying to acces a class using a List object in a different
class. I want the object to have the same features as LIst object but
the same time have acces to a different class in the same project.
thanks
joe
 
K

Knute Johnson

hi
I was trying to acces a class using a List object in a different
class. I want the object to have the same features as LIst object but
the same time have acces to a different class in the same project.
thanks
joe

Huh?
 
G

GArlington

hi
I was trying to acces a class using a List object in a different
class. I want the object to have the same features as LIst object but
the same time have acces to a different class in the same project.
thanks
joe

Are you trying to extend two classes? This is NOT possible...
You can extend ONE class AND implement MANY interfaces...
Or maybe you can find these useful:
http://forum.java.sun.com/thread.jspa?threadID=544569&messageID=2646191
http://www.artima.com/designtechniques/compoinh.html
 
L

Lew

GArlington said:
Are you trying to extend two classes? This is NOT possible...
You can extend ONE class AND implement MANY interfaces...
Or maybe you can find these useful:
http://forum.java.sun.com/thread.jspa?threadID=544569&messageID=2646191
http://www.artima.com/designtechniques/compoinh.html

I didn't read "have access to" as "inherit from", but as "use". The OP also
mentioned List, which /is/ an interface.

To the OP:
You place the List reference into a variable, e.g.,

List<Foo> something = new ArrayList<Foo>();

This can be any kind of variable: class, instance, local.

You pass the variable into a method of your other class, e.g., class Bar:

Bar bar = new Bar();
bar.someMethod( something );

Now the Bar someMethod() can operate on the argument:

public class Bar
{
public void someMethod( List<Foo> inVar )
{
// do some processing on inVar
}
}

Note that both the invoking class and the Bar class have to be "aware" of the
types List and Foo.
 
R

Roedy Green

I was trying to acces a class using a List object in a different
class. I want the object to have the same features as LIst object but
the same time have acces to a different class in the same project.
thanks

I read your sentence several times. I think you want to do this.

I want a object that has both the List methods and some others.

So you can either "implement List" or extend ArrayList or some other
List class. Then those objects will have all the List methods plus
any others you add. See SortedArrayList. Source is available from
http://mindprod.com/jgloss/products2.html#SORTED
 
D

Daniel Pitts

hi
I was trying to acces a class using a List object in a different
class. I want the object to have the same features as LIst object but
the same time have acces to a different class in the same project.
thanks
joe


public class MyList<T> extends AbstractList<T> implements List<T> {
DifferentClassInSameProject different;

public MyList(DifferentClassInSameProject different) {
this.different = different;
}

public void accessDifferent() {
different.doSomething();
}
// You have a little bit of implementing to do below.
// ...
}

Is that what you wanted?

Perhaps you'd get better answers if you asked us how to achieve a
certain goal, rather than how to do what you've determined will
achieve that goal.

HTH,
Daniel.
 
L

Lew

Daniel said:
public class MyList<T> extends AbstractList<T> implements List<T> {

The implements clause is redundant. AbstractList<T> already implements List<T>.
 
D

Daniel Pitts

The implements clause is redundant. AbstractList<T> already implements List<T>.

I know its redundant, but its explicitly explains what the purpose of
MyList is.
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top