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

Kulvir Bajwa
Kulvir Bajwa
1,232 Points

When using the @Override annotation, is it only the method signatures, return type and parameters that need to match?

Can't seem to get passed this @Override annotation exercise. I've checked that the method signature, return type and parameters match but it still won't accept it. Any ideas?

HelloWorld.java
public class HelloWorld {
  private String programmingLanguage;

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

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

2 Answers

andren
andren
28,558 Points

You are overriding the method correctly, that's not what is producing the compiler error.

Whenever you get a compiler error your first response should always be to look at the actual error message the compiler has produced. Compiler errors are often quite explicit about what is wrong in your code. The error message your code generates is this:

./HolaWorld.java:4: error: variable name is already defined in method sayHelloTo(String)
    String name = "";
           ^
1 error

You are getting a compiler error because the first thing the overridden sayHelloTo method does is declare a "name" variable even though it already has a "name" variable declared in its parameter list.

If you remove the line the compiler is pointing out, you'll be able to pass the challenge.

Sagar Thakkar
Sagar Thakkar
8,814 Points

just remove "String name = """ from HolaWorld class it will work.....