开发者

Compiler won't recognise constructor override properly

I'll let the code and errors do the talk, because I really think they say everything except THIS SHOULDN'T BE HAPPENING! Does anyone know how to make this compile?

Code


  class CountDownTimerGUI extends BHTimerGUI
  {
    private TimerJPanel control;
    private TimerDisplayJP开发者_运维百科anel disp;

    public CountDownTimerGUI(TimerJPanel control, TimerDisplayJPanel disp)
>>  {
      this.control = control;
      this.disp = disp;
    }
  }

(>>signifies the line of the error)

this overrides the constructor for BHTimerGUI, who's constructor is as follows:

  public BHTimerGUI(TimerJPanel control, TimerDisplayJPanel disp)
  {
    this.control = control;
    this.disp = disp;
  }

Compiler Error


I:\Java\NetBeansProjects\Blue Husky's Timer 2.0.0\src\bhtimer\GUI.java:145: cannot find symbol
symbol  : constructor BHTimerGUI()
location: class bhtimer.BHTimerGUI
    {

NetBeans shows a popup with the following text:

constructor BHTimerGUI in class bhtimer.BHTimerGUI cannot be applied to given types;
  required: bhtimer.TimerJPanel,bhtimer.TimerDisplayJPanel
  found: no arguments
  reason: actual and formal argument lists differ in length


Yes, this SHOULD be happening! you are not initializing the superclass constructor. Try with this constructor:

   public CountDownTimerGUI(TimerJPanel control, TimerDisplayJPanel disp){
      super(control, disp);
      this.control = control;
      this.disp = disp;
   }


Constructors aren't overridable in Java.

We'd need to see the constructor for BHTimerGUI, but sounds like it is not a no-args constructor, in which case you will need to explicitly code a super call to it, with the correct params e.g. add this as the first line of your constructor in CountDownTimerGUI:

 super(control, disp);

The compiler will at the moment be inserting:

super();

But that won't be finding a constructor in the superclass.


What arguments does BHTimerGUI's constructor take? You might need to add this line to the top of CountDownTimerGUI's constructor:

super(control, disp);


The message tells you that your code, as written, requires a call to the no-arg, default constructor for the super class. Unfortunately for you, one hasn't been written.

The solution, as explained by others, is to explicitly call one of the superclass constructors that has been defined.

When you see this message in the future, be sure to open the Java docs of the super class and check the arguments for the constructors that are implemented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜