1

The program below calculates the total of the areas of any number of shapes instantiated. The problem is that I don't know how to output the draw() method of the interface.

Here is the main class:

public class MyClass1{

public static void main(String[] args) {


    Shape[] shapes= new Shape[3];


    shapes[0]= new Circle(20.0);
    shapes[1]= new Rectangle(25, 63);
    shapes[2]= new Circle(25);

   double total=0;

  for(int i=0; i<shapes.length; i++){

    total+= shapes[i].area();

}

System.out.printf("Total: %.1f", total);

  }

 }

The superclass Shape

 abstract class Shape {

  abstract double area();

}

The interface Drawable

public interface Drawable {
    public abstract void draw();

}

The subclass Circle

public class Circle extends Shape implements Drawable {

    double radius;

    Circle(double aRadius){

    radius= aRadius;
}


   double area(){

       return Math.PI*radius*radius;
}

    public void draw(){
       System.out.println("This is a circle");
  }

}

The subclass Rectangle

  public class Rectangle extends Shape implements Drawable {

   double width, height;

   public Rectangle(double aWidth, double aHeight){

    width= aWidth;
    height= aHeight;

 }

    double area(){

       return width*height;
    }

   public void draw(){

       System.out.println("This is a rectangle.");
    }
  }

I assumed that to print out the draw() method,the code should be like this:

Shape obj= new Rectangle();
Shape obj1= new Circle();

obj.draw();
obj1.draw();

But it's not working out. Would like to know the correct way to print the draw method along with some explanation since I am a newbie in Java. Thanks.

1

3 Answers 3

6

The draw method is not available, because you have Shapes in your array, not Drawables. You could theoretically have UndrawableShape that extends Shape but doesn't implement Drawable, so the compiler won't let you call a draw() method that may not exist.

To call draw(), you can do the following:

Instead of having each concrete subclass implement Drawable, have the Shape class implement Drawable. Shape is abstract, so it doesn't have to implement draw() itself; subclasses will need to implement that method.

5
  • I don't quite get what you mean.. Can you show me how through the codes?
    – Tia
    Jun 15, 2015 at 21:43
  • Take implements Drawable off of Rectangle and Circle, and any other concrete subclasses of Shape you have, and put implements Drawable on Shape.
    – rgettman
    Jun 15, 2015 at 21:47
  • I guessed that's what you meant at at first. I tried it but since Shape is an abstract class, it cannot be instantiated so how will I access the draw() method? That's what I didn't get actually
    – Tia
    Jun 15, 2015 at 22:07
  • Because the Shape class is abstract, it doesn't have to implement the draw() method. But any concrete subclass such as Rectangle must implement the draw() method. Because of polymorphism, the runtime type of the Shape object in the array determines which draw() method will be run -- that of the concrete subclass.
    – rgettman
    Jun 15, 2015 at 22:08
  • In abstract class ex. Filling could be a color Aug 28, 2021 at 5:03
0

The problem is you're using the Shape type as your objects. Java sees those as Shapes, not Rectangles or Circles, and the Shape class has no draw() method implementation. You can either do Rectangle obj1 = new Rectangle() or Shape obj1 = new Rectangle then later cast it like ((Rectangle) obj1).draw() (the parentheses are because casting is a low priority operation and you must cast before you can access methods and fields that are class specific).

0

Here I am going to define a Shape class to show Inheritance (how other shapes such as Triangle, Circle and Rectangle inherit from Shape class) Remember Shape class is abstract, while Triangle for example is a concrete class, Triangle class is a child of Shape class and Shape is a parent class. I used Java program in order to show this.

package inheritancePackage;

/* PARENT CLASS – abstract class */
public abstract class Shape {
    protected double area;

    public abstract void draw();

    public abstract double getArea();


}

package inheritancePackage;

/* CHILD CLASS – concrete class, Circle */

public class Circle extends Shape{
    private double radius;

    public void draw() {
        System.out.println("I am drawing a circle");
    }

    public Circle(){
        radius = 0.0;
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    //getter and setter
    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return calculateArea();
    }

    //Area of circle
    private double calculateArea() {
        return radius * radius * Math.PI;
    }

    public String toString() {
        return "The radius of the circle is: " + radius + ", and the area is: "
                + getArea();
    }
}

package inheritancePackage;

/* CHILD CLASS – concrete class, Triangle */

public class Triangle extends Shape {
    private double base, high;

    public void draw() {
        System.out.println("I am drawing  a Triangle");
    }

    public Triangle(){
        base = 0.0;
        high = 0.0;
    }

    public Triangle(double base, double high) {
        this.base = base;
        this.high = high;
    }

    //getters and setters
    public double getBase(){
        return base;    
    }

    public void setBase(double base){
        this.base = base;

    }

    public double getHigh(){
        return high;    
    }

    public void setHigh(double high){
        this.high = high;

    }

    @Override
    public double getArea() {
        return calculateArea();
    }

    //Area of Triangle
    private double calculateArea(){
        area = (base * high) / 2;
        return area;
    }

    public String toString() {
        return "The base of the triangle is: " + base + " and the high is: " + high + ", and the area is: "
                + getArea();
    }

}

package inheritancePackage;

/* CHILD CLASS – concrete class, Rectangle */

public class Rectangle extends Shape {
    private double length, width;

    public void draw() {
        System.out.println("I am drawing  a Rectangle");
    }

    Rectangle(){
        length= 0.0;
        width = 0.0;        
    }

    Rectangle(double length, double width){
        this.length =length;
        this.width = width;
    }

//getters and setters

    public double getLenght() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    @Override
    public double getArea() {
        return calculateArea();
    }

    //Area of Rectangle
    private double calculateArea(){
        return area = width * length;
    }

    public String toString(){
        return "The width of the rectangle is: " + width + " and the length is: " + length + ", "
                + "and the area is: " + getArea();
    }

}

package inheritancePackage;

/* CHILD CLASS – concrete class, Star */

public class Star extends Shape {
    public void draw() {
        System.out.println("I am drawing  a Star");
        System.out.println("No area has been calculated for this kind of shape");
    }

    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return 0;
    }
}


package inheritancePackage;

/* This class is used to test and pass value through the respective constructors*/

public class TestShape {

    public static void main(String [] args) {

        Circle circle = new Circle(5);
        Rectangle rectangle = new Rectangle(10, 5);
        Triangle triangle = new Triangle(8.0,5.0);
        Star star = new Star();

        circle.draw();
        System.out.println(circle.toString()); 

        rectangle.draw();
        System.out.println(rectangle.toString());

        triangle.draw();
        System.out.println(triangle.toString());

        star.draw();

    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.