Directions:
Do the steps below to learn more about inheritance.
0) Start by cutting and
pasting into an editor, saving, and then running the code below. The color should
print out as null. Fix this by writing a constructor method for class TrafficLight that sets color to "Red". Run it to
make sure it works.
1) Uncomment up to just past
//1. Compile it. Notice the cryptic compiler error message. Now, define a new
class, TrafficLightDeluxe. Since the statement you
have just uncommented calls the TrafficLightDeluxe(...)
constructor with an argument, and the default constructor has none, you have to
define a constructor for your new class. Do that, making sure it takes a boolean argument. The body can just be an opening brace
followed by a closing brace: { }. Now it should compile. See hint below
only if necessary.
2) Uncomment up to just past
//2. The same problem occurs as in step 1), so define a UltraDeluxeLight
class. This time the default constructor is sufficient, so do not write a
constructor. Make sure the result runs.
3) Uncomment up to just past
//3. To make the program run, you will have to define a toggleBlinking
method. This method should toggle a private boolean
variable called, blinking. Make sure it runs.
4) Uncomment up to just past
//4. Try compiling. Note the two methods not in TrafficLightDeluxe.
To fix the problem with getColor(), make TrafficLightDeluxe "extends" TrafficLight.
Think: why does this work? Next, to fix the problem with getBlinking(
), write a getBlinking( ) method for TrafficLightDeluxe. This method simply returns the boolean value of the 'blinking' variable.
5) Uncomment up to just past
//5. Try compiling. Note the two methods not in UltraDeluxeLight.
One of these is in TrafficLight. But BOTH are in TrafficLightDeluxe, because TrafficLightDeluxe
defines getBlinking( ) and inherits getColor( ). To fix, therefore, you can have UltraDeluxeLight inherit from TrafficLightDeluxe.
Try this, compile, and both the previous error messages should disappear (to be
replaced by a new one).
6) UltraDeluxeLight
has a default constructor which is UltraDeluxeLight(
). Since UltraDeluxeLight( ) is not defined (you
haven't written it), it simply tries to inherit and run the default
(no-argument) constructor from TrafficLightDeluxe.
This default constructor does not exist however since you have overridden it by
defining a constructor for TrafficLightDeluxe, which
takes an argument. Solution: write a no-argument constructor for UltraDeluxeLight that simply calls the existing constructor
for TrafficLightDeluxe. Java syntax requires this
call to use the name "super(...)" not the name TrafficLightDeluxe(...),
but a call to super(...) is in fact a call to TrafficLightDeluxe(...).
Make sure it compiles.
7) Uncomment up to just past
//6 (leaving nothing still commented out). Compile and run. How is it that lincolnAndDuff.stepLight( ) works when there is no stepLight method that you defined for UltraDeluxeLight?
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Hint for step 1 (try it without the hint first, of course). This code should work:
class TrafficLightDeluxe
{TrafficLightDeluxe(boolean b){}
}
Hint for step 2.
This code should suffice:
class UltraDeluxeLight{ }
Hint for step 3.
private boolean blinking;
…
public void toggleBlinking( ){
if (blinking) blinking=false; else blinking=true;
}
Hint for step 4.
class TrafficLightDeluxe extends TrafficLight {
…
public boolean getBlinking( ){
return (blinking);
}
Hint for step 5.
class UltraDeluxeLight extends TrafficLightDeluxe{
}
Hint for step 6.
UltraDeluxeLight ( ){super(false);}
Code to download
and begin with:
public class Traffic
{
public static void main
(String args[])
{
TrafficLight RockAndBlock
= new TrafficLight( );
System.out.print
("Intersection RockAndBlock: color on Rock is ");
System.out.println(RockAndBlock.getColor( ));
/*
TrafficLightDeluxe LincolnAndWelch
= new TrafficLightDeluxe(false); //1
UltraDeluxeLight LincolnAndDuff
= new UltraDeluxeLight( ); //2
LincolnAndWelch.toggleBlinking( ); //3
("Intersection
LincolnAndWelch: Lincoln is ");
System.out.print
(LincolnAndWelch.getColor( ));
System.out.print(", ");
System.out.println
(LincolnAndWelch.getBlinking( )); //4
System.out.print
("Intersection LincolnAndDuff: Lincoln is ");
System.out.print(LincolnAndDuff.getColor( ));
System.out.print(", ");
System.out.println(LincolnAndDuff.getBlinking( )); //5
LincolnAndDuff.stepLight( );
System.out.print
("Intersection LincolnAndDuff is: ");
System.out.println(LincolnAndDuff.getColor( )); //6
*/
}
}
class TrafficLight
private String color;
public void stepLight( )
{wait(30);
changeColor( );
}
private void wait(int seconds){
}
public String getColor( ){
return (color);
private void changeColor( ){
}
}