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

Axel Rearte
Axel Rearte
2,125 Points

I dont understand the task!

can someone explain me ?

HelloWorld.java
public class HelloWorld {
  private String programmingLanguage;

  public HelloWorld() {
    programmingLanguage = "Java";
  }

   @Override
  public void sayHelloTo() {
    String name = "";
    System.out.printf("Hola, %s%n!", name);
  }
}
HolaWorld.java
public class HolaWorld extends HelloWorld {
  @Override
  public void sayHelloTo() {
    String name = "";
    System.out.printf("Hola, %s%n!", name);
  }
}

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

The 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);
  }
}