Java Class Succession Does Not Make You Knows

Asked 1 years ago, Updated 1 years ago, 349 views

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:

  • A constructor that accepts only stringed series names
  • Specify 1 for the number of volumes of the constructor being inherited

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

2022-09-30 22:03

2 Answers

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.


2022-09-30 22:03

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);
    }
}
  • The constructor has the same name as the class name (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.
    • In other words, in this case, the first argument is the series name and the second argument is the number of volumes.
  • In other words, in this case, the first argument is the series name and the second argument is the number of volumes.


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.