2

Although the class B implements the interface A, it does not provide the implementation of the method show(). Again, D extends C but in class D the implementation of displayNothing() is the same as the implementation of displayNothing() method in its superclass C.

But in both the cases @Override annotation work properly, why?

//code snippet 1
interface A
{
    void show();
}
abstract class B implements A
{
    @Override        
    abstract public void show(); 
}

//code snippet 2
class C
{
    void displayNothing()
    {
    }
}
class D extends C
{
    @Override
    void displayNothing()
    {
    }
}
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142

4 Answers4

0

B is abstract. Abstract classes don’t need to provide implementations. If an abstract class doesn’t implement a required method it doesn’t matter. If a class isn’t abstract, then implementations of all abstract methods need to be there.

D provides an override of the displayNothing method in C. The compiler doesn’t check whether the overridden version is the same as the overriding version, and it doesn't care if the overriding implementation is empty. All the Override annotation does is confirm that there is a method with the same signature above the annotated one in the class hierarchy.

The purpose of the annotation is to confirm that you’re actually overriding an existing method and haven’t introduced a bug by misspelling the method name or otherwise not matching the signature of the method you mean to override. See the Java Language Specification, 9.6.4.4, where it says:

Programmers occasionally overload a method declaration when they mean to override it, leading to subtle problems. The annotation type Override supports early detection of such problems.

The classic example concerns the equals method. Programmers write the following in class Foo:

public boolean equals(Foo that) { ... }

when they mean to write:

public boolean equals(Object that) { ... }

This is perfectly legal, but class Foo inherits the equals implementation from Object, which can cause some very subtle bugs.

(Be aware when I describe what @Override does I'm skipping over override-equivalence because you didn't ask about it and there are already perfectly good answers that cover that.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Then what does the '@Override' annotation check at the compile time? – Pratip Chakraborty Jul 08 '18 at 05:18
  • You meant that the annotation just checks that if the class that implements an interface or extends another class is implementing or overriding a method respectively with correct method header or method signature. It has nothing to do with the versions (be it overridden or overriding). Is it so? @Nathan Hughes – Pratip Chakraborty Jul 08 '18 at 06:02
  • @Pratip: it only matches method signatures to make sure you're actually overriding something. – Nathan Hughes Jul 08 '18 at 06:07
0

@Override is only bothered with whether your function name along with the data type is just the same as the method in your super class (with no Alphabetical error). In your case it is the same.Try changing void as int in your class D. It shows a compilation error. As for your abstract class method, it needn't define anything coz the name says it all, it is abstract.

Nithya
  • 51
  • 10
  • I know that abstract classes need not provide the implementations of the abstract methods so I intentionally kept it abstract in the class B. But still '@Override' work properly although I've not implemented the show() method of the interface A that it implements. – Pratip Chakraborty Jul 08 '18 at 05:47
0

The purpose of the @Override annotation is to prevent bugs. It's presence informs the compiler that there must be a inherited method with that same exact signature that this class is overriding. It does matter if there is an implementation of a method or not.

It is perfectly legal to not include an @Override annotation. The potential problem you might encounter, though, is if you accidentally had a typo or inadvertently changed the method signature, you would be overloading the method, not overriding it. The compiler would not be able to catch this bug. The program might crash or it might even run, but run incorrectly.

Because this might be a difficult bug to spot, especially with methods containing several parameters, the @Override annotation was created. The @Override annotation informs the compiler that this method is an override, not an overload and if the compiler fails to find a method to override, return a compilation error.

All these methods would compile and could be run but none would override the displayNothing() method.

void displayNothng();
void displaynothing();
void displayNothing(String value);
LiveNLearn
  • 321
  • 1
  • 5
0

The @Override annotation "works properly" in both presented cases, because java reuses this annotation for interfaces and classes, even if you feel that the first and the second snippet of code do something different it's just not as important to introduce separated keywords/annotations to differentiate between overriding from an interface and from a class. Even an abstract class doesn't change things here.

The @Override indicates that a given method has a compatible representation in the super class/interface, it's used to ensure we don't have any syntax issue in the definition.