• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic