How do you override a superclass's methods with a subclass? These are my two programs, the first being the super class, and the second being the subclass of which I am attempting to override the superclass's methods.
// This class represents a walker with two feet.
import java.awt.Image;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Walker extends JPanel
{
public static final int PIXELS_PER_INCH = 6;
private Foot leftFoot, rightFoot;
private int stepLength;
public int stepsCount;
// Constructor
public Walker(int x, int y, Image leftPic, Image rightPic)
{
leftFoot = new Foot(x, y - PIXELS_PER_INCH * 4, leftPic);
rightFoot = new Foot(x, y + PIXELS_PER_INCH * 4, rightPic);
stepLength = PIXELS_PER_INCH * 12;
}
// Returns the left foot
public Foot getLeftFoot()
{
return leftFoot;
}
// Returns the right foot
public Foot getRightFoot()
{
return rightFoot;
}
// Makes first step, starting with the left foot
public void firstStep()
{
leftFoot.moveForward(stepLength);
stepsCount = 1;
}
// Makes next step
public void nextStep()
{
if (stepsCount % 2 == 0) // if stepsCount is even
leftFoot.moveForward(2 * stepLength);
else
rightFoot.moveForward(2 * stepLength);
stepsCount++; // increment by 1
}
// Stops this walker (brings its feet together)
public void stop()
{
if (stepsCount % 2 == 0) // if stepsCount is even
leftFoot.moveForward(stepLength);
else
rightFoot.moveForward(stepLength);
stepsCount++; // increment by 1
}
// Returns the distance walked
public int distanceTraveled()
{
return stepsCount * stepLength;
}
// Draws this walker
public void draw(Graphics g)
{
rightFoot.draw(g);
leftFoot.draw(g);
}
}
// This class represents a subclass of Walker.
import java.awt.Image;
import java.awt.Graphics;
public class Bystander extends Walker
{
public static final int PIXELS_PER_INCH = 6;
private Foot leftFoot, rightFoot;
private int stepLength;
private int tapsCount;
// Constructor
public Bystander(int x, int y, Image leftPic, Image rightPic)
{
super(x, y, leftPic, rightPic);
}
// Returns the left foot
public Foot getLeftFoot()
{
return leftFoot;
}
// Returns the right foot
public Foot getRightFoot()
{
return rightFoot;
}
// Makes first step, starting with the left foot
public void firstStep()
{
leftFoot.turn(-45);
tapsCount = 1;
}
// Makes next tap
public void nextStep()
{
if (stepsCount % 2 == 0) // if stepsCount is even
leftFoot.turn(45);
else
rightFoot.turn(-45);
tapsCount++; // increment by 1
}
// Stops this walker (brings its feet together)
public void stop()
{
if (tapsCount % 2 == 0) // if stepsCount is even
leftFoot.turn(45);
else
rightFoot.turn(0);
tapsCount++; // increment by 1
}
// Returns the distance walked
public int distanceTraveled()
{
return(0);
}
// Draws this walker
public void draw(Graphics g)
{
super.draw(g);
super.draw(g);
}
}