| Author |
a subclass taking unlimited objects of only a few classes
|
Kiley smith
Greenhorn
Joined: Jun 17, 2011
Posts: 27
|
|
Hi everyone,
I have:
First class:1) Product
Subclasses of Parent: 1.1) Cheese - 1.2) ComputerParts - 1.3) Service - 1.4) Fruit
SubSubclasses of Computer parts: 1.2.1) Ram - 1.2.2) Peripheral
Sub Sub Subclasses of Peripheral: 1.2.2.1) Printer - 1.2.2.2) Monitor
I need to code a subclass of a class called GenericOrder and call it ComputerOrder which will take an arbitrary number of only ComputerParts Objects, Peripheral Objects and Service Objects.
BTW my GenericsOrder class is:
I'm very stuck, I'm afraid this is as far as I've gotten:
*** but this is incorrect: i need to be able to take an arbitrary number of different classes of ComputerPart objects, Peripheral objects and Service objects.
Many thanks,
|
 |
Andrey Kozhanov
Ranch Hand
Joined: Mar 12, 2010
Posts: 79
|
|
|
Create some declarative interface (for example, call it IComputer) and implement it in classes you want to add to a list. Then your list will be declared like this: ArrayList<IComputer>.
|
 |
Kiley smith
Greenhorn
Joined: Jun 17, 2011
Posts: 27
|
|
|
This sounds promising but I don't really know how to do it. I know how to create just an interface but not one in a class.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32631
|
|
|
There is a lot of controversy about tagging interfaces, but this is how you can do it
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32631
|
|
|
You can probably also do it with an annotation, but I am not sure how. Anybody else?
|
 |
Kiley smith
Greenhorn
Joined: Jun 17, 2011
Posts: 27
|
|
Hi everyone,
Thank you for sticking with me. I have posted the class structure here: www.netmatrix.com/classes.jpg
This is where I am at:
This doesn't seen quite right to me. I understand interfaces cannot have any logic (definitions in them) so I need somehow check that only ComputerPart, Peripheral or Service objects are permitted. Did I create the interface correctly or is this what it is supposed to be:
and do error checking in ComputerOrder?
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4163
|
|
final edit: Removed irrelevant stuff after modifying the code
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2770
|
|
So I guess we're done with this other thread?
http://www.coderanch.com/t/543458/java/java/Generics-tuples-panic
|
 |
Kiley smith
Greenhorn
Joined: Jun 17, 2011
Posts: 27
|
|
Hi Darryl and Mike,
Darryl's solution works, thank you. Could some help me understand why my version of this does not work:
My line "myComputerOrder.add(new ComputerOrder(1));" is wanting an IComputerOrder object. Can someone explain this to me? I really want to understand this. Thank you.
|
 |
Kiley smith
Greenhorn
Joined: Jun 17, 2011
Posts: 27
|
|
Hi Mike
Yes, this one can be closed or deleted as it's live over here. Thanks
http://www.coderanch.com/t/543458/java/java/Generics-tuples-panic
|
 |
Kamal Tripathi
Ranch Hand
Joined: Oct 02, 2008
Posts: 86
|
|
Kiley smith wrote:Hi Darryl and Mike,
Darryl's solution works, thank you. Could some help me understand why my version of this does not work:
My line "myComputerOrder.add(new ComputerOrder(1));" is wanting an IComputerOrder object. Can someone explain this to me? I really want to understand this. Thank you.
Darryl's solution modifies ComputerPart as an interface (rather than a concrete object as the problem suggests). Guessing from what you are trying to implement (after inferring from other answers), your problem may be around the point where you declare interface (IComputerOrder) and how ComputerPart is defined as concrete class. My guess is that your ComputerPart concrete class doesn't implement IComputerOrder interface.
Can you post the code for IComputerOrder and ComputerPart. I am just guessing so I may be wrong.
|
Kamal Tripathi
SCJP 1.4 90%, SCWCD5 94%, Next SCDJWS--> In Naescent stage. Researching abt exam and material itself.
|
 |
 |
|
|
subject: a subclass taking unlimited objects of only a few classes
|
|
|