7

This is a very naiveish question, but here goes:

An overriden method from a base class will mean that calls to the sub class will call the derived, overriden method, correct?

Thus, if there is no override annotation, the method in the base class will be called. So the override method will serve purely to document the intent - call one version of a method over the other.

Is this the case?

This leads me to the following question:

What is the difference between an abstract class which 5-6 classes may derive from but the methods inherited in the derived classes are not overriden, and one class (Static or not being irrelevant), used by those 5-6 classes?

GurdeepS
  • 65,107
  • 109
  • 251
  • 387

4 Answers4

10

The @Override annotation is intended ONLY to catch errors at compilation time. It does not affect override behavior at runtime. The idea is that you give the compiler the chance to inform you that your method name or signature is wrong.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 6
    It is particularly useful when you consider code changes over time. If your subclass overrides a method of the base class, but later someone changes that method signature in the base class, your subclass is no longer overriding anything, which is probably incorrect. The @Override tells the compiler that the method is expected to override something, so it will trip at build time, letting you investigate and correct the situation. Think of it as a build-time assertion. – Konstantin Komissarchik Jan 10 '11 at 21:50
1

The override annotation is simply a marker to show the the method overrides a superclass method.

It being there has no effect at runtime. See here:

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Override.html

Pablojim
  • 8,542
  • 8
  • 45
  • 69
0

Using abstract is an architectural design to ensure that users either must implement an abstract method (declared but not implemented in the base class) or that they all have access to the same base methods and data (this is the same as the latter example you gave).

Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
0

Thus, if there is no override annotation, the method in the base class will be called.

No. The overriding method in the derived class will always be called.

So the override method will serve purely to document the intent - call one version of a method over the other.

Is this the case?

No. As per the other answers, it tells the compiler to insist that there was something to override.

user207421
  • 305,947
  • 44
  • 307
  • 483