Java – What are Default Methods II

Prior to Java 8, Java interfaces cannot be implemented. Because of a class can implement more than 1 interface, and imagine in the event of there is same signature in different interfaces which the class is implementing, then in this case, the class might be confused to picking which one, cause in the past prior to Java 8, there was no rule in compiler to determine, which interface’s method should be picked.

Multiple Interface Inheritance Rules

Java compiler provides some rules to determine which default method a particular class uses.

1. Instance methods are preferred over interface default methods.

If a class extends another class and other interfaces, where the parent class and the interfaces co-incidentally have the same signature. Then the parent class’s signature will be chosen rather than the interfaces’ one.

Result: Process finished with exit code 0

2. Methods that are already overridden by other candidates are ignored

The move() of Combustion is overridden by Turbo‘s one, therefore the Turbo‘s move() is chosen.

Source Codes

GitHub

Author: Wu Chia Chong

My main areas of interest are software architectures and software design methods, patterns, and new trends in software development.