7 Errors in java code related to inheritance. Please assist me. Thank you very much.?
Hi Everyone
The code is: public abstract class Ball
{
public String type;
public Ball(String type)
{
this.type = type;
}
abstract public void play();
public String getType()
{
return type;
}
public abstract class Bounceable extends Ball
{
public Bounceable()
{
super("Bounceable");
}
abstract public String bounce();
}
public abstract class Baseball extends Ball
{
public Baseball(String msg)
{
super(msg);
}
public String usage()
{
return new String("The American pasttime - Batter up!");
}
}
public abstract class Bowlingball extends Ball
{
public Bowlingball(String msg)
{
super(msg);
}
public String usage()
{
return new String("Set up the pins. Do you know how to score?");
}
}
public abstract class Tennisball extends Bounceable
{
public Tennisball()
{
super("Tennisball");
}
public void play()
{
System.out.println("All set for a match. I love this game");
}
public String bounce()
{
String bounce = "Boing!";
return bounce;
}
}
public abstract class Basketball extends Bounceable
{
public Basketball()
{
super("Basketball");
}
public void play()
{
System.out.println("Basketball begins with a tipoff.");
}
public String bounce()
{
String bounce = "dribble...";
return bounce;
}
}
public class PolyTest
{
public static void main(String[] args)
{
Ball[] ball = {Baseball(),Basketball(),Bowlingball(),Tennisball()};
System.out.println("For each type of ball...\n");
for(int i = 0; i < ball.length; ++i)
{
System.out.println("Ball #"+(i+1)+" is a "+ball[i].getType());
System.out.println("Playing: ");
ball[i].play();
//checks to see if it an object of type Bounceable
if( ball[i] instanceof Bounceable)
//if so, call bounce method of Bounceable object
System.out.println("Bouncing");
else
System.out.println("You can't bounce this ball");
System.out.println("\n");
}
System.out.println("\nEnd of program.");
}
}
}
The Errors are: java:66: error: constructor Bounceable in class Ball.Bounceable cannot be applied to given types;
super("Tennisball");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
java:85: error: constructor Bounceable in class Ball.Bounceable cannot be applied to given types;
super("Basketball");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
java:104: error: cannot find symbol
Ball[] ball = {Baseball(),Basketball(),Bowlingball(),Tennisball()};
^
symbol: method Baseball()
location: class Ball.PolyTest
java:104: error: cannot find symbol
Ball[] ball = {Baseball(),Basketball(),Bowlingball(),Tennisball()};
^
symbol: method Basketball()
location: class Ball.PolyTest
C:\Course Technology\85985-0d\Chapter10\Ball.java:104: error: cannot find symbol
Ball[] ball = {Baseball(),Basketball(),Bowlingball(),Tennisball()};
^
symbol: method Bowlingball()
location: class Ball.PolyTest
.java:104: error: cannot find symbol
Ball[] ball = {Baseball(),Basketball(),Bowlingball(),Tennisball()};
^
symbol: method Tennisball()
location: class Ball.PolyTest
C:\Course Technology\85985-0d\Chapter10\Ball.java:102: error: Illegal static declaration in inner class Ball.PolyTest
public static void main(String[] args)
^
modifier 'static' is only allowed in constant variable declarations
The errors only start after the 3rd part:
1st i had to create an abstract base class, Ball. The single constructor requires a string to indicate the type of ball which is then stored in an instance variable. Instance method getType(), returns a new string containing the type of ball. The class declares an abstract method play(), which returns void. Then i have to create an abstract class Bounceable which inherits from ball which has a single abstract method bounce() which returns a string but n no instance variables. then create 2 concrete classes Baseball and Bowlingball, which inherits from ball. only implementation of inherited abstract methods. no instance variables or additional methods required. My code compiles up till this point, then the problem comes in here: Next i have to create 2 concrete classes Tennisball and Basketball which inherit from Bounceable. No instance variables or methods required only implementation