Link to home
Start Free TrialLog in
Avatar of Cheney
Cheney

asked on

Inhertiance??

I know this is a lame question but...

I have a class called Engine:

public class Engine extends Thread
{
     protected final int MAX_SPEED = 10;

     public void run()
     {
     .... Run code here
     }

    ... Rest of class...
}

and a derived class

public class SuperEngine extends Engine
{
     protected final int MAX_SPEED = 50;
}

Now I implement two other classes

public class Car
{

     protected Engine engine;

     public Car()
     {
          engine = new Engine();
          engine.start();
     }
}

and...

public class SuperCar extends Car
{
     public SuperCar()
     {
          engine = new SuperEngine();
          engine.start();
     }
}

Some other code I have written shows the cars moving in an applet window:

public class Drawer
{
     public void draw()
     {
          Engine engine1 = car.getEngine();
          Engine engine2 = supercar.getEngine();

         // ... Do stuff - the point is that this class doesnt know what type of engines each of the cars have so it uses the parent
         // class
     }
}

My problem is that the both the Car and the SuperCar have the same max speed!  grrrr. How do I force the SuperCar class to use the SuperEngine's MAX_SPEED variable rather than the Engine's MAX_SPEED even though it is basically declared as "Engine engine = new SuperEngine();" and referenced as an instance of Engine?
(In cpp I'd use virtual variables but Java doesnt do those!)

Thanks for your help..
PS. Casting is a no go.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

public class Engine extends Thread
{
     protected int MAX_SPEED = 10;

     public Engine(int maxspeed)
     {
        MAX_SPEED = maxspeed;
     }

     public void run()
     {
     .... Run code here
     }

    ... Rest of class...
}

and a derived class

public class SuperEngine extends Engine
{
   public SuperEngine()
   {
      suoer(50);
   }
}
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pjgould
pjgould

The problem is to do with 'Variable Shadowing' of the field MAX_SPEED in the extended SuperEngine class.

In case you're interested, here's a link to a page giving an explanation of why the above solution works (variable shadowing vs. method overriding):

http://builder.com.com/5100-6370-5031837.html#Listing%20B

It has an example that reflects your problem exactly.

All you need to change in your code is the addition of the getMaxSpeed(){ return MAX_SPEED; } to Engine and SuperEngine.
Yes, of course, because when you access the MAX_SPEED variable directly with 'first' or 'second', then it will print the value of 10. However, while invoking the method, the dynamic dispatch will come into play. That's why I told him that first.getMaxSpeed () and second.getMaxSpeed () will work, but first.MAX_SPEED and second.MAX_SPEED will give the value 10 for both. (Another reason why I prefer never to access class-members directly, unless it is mandatory to do so.)

Cheers,
Mayank.