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

OO, basics

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I found some examples of coursework, and Im trying to use them to learn better OO programming

the excerpt of specs :


* Mountain Bike (�20) � which can be booked with an optional helmet (�5). These come with a ladies or gents frame in sizes small/ medium/ large.
* Racing bike (�25) � this can be booked with an optional helmet and panniers (�10). These come with a ladies or gents frame in sizes small/ medium/ large.
* Children�s bike (�15) � this includes a cycle helmet which must be worn. These come in ages 4 to 14.



what I did, I created basic class Bike which implements Rentable,
Now I have question about Mountain Bike and Racing one,
Is it better to create one class for example


AdultBike extends Bike {
FrameType frameType;
FrameSize frameSize;
List<Options> options;

public AdultBike(String name, int price, ... ) {
super(name, price);
}
}



because those 2 types differ in numbers of options

or 2.way create MountainBike extends Bike, and then RacingBike extends MountainBike. but it doesnt sound good, because in real world racing bike doest come from mountain bike.

Thank you for your help!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're thinking about the right things here.

I'd probably look to put the common things in the base Bike class. They all have a Frame and a collection of options, so let's move those up to Bike:

Now MountainBike doesn't have to do anything different except plug in a Frame and some options.

To your question about RacingBike extending MountainBike, I like your instinct that this doesn't seem right. You can make both extend the base Bike. Also, it's good to avoid extending a concrete class. That's why I added "abstract" to Bike.

Note that since Frame has several attributes - ladies or gents, size, etc - I thought it was interesting enough to be its own class. Options may have some interesting bits, too, like optional or required. You already specified an Options class, so you're ready for that.

Is that making sense so far?
 
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic