This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Question" Watch "Question" New topic
Author

Question

Priya Mandal
Greenhorn

Joined: Oct 04, 2012
Posts: 2
I have two classes one is APPLE and the other is Bag of APPLES.Which one would I have as a base class and why?Would like to know how would we implement it?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 792
    
    2

Welcome to the Ranch!

Please UseAMeaningfulSubjectLine for the topics you start.

As for your question, the answer is - none. The relationship between Apple and BagOfApples (if they really reflect what their names are) does not correspond to the relationship inheritance represents.
For instance you might have one class, say Fruit, that would be the base class for your Apple class.

Check tutorial on Inheritance to get a better understanding of what it actually is.


The quieter you are, the more you are able to hear.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

If you can say "ClassA IS A ClassB", then you have a case for making a sub-class. As Kemal implied, and Apple is a Fruit, so Fruit would be the base class, and Apple would extend it.

Your example is one of composition. a BagOfApples CONTAINS Apples. So, your BagOfApples would most likely have some kind of collection (an ArrayList, a HashMap, etc) that you put Apples into.


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Question
 
Similar Threads
"Foundation..." The Winners [Announced]
Dead Mermaid Found After Tsunami
dynamic/static ip
Clarification on lock tracking
What would you choose?