Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Polymorphism problem!!

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hi.

My problem is that I have a List (ADT) class with array, methods such as getLength() and getItem(int index) etc which make the class appear as an array. I have two of these, CustomerList & BookList.

I also have a binarySearch class (which suprisingly enough performs a binary search).
I want this binarySearch class to be able to accept any type of list as a parameter; CustomerList, BookList etc.

So I made an abstract class List which CustomerList and BookList extend.
However, both objects have getItem() methods which return either Customer or Book objects respectively.

How can I make allowances for this in my abstract List class, so that a getItem(int index) call on the List object passed in will return a Customer or Book object depending on whether a CustomerList object or BookList object was passed in??

Sorry if this is a little confusing, but there's too much code to expect ppl to read thru.
Here's the abstract List class tho:
------------------------List.java--------------------
public abstract class List
{
public abstract int getLength();

public abstract Object getItem(int index);

public abstract void addItem(Object item);

}
----------------------------------------------------
As you can see, an object of type Object is returned at the moment from getItem() but this means that unless I cast it back to either type Customer or Book in my binarySearch I cannot access methods defined in each.

I know casting is one answer, but it doesnt seem to be very polymorphic which is an important aspect of the program.

If anyone has ANY ideas, I'd be VERY grateful.
Thankyou all!

Oxy




we are all of us living in the gutter.
But some of us are looking at the stars.
 
What if you created an Interface called Searchable that defined a single method "contains()", like this:

public interface Searchable
{
public boolean contains(Object obj);
}

Make List implement Searchable:

public abstract class List implements Searchable

In BookList and CustomerList, create the contains method that checks to see if the object contains the thing the binarySearch will look for:

public class BookList extends myList
{
public BookList() {}

public boolean contains( Object obj )
{
// Add check for obj in this object, return true;
return false;
}
}

Lastly, change binarySearch to accept any object that implements Searchable (instead of any object that extends List). This should solve your problem and give you a binary search class that will work on other lists of objects as well...




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top