Reputation: 1
I am getting a "cannot find symbol" error when I try to compile this code. In the main, I can create objects of type Ball no problem, but if I try to create object of type Basketball, compiler complains. I am very new to java, please help?
class Ball {
public double radious;
public String color;
Ball( double radious, String color ) {
this.radious = radious;
this.color = color;
}
public double area() {
return (4 * Math.pow(this.radious, 2) * Math.PI );
}
public void display() {
System.out.println("\nRadious: " + radious );
System.out.println("Color: " + color );
}
class Basketball extends Ball {
public int noOfStripes;
Basketball( int n, double r, String c ) {
super( r, c );
noOfStripes = n;
}
Basketball( double r, String c ) {
super( r, c );
noOfStripes = 8; // Default value
}
public void display() {
super.display();
System.out.println("Area: " + area() );
System.out.println("noOfStripes: " + noOfStripes );
}
}
}
class Driver {
public static void main( String[] args ) {
Ball basket1 = new Ball( 29.5, "Orange" );
basket1.display();
Ball basket2 =new Basketball( 29.5, "Black" );
}
}
Upvotes: 0
Views: 281
Reputation: 8386
Since Basketball
is an inner class of Ball
you have to create objectes the following way:
Ball.Basketball myBasketball = new Ball().new Basketball(/* foo arguments */);
But a better solution would be, to move it outside to be an own class.
Upvotes: 0
Reputation: 178293
You have created Basketball
not only as a subclass of Ball
, but as an inner class as well. It doesn't need to be an inner class.
Define Basketball
outside of the braces for the Ball
class.
}
} // End of Ball class
class Basketball extends Ball
{
// ...
Upvotes: 3
Reputation: 631
You need to qualify BasketBall
with Ball
:
Ball.Basketball basket2 = new Ball.Basketball( 29.5, "Black" );
As other comments and answers have pointed out, you should not use inner classes for inheritance. BasketBall should be a separate class:
public class BasketBall extends Ball {
}
Upvotes: 0