I know I can easily override methods when using inheritance. What I remember from C++ I could do the same with attributes.

 

Assume the following very simple example:

 

class Boat

{

  private int maxSpeed = 10;

  public int getMaxSpeed()

  { return maxSpeed; }

}

 

class SpeedBoat extends Boat

{

  private int maxSpeed = 20;

}

 

If I do like this:

  System.out.println("Boat maxspeed: " + boat.getMaxSpeed() );

  System.out.println("SpeedBoat maxspeed: " + speedBoat.getMaxSpeed() );

I would expect to get:

  Boat maxspeed: 10

  SpeedBoat maxspeed: 20

But what I get is:

  Boat maxspeed: 10

  SpeedBoat maxspeed: 10

 

To me this seems like a bug in Java. Or maybe I’m doing something wrong?

 

The only work-around I’ve found is to override a getter method for each attribute you want to override, but that forces a lot more code than seems necessary.

 

  Mattias Jiderhamn

  Expert Systems

  [EMAIL PROTECTED]

 

Reply via email to