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 trialAxel Rearte
2,125 PointsI dont understand the task!
can someone explain me ?
public class HelloWorld {
private String programmingLanguage;
public HelloWorld() {
programmingLanguage = "Java";
}
@Override
public void sayHelloTo() {
String name = "";
System.out.printf("Hola, %s%n!", name);
}
}
public class HolaWorld extends HelloWorld {
@Override
public void sayHelloTo() {
String name = "";
System.out.printf("Hola, %s%n!", name);
}
}
1 Answer
Kourosh Raeen
23,733 PointsThe HolaWorld class extends the HelloWorld class and it has a method called sayHelloTo() that is supposed to override a method of the same name in the parent class. By using the @Override annotation the compiler will check to see if we are doing the overriding correctly. The method names and their signatures have to match but as it is the signatures do not, since the method in the parent class has a String parameter and the one in the child class has none. So to fix the problem we need to add that parameter to sayHelloTo() in HolaWord. We also need to make sure that we are not redefining the variable name inside the method:
public class HolaWorld extends HelloWorld {
@Override
public void sayHelloTo(String name) {
name = "";
System.out.printf("Hola, %s%n!", name);
}
}