@Overrride annotation introduced in Java1.5

Whenever we have a subclass that implements parent method it is marked with annotation as @Override. This is to make compiler know that the child overrides the method defined or declared in parent.

If @Overrride annotation is commented, the subclass will work as it was.
But if in future, the signature of parent class changes, the compiler wont ask to implement the method instead it will think subclass has overloaded the method present in base class.

If you uncomment @Override annotation, the compiler will throw compile time error as method signature in parent class has changed.

Annotation should be used as standard as-

  • It ensures that for any change in method signature of parent class, compiler will ask to change it for the subclasses.   
  • Also it is better to resolve issue in compile time rather than runtime

Comments

Popular posts from this blog

Liskov Substitution Principle (LSP)

Interface Segregation Principle