This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I created my own generic Queue to hold a wide range of objects. I use it as both a standard queue and a priority queue. With methods "enter" and "priorityEnter" depending on which one it is. When I use it as a priority queue I need to do ordered inserts of two diffrent kind of objects.
Data (abstract superclass) -Rooms extends Data -People extends Data -Events extends Data (abstract superclass) -Event1 extends Events -Event2 extends Events -Event3 extends Events
People and Events need to be stored in the priority queue sorting Poeple by a variable called priority, and Events by a variable called time. I can easily use instance of to determine if i'm working with a People object and perform the desired cast and perform my ordered insert. But I can only go as far as using instanceof to determine if it's an Events object, than it can't find the method getTime() in Events to do the ordered insert. Since there are diffrent kinds of events I don't want to re-write the code 3 times using the appropriate cast for each Event object. What can I do?
So I know that the parameter i'm working with is an instanceof Events ( could be any one of them ). The getTime() method is inside the Events class, and should be inherited by all of the classes that extend it. How do I access it? Or do I have to re-write the code for every class that extends is and go instanceof Event1, instanceof Event2 etc...?
So since Events is abstract, and the method getTime cannot be instantiated except only from one of Events subclasses. How can I write the code once, but cast it to the specific event and perform the ordered insert? If this is possible I can eliminate the duplicate code I wrote for "People", because I perform a "People" cast, but other than that the code is identical. [ January 25, 2008: Message edited by: John Lockheart ]