Interface Method- Default Ignore/Fail

From design perspective , it is good to create interface with different methods and implement the interface in different classes and adopt the method implementation for that specific requirement. The class that implements the interface ideally implements all the interface methods.

Considering the case that, sometimes the interface contains huge number of methods and the implementing different classes may not need to implement all the methods of the interface. To cater this now in the interface we can mark some method as default ignore /fail.

If any method is marked as default ignore/fail, then the implementation of the method in the subsequent classes are not mandatory. But if these methods are called from somewhere then if it is marked as IGNORE then it works like a method which is implemented but with an empty body else if it is marked as FAIL then run time exception occurs. So depending on the requirement these additions can be used in interface methods.

Below we have an interface-

The method IS_PRICE_REQUIRED is like usual method that we created without any addition.

The method CALCULATE_PRICE is marked as DEFAULT IGNORE .

The method GET_BASE_PRICE is marked as DEFAULT FAIL.

Here are the details-

Now implement the interface in a class and as usual the warning message appears for the interface method without any IGNORE/FAIL addition.

So implement the IS_PRICE_REQUIRED method.

Here are the details-

Let’s just add a new method to the class” CALCULATE’ and let’s call the interface method IS_PRICE_REQUIRED inside it.

Here are the details-

Here are the details-

Here are the details-

Now if we test the CALCULATE method of the class- things are fine. No output as there are empty methods.

Now let’s call the interface method CALCULATE_PRICE which is marked as DEFAULT IGNORE. Remember that there is no implementation for this method.

There is no implementation for this method – CALCULATE_PRICE

Test the method and there is no exception. So the method CALCULATE_PRICE works like an empty body method.

Now let’s call the method GET_BASE_PRICE and the addition is DEFAULT FAIL.

Here are the details. No implementation for method- GET_BASE_PRICE.

Now test the method call-

Here we get the exception for the call of non implemented method GET_BASE_PRICE with addition DEFAULT FAIL.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s