File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes OO, basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "OO, basics" Watch "OO, basics" New topic

OO, basics

Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
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!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: OO, basics
It's not a secret anymore!