0

this is my inteface.

public interface ConnectionListener{
    public void onConnectionReady();
    public void onConnectionDown();
}

I implement this interfaces in HomeActivity class.I would like to learn what is the differences between using @Override in implemented methods and not using @Override annotation...

public class HomeActivity implements  ConnectionListener
 {

     @Override
      public void onConnectionReady() {

    }


     @Override
    public void onConnectionDown() {

    }
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
seyid yagmur
  • 1,572
  • 17
  • 26
  • 4
    According to the docs `While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.` – Knossos Jan 19 '16 at 14:52
  • If you don't use `@Override` there the two methods will be treated as local methods, and you will get an error for not implementing all inherited methods aka methods listed in the `ConnectionListener` interface. – Shark Jan 19 '16 at 14:55
  • 3
    @Shark somehow doubt that. They just need to provide an implementation, Override annotation is never mandatory. – Stultuske Jan 19 '16 at 14:57
  • Yes, my wording is bad, it never mentions that it's mandatory however. – Shark Jan 19 '16 at 15:00
  • 1
    @Shark: "If you don't use ... you will get an error". it kind of does. – Stultuske Jan 19 '16 at 15:01
  • @Stultuske "you will get an error for not implementing all inherited methods aka methods listed in the ConnectionListener" please, don't take my words out of context. But as far as the annotation itself goes - it's not needed. What I meant to say was "you need to provide an implementation, the annotation itself is irrelevant. it's eye-candy" – Shark Jan 19 '16 at 15:02
  • 2
    thank you @Knossos . I got it now. I am not getting any error coz of not using @ Override as you said. – seyid yagmur Jan 19 '16 at 15:04
  • 1
    But you would get an error if you have the annotation, but for example a typo in your method name like `@Override public void onConnectionready() {}` (note the lowercase `r` in "ready"). That's why it is _always_ a good idea to use that annotation if you override a method. – Tom Jan 19 '16 at 15:10
  • thank you @Tom .it is an understandable answer.it helped me enough. – seyid yagmur Jan 19 '16 at 15:17
  • Also , some IDE like Eclipse, will display the javadoc of the interface method, when you hover over the implementation method signature. – Arnaud Jan 19 '16 at 15:20
  • @Shark I didn't take it out of context, you either used the 'and' word incorrectly, or you didn't complete your sentence. – Stultuske Jan 20 '16 at 07:21

3 Answers3

6

@Override only shows the compiler that you would like to override a method. If the method signature is not known in a super class or an implemented interface you get a compile time error.

At runtime there is no difference.

For more Information see the javadoc.

2Dee
  • 8,609
  • 7
  • 42
  • 53
Jens
  • 67,715
  • 15
  • 98
  • 113
0

If you have class B which extends class A (B extends A) can you use @Override to "replace" some method. You can modify how methods work in class B without replace it in class A. In overriden method you can invoked method from class A too by using "super" method and modify how it will work after super usage. In life you can use class Animal to extend class Dog and Cat. If Animal class had method getSound() you can for Dog override it for dog sound and for Cat for a cat sound. But Dog and Cat will have all artifacts which have animal like legs or other parts of body.

Interface you can use when you try to standardize some group class there will be same methods name but in interface you didn't implement a code of a method.

You can read more about it here: Implements vs extends: When to use? What's the difference?

Community
  • 1
  • 1
rSir
  • 1
  • wouldn't `Dog` and `Cat` extend `Animal` ? :) there's no multiple inheritance in java, so there's no way `Animal extends Dog, Cat` unlike C++. – Shark Jan 19 '16 at 15:17
  • I mean you can use Class Animal to extend Class Dog and Cat. In code like "Dog extends Animal" and "Cat extends Animal". Sorry for my english I know it isn't perfect :) – rSir Jan 19 '16 at 23:22
0

You will find the best usage of @Override tag when using it with static methods during inheritance. Suppose class A has a static method EAT, class B extends class A and creates a static method EAT in class B. Now class A and B both have static method EAT with different implementation but it is not overridden, the moment you try to tag it with @Override, it shows you an error which means the sub-class has the same signature and different implementations but not overridden.

  • The first sentence of your answer seems basically incorrect: you can't use `@Override` with static methods. Could you clarify what you mean? – jirassimok Sep 14 '19 at 20:33