|
-
September 8th, 2009, 06:15 AM
#1
Am i violating oop rules
I have an interface that requires my vehicles to have a engine power and break power. My abstract class of boat has a but every type of boat(sub-classes) has its own engine power. My abstract car class leaves it up to the subclasses how much brake power they have. Is the boat abstract class wrong
heres so code to help clear it up
PHP Code:
[code]
interface IVehicule{
double EnginePower = null;
double BreakPower = null;
double Decelerate(double speed, int gear, double brake);
double Accelerate(int gear, double EnginePower);
int IncGear();
int DecGear();
}
abstract class Boats implements IVehicule
{
private static final double Brake = 5;
protected double speed;
protected int gear;
public double Decelerate (double speed, int gear, double brake)
{
return this.speed=speed/brake+(gear/2);
}
public int DecGear ()
{
return this.gear--;
}
public int IncGear ()
{
return this.gear++;
}
public double Accelerate (int gear, double EnginePower)
{
return gear*EnginePower;
}
}
class SpeedBoat extends Boats implements IVehicule{
public static double EnginePower = 20.0;
public void decelerate(double speed,int gear,double brake){
Decelerate(speed, gear, brake);
}
public void accelerate(int gear){
Accelerate(gear, EnginePower);
}
public void incgear(){
IncGear();
}
public void decgear(){
DecGear();
}
}/* does this class need to
** implement its own field
** decleration for brake or
** is it fine in the abstract class? */
[/code]
-
September 8th, 2009, 06:34 AM
#2
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
-
September 8th, 2009, 07:42 AM
#3
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
Last edited by dlorde; September 8th, 2009 at 07:52 AM.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 9th, 2009, 03:37 AM
#4
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).
Last edited by socialite; September 9th, 2009 at 03:38 AM.
Reason: spelling
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|