1

I am beginner in java, trying to create an abstract method abstract int area() of a abstract class Shape, the Shape class extended on three other classes and overridden area method of these class are calculating area and returns int value and i want to store in Shape class array.

import java.util.Scanner;
abstract class Shape{
abstract int area();
}

class Ractangle extends Shape {
    private int length,width;
    public Rectangle(){
        System.out.println("Enter length rectangle ");
        this.length = Test.scan.nextInt();
        System.out.println("Enter width rectangle ");
        this.width = Test.scan.nextInt();
    }
    public int area(){
    return length*width;
    }
}

class Square extends Shape{
    private int side;
    public Square(){
        System.out.println("Enter side square ");
        this.side = Test.scan.nextInt();
    }
    public int area(){
        return side*side;
    }
}
class Parlellogram extends Shape{
    private int base,height;
    public Parallelogram(){
        System.out.println("Enter base parallelogram");
        this.base = Test.scan.nextInt();
        System.out.println("Enter height parallelogram ");
        this.height = Test.scan.nextInt();
    }
    public int area(){
        return base*height;
    }
}
class Test{
    public static Scanner scan;
    public static void main(String[] args){
        scan = new Scanner(System.in);
        Rectangle rect = new Rectangle();
        Square sq = new Square();
        Parallelogram prlg = new Parallelogram();

        Shape[] shapeArray = {rect.area(), sq.area(), prlg.area()}; //error here

        //shapeArray[0] = rect.area();
        //shapeArray[1] = sq.area();
        //shapeArray[2] = prlg.area();
    }
}

error: incompatible types: int cannot be converted to Shape

5 Answers 5

2

Did perhaps mean to write this?

int[] shapeArray = {ract.area(), sq.area(), prlg.area()};

Or tihs?

Shape[] shapeArray = {ract, sq, prlg};

If you compute the area of a shape, you no longer have a Shape, but an int.

Sign up to request clarification or add additional context in comments.

Comments

1

Java - is a statically typed language. That means that you cannot store int values inside of the array of Shape. In order to resolve the issue, you should create an array of integers:

int[] shapeArray = { ract.area(), sq.area(), prlg.area() };

Comments

1

Exception itself says about issue int cannot be converted to Shape, change the array data type

 int[] shapeArray = {ract.area(),sq.area(),prlg.area()};

the reason is area() method returning value of type int not shape.

Comments

0

Your Shape[] is an array of shapes (just rename shapeArray to shapes and you'll see it) but you are trying to put areas in it.

So from an design point of view, if those are areas, it should be Shape[] areas. Because an area is not a Shape your array needs to be of the type, the areas are, that is int[] areas.

So this should be

int[] areas = {rect.area(), sq.area(), prlg.area()};

Also, try more sophisticated types like List<T>

List<Integer> areas = new List<>() {{
    add(ract.area());
    add(sq.area());
    add(prlg.area());
}};

Comments

0

You are trying to put primitive type(int) in an array of an object type(Shape), which is illegal.

If you meant to "put area values in an array", you have to create an array of type int. Please do the following;

int[] areaArray = {ract.area(),sq.area(),prlg.area()};

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.