Overriding in Interface Methods

2 0 0
                                    

An interface has an abstract, default and static method. The difference of an interface to a class is that an interface don't have fields and you cannot construct an object via an interface unlike a class that has field members and can create an object. Why I mentioned this? It is because a class can only inherit from one super class. Multiple inheritance in a class is not allowed due to name conflicts in the future where the compiler cannot inferred which one to use. But in interface, multiple inheritance or in layman's terms "you can extends multiple interfaces" is allowed due to the reason that interfaces don't have fields and cannot create an object. But there are instances where the superclass can have the same declaration with the interfaces default or abstract methods. The following rules below are applied.

Rules:

1. Abstract and default methods of an interface are inherited like an instance method from a class.

2. Static methods from an interface cannot be inherited.

3. Instance methods of a super class takes priority in a compiler rather than default methods of an interface.

4. If in case an interface default methods is overridden by a super class method, then that default methods will be ignored.

5. If in case there were two or more interfaces with the same signature in their default methods, you need to be specific by using the super keyword to declare which one you intend to use. For example: InterfaceName.super.defaultMethodName(); This was the same rule before in using the this keyword in the shadowing of variables in the local class.

6. If in case that your super class has the same signature with the abstract method of an interface that you implemented, then, the compiler will implicitly overrides that abstract method of an interface. Like for example: Class A extends Class B implements Interface C. And then in the scenario above that Class B has the same signature with your Interface C, then as you invoke that instance method of class B in Class A, the abstract method of interface C will be overridden implicitly.

7. The modifiers like public and private can be extended more but not less. Meaning if the overridden method is protected, the overriding method is not allowed to declare it less like private but you can extend the access more like public.

====================

class Class1 extends MotherClass implements Coffee, EnergyDrink{

public static void main(String[] args) {
System.out.println("====Implementing Coffee=====");
Class1 one = new Class1();
one.typeOfDrink("Black Coffee");
one.inviteCompanion("Linda L. Taylor");
Coffee.whyCoffee("L: It has an option of no sugar?..");
EnergyDrink.whyEnergyDrink("M: You are young and energetic!");
}

@Override
public void typeOfDrink(String name) {
System.out.println("I'll take that " + name + " !Please!");
super.typeOfDrink(name);
}
/*
Bad type qualifier error occurred in coffee and energy drink
@Override
public void inviteCompanion(String name) {
Coffee.super.inviteCompanion(name);
EnergyDrink.super.inviteCompanion(name);
}
*/


@Override
public void inviteCompanion(String name) {
super.inviteCompanion(name);
}
}
class MotherClass implements Coffee, EnergyDrink{
final String mother = "Mother: ";
@Override
public void typeOfDrink(String name) {
System.out.print(mother);
System.out.println("Don't drink that " + name + " !");
}

@Override
public void inviteCompanion(String name) {
Coffee.super.inviteCompanion(name);
EnergyDrink.super.inviteCompanion(name);
}
}
interface Coffee{
void typeOfDrink(String name);
default void inviteCompanion(String name){
System.out.println("gf: " + name + "! Care to join for a coffee..");
}
static void whyCoffee(String reason){
System.out.println(reason);
}
}
interface EnergyDrink{
void typeOfDrink(String name);
default void inviteCompanion(String name){
System.out.println("angry face: " + name + "! Care to join for an energy drink..");
}
static void whyEnergyDrink(String reason){
System.out.println(reason);
}
}

===================

Results:

====Implementing Coffee=====I'll take that Black Coffee !Please!Mother: Don't drink that Black Coffee !gf: Linda L. Taylor! Care to join for a coffee..angry face: Linda L. Taylor! Care to join for an energy drink..L: It has an option of no sugar?..M: You are young and energetic!

Java ProgrammingWhere stories live. Discover now