Lapo
Lapo

Reputation: 992

Java classes and subclasses logic

I'm learning about Java classes and this example came on my mind:

Let's say I'm developing an app that can create cars. Each car is made up of some components (engine, tires, ...) and each component gives the car a boost in some areas (for example the engine boosts the acceleration).

To develop this I thought I can build a class "Cars" which has some variables that hold which components that car is equipped with. Then I create a base class "Components" which holds the characteristic common to all components (their weight, size, ...) and some generic functions. Then I create some subclasses of the "Components" class which define a specific component (for example I create the class "Engines" or "Tires"). Of course each engine can be of different brands (BMW, Audi, ...) and each brand has different characteristics (let's say Audi engines accelerate faster than BMW ones).

Now the question is, do I have to create different subclasses for each brand engine? Of course I can, but is this the correct way to build these kinds of "structures"?

And for example, if, after I build all the classes, I notice that I have to add another characteristic to all engine (for example maximum speed) I have to change all the subclasses of each specific brand engine, but this doesn't sound very "efficient".

Upvotes: 0

Views: 132

Answers (4)

mad.meesh
mad.meesh

Reputation: 2766

you're on the right track but you're running into decisions that all designers face and there is no clear answer.

but to expand you could specifically define behaviours.. for example, you could start with your base class car. then you could define an interface such as:

interface driveable
{
     void drive(engine type_of_engine, tire type_of_tire);
}

interface moddable
{
    void tune(exhuast type_of_exhaust);
}

then with car:

class car : driveable
{
    void drive(...)
}

class bmw : car {...}
class audi : car, moddable {...}

Upvotes: 1

PatrycjaMirko
PatrycjaMirko

Reputation: 46

I think you can use polymorphism and inheritance. For example, you create a class of slinics - each has the same variables (brand, power, etc.). Then, when calling the class constructors, you can initialize variables (assign them "BMW", 130 etc.) It all depends on how your application works. If this is not the answer you want, specify the question and I will try to help.

Upvotes: 0

jingx
jingx

Reputation: 4014

If the difference can be expressed with different values of one same data attribute, and does not require different code paths to process those values, then you don't need separate subclasses for them.

For instance, if you are calculating 0-60, you would have a generic formula, to which you plugin a particular engine's acceleration parameters. There is no need for separate AudiEngine and BMWEngine classes.

OTOH, if an Audi engine has the additional feature of making a car invisible, then you do need a special AudiEngine class, with a special enableInvisibility() method on it.

Rule of thumb: if you find yourself writing if/else blocks that check on object types and do different things accordingly, you need subclasses.

Upvotes: 0

Timothy Truckle
Timothy Truckle

Reputation: 15622

The "rule of thumb" is: you need separate classes if there is different behavior.

eg.: An engine has a different behavior than a Tire, but a Audi engine has the same behavior as a BMW engine, just the property "amount of acceleration" differs, which is a configuration, not behavior.

Upvotes: 0

Related Questions