I am trying to figure out abstraction for a project on Sololearn and I reached module 5. I am asked to create two classes ( Square and Circle) both with contructors taking parameters and to set up an abstract method inherited from an abstract class in order to calculate the area. I feel really stupid as I can't figure out what am I doing wrong. Firstly, I tried writing @Override above the method as I thought it will work. Second, I tried changing the return type of the overridden abstract method to void from int/double but in my head it didn't make any sense as it should return a number, be that an int or a double. Anyway, here is the code, hopefully someone can shed a light on this dilemma:
import java.util.Scanner;
abstract class Shape {
int width;
abstract void area();
}
//your code goes here
class Square extends Shape{
int area(int width){
return width*width;
}
Square(int width){
width = width;
}
}
class Circle extends Shape{
double PI = 3.14;
double area(int width){
return PI*width*width;
}
Circle(int width){
width = width;
}
}
public class Program {
public static void main(String[ ] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
Square a = new Square(x);
Circle b = new Circle(y);
a.area();
b.area();
}
}