Java.whats wrong with my code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java.whats wrong with my code

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Circle extends Shape { double pi = 3.14, ar; @Override public double area(int y) { super.area(); ar = pi * y * y; System.out.println("Area of circle:"+ar); } } class Square extends Shape { int ar; @Override public int area(int x) { super.area (); ar = x * x; System.out.println("Area of Square :"+ar); } } 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(); } }

3rd Feb 2021, 6:14 AM
mangesh choudhari
mangesh choudhari - avatar
1 Answer
0
1. your classes don't have constructors, and you don't use the width property. 2. prototype of the area method in the inherited classes doesn't match the prototype of the area class in the abstract class. 3. you try to call abstract method from the overriden area method with super.area() I tried to fix it: https://code.sololearn.com/cREbI3SfkAhY/?ref=app
3rd Feb 2021, 8:06 AM
John Robotane
John Robotane - avatar