• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

a subclass taking unlimited objects of only a few classes

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a lot of controversy about tagging interfaces, but this is how you can do it
 
Campbell Ritchie
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


final edit: Removed irrelevant stuff after modifying the code
 
Master Rancher
Posts: 5059
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I guess we're done with this other thread?

https://coderanch.com/t/543458/java/java/Generics-tuples-panic
 
Kiley smith
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike

Yes, this one can be closed or deleted as it's live over here. Thanks

https://coderanch.com/t/543458/java/java/Generics-tuples-panic
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic