1

I have an abstract class like so:

abstract class Player {

        //some stuff

        public abstract String getName();

    }

and in one of the subclasses I am implementing (and overloading / overriding as well? Here is where I am not sure) the function getName() like so by typing out the whole function again:

public String getName(){
    return "John";
}

but I can also in my subclass simply write "getName()", press enter and my IDE automatically fills out the following code for me, which also works (if I change null to "John" in this case):

@Override
public String getName(){
    return null;
}

Obviously since it states "Override" I am here overriding the function in the abstract superclass. But am I not already overriding the function even without putting "@Override" above the function?

What I am saying is: I have an abstract function, so it is only implemented in a subclass and not in the superclass itself. Doesn't this mean that by implementing it I am automatically overriding it as well, since it has no implementation in the superclass to begin with? Why should I write @Override then? Or am I just "overloading" if I don't put "@Override" in there?

user5846939
  • 415
  • 4
  • 11

2 Answers2

2

There is no method overloading in your code snippet, only overriding. Method overloading occurs when multiple methods with different signatures (i.e. different arguments) have the same name.

The @Override annotation is optional. If you remove it, your getName() would still override the abstract method of the super-class. The advantage of using this annotation is that it helps to catch bugs - if you try to override a method that does not appear (with the same name and signature) in any of the super-classes or super-interfaces of your class, if you use the @Override annotation, the compiler will give you an error.

Therefore, if by mistake you write :

@Override
public String getname(){
    return null;
}

or

@Override
public String getName(int something){
    return null;
}

The compiler will give you an error. Without the @Override annotation, you would get no error (well, you would get an error for not implementing an abstract method, but if getName() of the super-class wasn't abstract, you would get no error, and your method in the sub-class would simply not override the getName method of the super-class).

Eran
  • 387,369
  • 54
  • 702
  • 768
0

You are not required to put @Override annotation when overriding, but it may help you. If you write

abstract class Player {
    public abstract String getName();
}

abstract class ConcretePlayer extends Player{
    public String getMame() {    //typo here
        /* some code */
    }
}

this will compile and you'll probably introduce a bug in your code, while this

abstract class Player {
    public abstract String getName();
}

abstract class ConcretePlayer extends Player{
    @Override               
    public String getMame() { //exception here
        /* some code */
    }
}

will result in a compilation error.

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48