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 trial

Java Java Annotations Using Java's Built-In Annotations Using @Override to Fix an Error

Awais Jamil
Awais Jamil
7,539 Points

Cannot Solve this Excersice

Please Help i am not Sure how to you Override this Method.

HelloWorld.java
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);  
  }
}
HolaWorld.java
public class HolaWorld extends HelloWorld {
  @Override
  public void sayHelloTo(String name) {
    String name = "";
    return System.out.printf("Hola, %s%n!", name);
  }
}

2 Answers

Hi 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!


sig

Using 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.