Re: Am i violating oop rules
This could be a bit of a debate apparently but it works and minimizes code. Thanks for any replys :). Also i gues i dont need to include the interface into the speedboat class either
Re: Am i violating oop rules
If you're going to have data in the interface, it will be constant, and must be given a value to be useful. If you want to set the values of enginePower and brakePower in subclasses, they can't go in the interface - put them in an abstract class (AbstractVehicle ?). Also, you can't set primitive type 'double' to null. You can set it to a value or leave it to default to 0.0. Instead of having a static engine power constant in the SpeedBoat class, initialize the inherited engine power constant in the SpeedBoat constructor.
Don't write methods in the subclass to call methods in the superclass unless they have something to add. Don't use methods with the same names but different capitalisation - Use the Java naming standards: classes start with an upper-case letter, methods and variables start with a lower-case letter. Constants are often written all upper-case with underscore word-separators.
Why do the subclass methods incgear and decgear call the superclass methods IncGear and DecGear and ignore their return values? If a user doesn't want the return value, he can ignore it anyway.
As far as I can see, SpeedBoat doesn't need any of its current contents, just a constructor that sets the engine power. In principle, you don't even need a SpeedBoat class, because you could just create a Boats (shouldn't that be 'Boat'?) with the appropriate engine power, e.g:
Code:
class AbstractVehicle implements IVehicle {
private double EnginePower;
private double BreakPower;
...
}
class Boat implements AbstractVehicle {
// Boat engine power constants (could be made into an enum)
private static final double SPEEDBOAT_POWER = 20.0;
private static final double ROWBOAT_POWER = 0.5;
...
// constructor to set the power
public Boat(double enginePower) {
this.enginePower = enginePower;
}
...
public Boat getSpeedboat() {
return new Boat(SPEEDBOAT_POWER);
}
public Boat getRowboat() {
return new Boat(ROWBOAT_POWER);
}
...
}
This is just one suggestion, there are many other other ways of doing it. You may actually need an abstract Boat class if there are methods that only a Boat subclass can implement, so you may need SpeedBoat class if a SpeedBoat can do more stuff than an ordinary Boat, or does it differently, but the principles are the same.
The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents...
N. Borenstein
Re: Am i violating oop rules
Thanky you DLorde once again you have meticulously picked my code apart and shown me ways i can better myself :). I was thinking of sailboats and adding some extra methods to the abstract boat class for that.
Putting the EnginePower varaible into the interface i should have done . And i will never violate naming rules agains (just as soon as I look them up).