I am a beginner in Java.There is a problem with class inheritance.Please let me know
Problem statement:
Create a ShortComic class that inherits the Comic class.ShortComic represents a comic book with only one volume.The ShortComic class implements:
The ShortComic class must be defined in the book package (ShortComic.java is in the appropriate directory) and must be accessible from outside the package
Written by:
importbook.Comic;
public class ShortComic extensions Comic {
public void Comic (String series) {
super();
This.volume=1;
}
}
Error Description:
error: constructor Comic in class Comic cannot be applied to give types;
public class ShortComic extensions Comic {
^
required —String, int
found —No arguments
reason —actual and formal argument lists different in length
book/ShortComic.java:24:error:call to super must be first statement constructor
super();
^
book/ShortComic.java:25:error:volume has private access in Comic
This.volume=1;
^
3 errors
I have this kind of error, but I don't understand it at all.Please let me know if there is anything wrong.
java
error: constructor Comic in class Comic cannot be applied to give types;
error:call to super must be first statement constructor
This is because the ShortComic
class does not have the right constructor.
public void Comic (String series) {
publicShortComic(String series){
.
error:volume has private access in Comic
The question statement does not tell you to substitute the volume
field.
Specify 1 for the number of volumes of the constructor being inherited
You will pass 1
to the constructor.
Comic
class may look like this:
package book;
public class Comic {
private final String series;
private final int volume;
public Comic (String series, int volume) {
this.series = series;
this.volume=volume;
}
// ...
}
In this case, the ShortComic
implementation that meets the challenge would be:
package book;
public class ShortComic extensions Comic {
public ShortComic (String series) {
super(series,1);
}
}
ShortComic
in this case).super(...);
is a parent class constructor call (here publicComic(String series, int volume){...}
).Therefore, you must match what you want to pass to the argument to the parent constructor.
© 2024 OneMinuteCode. All rights reserved.