1

While we use any method from the interface it asks us to override all unimplemented method. also we are using '@Override' annotation while implementing the method. Does it really called overriding ? because interface contains only method definition(no executable code). The interface is say,

public interface ITestListener extends ITestNGListener {
  void onTestStart(ITestResult result);
  public void onTestSuccess(ITestResult result);
  public void onTestFailure(ITestResult result);
  public void onTestSkipped(ITestResult result);
  public void onTestFailedButWithinSuccessPercentage(ITestResult result);
}

and the implementing class is

public class TestNGTestBase implements ITestListener{  @Override
    public void onTestStart(ITestResult result) {
//xyz

    }                                                                                              @Override
    public void onTestSuccess(ITestResult result) { /// xyz} @Override
    public void onTestSkipped(ITestResult result) {


    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        // TODO Auto-generated method stub

    }}    

also why is this mandatory to override all the methods in the interface ?

Raviteja
  • 3,399
  • 23
  • 42
  • 69
Ram Patro
  • 121
  • 4
  • 10
  • 3
    yes it is overriding. And the `@Override` notation makes sure that the method is really an overriden one and would create a compile time error if the method signature of any of these methods would change and there wouldn´t be a method fitting that signature anymore. – SomeJavaGuy Mar 03 '16 at 07:27

2 Answers2

1

Yes the method is overriding from the superclass. This notation would create a compile time error if the method signature of any of these methods would change.

Overriding is a feature that is available while using Inheritance.

It is used when a class that extends from another class wants to use most of the feature of the parent class and wants to implement specific functionality in certain cases.

Luc Geven
  • 164
  • 12
0

@Overriding ensures the method is a correct override and gives compile time error if its not a valid override. It is not mandatory to override all methods of an interface in a class,you need to declare the class "abstract" in this scenario.

But if you want a concrete class(which can be instantiated) to implement an interface,you will need to implement all methods of your interface in this concrete class

S.B
  • 678
  • 1
  • 5
  • 11