简体   繁体   中英

calling a constructor from a derived class

Hi I have been trying to use my zeroparameter constructors to call my SunkenObject constructor with initial weight values but for some reason i keep getting this error

constructor SunkenObject in class SunkenObject cannot be applied to given types ;`

required: float
  found: no arguments
  reason: actual and formal argument lists differ in length

This is my SunkenObject constructor:

public abstract class SunkenObject extends CatchableThing
{
   protected float weight;

   public SunkenObject(float w)
   {
     weight = w;
   }

  public float getWeight() { return weight; }

    public String toString () 
    {
        return (getClass().getSimpleName());
    }
}

and this is one of the objects (a rusty chain) that is extended by SunkenObject

public class RustyChain extends SunkenObject 
{

    public RustyChain (float w)
  {
    super(w);  
  }
   public RustyChain()
   {
     weight = 8.0f;
   }


}

can anyone tell me what I am doing wrong because from my perspective nothing is wrong with the code. Thank You!

You mean like this:

public RustyChain() {
    super(8.0f);
}

weight = 8.0f doesn't call the super class constructor.

The first default statement of a concrete class constructor is super(); calling statement.

So, to make it work, you either remove

public RustyChain()
{
 weight = 8.0f;
}

or add a no-arg constructor in the super class like this

public SunkenObject()
{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM