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.
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.