Passing in parameters for super and subclasses?

ICEFIRE_16 :

The values I am supposed to pass in:

  • Name and family should be saved for all instruments
  • We need to specify whether a strings instrument uses a bow

When I run my code it gives me the error: "constructor Strings in class Strings cannot be applied to given types;"

public class InstrumentTester
{
    public static void main(String[] args)
    {
        /**
         * Don't Change This Tester Class!
         * 
         * When you are finished, this should run without error.
         */ 
        Wind tuba = new Wind("Tuba", "Brass", false);
        Wind clarinet = new Wind("Clarinet", "Woodwind", true);

        Strings violin = new Strings("Violin", true);
        Strings harp = new Strings("Harp", false);

        System.out.println(tuba);
        System.out.println(clarinet);

        System.out.println(violin);
        System.out.println(harp);
    }
}

public class Instrument
{
    private String name;
    private String family;

    public Instrument(String name, String family)
    {
        this.name = name;
        this.family = family;
    }

    public String getName()
    {
        return name;
    }

    public String getFamily()
    {
        return family;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setFamily(String family)
    {
        this.family = family;
    }
}

public class Strings extends Instrument
{
    private boolean useBow;

    public Strings(String name, String family, boolean useBow)
    {
        super(name, family);
        this.useBow = useBow;
    }


    public boolean getUseBow()
    {
        return useBow;
    }

    public void setUseBow(boolean useBow)
    {
        this.useBow = useBow;
    }
}

How do I pass in the parameter family if it doesn't take it?

John Kugelman :
Strings violin = new Strings("Violin", true);
Strings harp = new Strings("Harp", false);

The violin and harp don't get passed a family name when they're created, so the Strings constructor mustn't expect one as an argument.

public Strings(String name, boolean useBow)

What do you pass to super(), then? If all strings belong to the same family then you can hard code the value. Perhaps just "String":

public Strings(String name, boolean useBow)
{
    super(name, "String");
    this.useBow = useBow;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=335417&siteId=1