The other questions I have seen are about intentionally referencing a superclass constructor method and failing. I am not referencing a constructor yet; just trying to extend a class.
I am creating the class Square.java, which extends Rectangle.java.
Square.java is the subclass of Rectangle.java.
Interface.java
-Abstract.java - abstract class Abstract implements Interface.java
--Rectangle.java - public class Rectangle extends Abstract
---Square.java - public class Square extends Rectangle.
When compiling Square.java, I received the error message.
Square.java:3: error: constructor Rectangle in class Rectangle cannot be applied to given types;
public class Square extends Rectangle {
^
required: String,String,String,double,double
found: no arguments
reason: actual and formal argument lists differ in length
1 error
The content of Square.java is
public class Square extends Rectangle {
//no code is in the body
}
The constructor method of rectangle is:
public Rectangle (String t,String n, String c, double w, double h ){
width = w;
height = h;
color = c;
name = n;
type = t;
};
I want to create Square.java. Within this file, I would like to modify the Rectangle constructor method. But for right now, I just want the file to compile.
I have tried
- Copying the Rectangle constructor method into Square.java. That resulted in the same error message.
- Copying in the Rectangle constructor method and changing the name of the constructor method to Square. This resulted in the same error message.
- Commenting out the Rectangle constructor method within Rectangle.java. Square.java compiled without a problem. But... I need the Rectangle constructor in Rectangle.java.
- Creating a new Square constructor method and ran into more errors. Eventually, I want the Square constructor method to be a modified Rectangle constructor method.
From my understanding, constructor methods are not passed on to subclasses. I have not written code to intentionally reference the Rectangle constructor.
I am getting the feeling that the compiler is interpreting "... extends Rectangle { " as an attempt to reference the constructor instead of the class. I have run out of ideas on how to resolve this.
Thank you for any help.
CodePudding user response:
- All classes must contain at least one constructor. They cannot be compiled without having one.
- All constructors must begin with a
super()
call of some sort (invoking a constructor in the parent class), unless they start with athis()
call (redirect to another constructor). - ... However, if you fail to add a
super()
call, the compiler will simply assume you meant to and acts as if you wrotesuper();
at the top of it. - ... However, if you fail to add a constructor, the compiler will simply assume you meant to write one, and acts as if you wrote:
public MyClassName() { super(); }
.
With these facts in mind, super()
is not going to work in your case, because Rectangle
does not have a no-args constructor.
All you need:
public class Square extends Rectangle {
public Square(String t, String n, String c, double w) {
super(t, n, c, w, w);
}
}
This means Square has 1 constructor, taking 't', 'n', and 'c', whatever those are, as well as a single double indicating the length of any edge. (NB: This is bad code style, parameters should have clear names, especially if the type of the parameter isn't particularly enlightening. 'String c' means nothing. Color c
- you can get away with that)
It then calls the Rectangle constructor, passing that 'edge length' value for both the width and height dimensions.
CodePudding user response:
The problem was solved with the help of this question. Similar content
I create another constructor with super(), this resolved the error.
public Square (String t,String n, String c, double w, double h ){
super(t,n,c,w,h);
};