This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a subclass taking unlimited objects of only a few classes

 
Kiley smith
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a lot of controversy about tagging interfaces, but this is how you can do it
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably also do it with an annotation, but I am not sure how. Anybody else?
 
Kiley smith
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


final edit: Removed irrelevant stuff after modifying the code
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I guess we're done with this other thread?

http://www.coderanch.com/t/543458/java/java/Generics-tuples-panic
 
Kiley smith
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic