Home > Back-end >  How to rewrite the abstract method in the parent class
How to rewrite the abstract method in the parent class

Time:09-28

import java.util.Scanner;

Public class ShapeTrouble {
Public static void main (String [] args) {
Scanner Scanner=new Scanner (System. In);
Double radius=scanner. NextDouble ();
Double length=scanner. NextDouble ();
Double width=scanner. NextDouble ();

Circle Circle=new Circle (radius);
A Rectangle Rectangle=new Rectangle (length, width);

System. The out. Println (circle. GetAcreage ());
System. The out. Print (rectangle. GetAcreage ());
}
}

/* * * * * * * * * * the Begin * * * * * * * * */
The abstract class Shape {
The abstract double getAcreage ();//define abstract methods
}

The class Circle extends Shape {
Private final double PI=3.1415926;
Private double radius;
Public Circle radius, (double) {
Enclosing the radius=radius;
}
Public double getAcreage () {
Return PI * this. Radius * this. The radius;
}
}

Class a Rectangle extends Shape {
Private double length;
Private double width;
Private Rectangle (double length, double width) {
This. Length=length;
This. Width=width;
}
Private double getAcreage () {
Return this. Length * this. Width;
}
}

Error:
0/3 SRC/step1 ShapeTrouble. Java: 13: error: a Rectangle (double, double) has private access in a Rectangle
A Rectangle Rectangle=new Rectangle (length, width);
^
SRC/step1 ShapeTrouble. Java: 16: error: getAcreage () from the private access in a Rectangle
System. The out. Print (rectangle. GetAcreage ());
^
SRC/step1 ShapeTrouble. Java: 43: error: getAcreage () in a Rectangle always override getAcreage () in Shape
Private double getAcreage () {
^
Attempting to assign weaker access privileges; The was
3 errors

CodePudding user response:

Where your mistakes are not rewriting abstract methods, the scope of access modifiers, private private modifiers, can only be accessed to use in your own class, so you need to a Rectangle in the class constructor and rewrite the abstract methods of the parent's access modifiers to the public or the default (write) nothing
  • Related