Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFernando Boza
25,384 Pointstrying to add a parameter to a constructor but getting a message
trying to add a String parameter but keep getting an err message when instantiating the object
I keep getting this err msg
Main.java:14: error: constructor Animal in class Animal cannot be applied to given types;
super(sound);
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
1 error
compiler exit status 1
CODE
class Main {
public static void main(String[] args) {
Dog Loki = new Dog("Woof");
Dog Thor = new Dog("Bark");
}
}
abstract class Animal {
String sound = "";
}
class Dog extends Animal {
Dog(String sound) {
super(sound);
}
}
1 Answer
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsThe Animal abstract class has a single field sound but doesn't have any methods or constructors, only the default Animal() constructor. So when you call super(sound) in the Dog class it won't work as you trying to call a constructor that doesn't exist. So you can do something like this:
Dog(String sound) {
this.sound = sound
}