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 trialAwais Jamil
7,539 PointsCannot Solve this Excersice
Please Help i am not Sure how to you Override this Method.
public class HelloWorld {
private String programmingLanguage;
public HelloWorld() {
programmingLanguage = "Java";
}
@Override
public void sayHelloTo(String name) {
HolaWorld obj = new HolaWorld();
obj.sayHelloTO();
System.out.printf("Hello, %s%n!", name);
}
}
public class HolaWorld extends HelloWorld {
@Override
public void sayHelloTo(String name) {
String name = "";
return System.out.printf("Hola, %s%n!", name);
}
}
2 Answers
doesitmatter
12,885 PointsHi Awais Jamil,
@Override
public void sayHelloTo(String name) {
//sets name to "" meaning we remove the value we are supposed to print... not good
String name = "";
//This gives an error, because System.out.printf() is a VOID method, it doesnt return anything.. This method itself is also void, it isn't supposed to return anything
return System.out.printf("Hola, %s%n!", name);
}
Notice that even if you don't return System.out.printf but just run it, your method now always returns Hola without a name. Is String name = "";
really necessary?
Good luck!
Cameron Jones
10,049 PointsUsing the @Override method the compiler will check if it's correct with the parent class. However, the compiler throws an error, because the new method trying to Override the parent is missing its parameters, and it attempts to create an unnecessary name.