Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Inhertiance??

Posted on 2003-11-27
4
Medium Priority
?
228 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:Cheney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9834991
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);
   }
}
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 9835159
Can you post your entire code? I guess the second class should use MAX_SPEED = 50 ;

I tried out this piece of code. See the output:

class Engine
{
      protected final int MAX_SPEED = 10 ;

      protected int getMaxSpeed ()
      {
            return MAX_SPEED ;

      } // end of getMaxSpeed ()

} // class definition over

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

      protected int getMaxSpeed ()
      {
            return MAX_SPEED ;

      } // end of getMaxSpeed ()

} // class definition over

class Car
{
      protected Engine engine ;

      Car () // constructor ()
      {
            engine = new Engine () ;

      } // end of constructor ()

      protected Engine getEngine ()
      {
            return engine ;

      } // end of getEngine ()

} // class definition over

class SuperCar
{
      protected Engine engine ;

      SuperCar () // constructor ()
      {
            engine = new SuperEngine () ;

      } // end of constructor ()

      protected Engine getEngine ()
      {
            return engine ;

      } // end of getEngine ()

} // class definition over

public class TestClass
{
      public static void main ( String args[] )
      {
            Car car = new Car () ;
            SuperCar supercar = new SuperCar () ;
            Engine first = car.getEngine () ;
            Engine second = supercar.getEngine () ;
            System.out.println ( "CAR: " + first.getMaxSpeed () ) ;
            System.out.println ( "SUPERCAR: " + second.getMaxSpeed () ) ;

      } // end of main ()

} // class definition over

It prints:

CAR: 10
SUPERCAR: 50

However, if you print first.MAX_SPEED and second.MAX_SPEED instead of invoking the getMaxSpeed () methods, then it will print 10 for both.

Mayank.
0
 

Expert Comment

by:pjgould
ID: 9835987
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9836056
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question