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