Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 default methods vs. non-abstract methods in abstract classes

Java 8 interface default methods vs. non-abstract methods in abstract classes - are there any differences between the two (besides the differences of iface - class, visibility etc.)

Isn't a default method a step back in Java, meaning it's against the essence that Java has advertised for years?!

like image 913
Parobay Avatar asked Nov 22 '13 07:11

Parobay


1 Answers

non-abstract methods in abstract classes will be called when it's concrete subclass calls super() if it is overridden. So there are multiple possibilities. If method is not overridden then the super class method will be executed. if we use super() in the concrete subclass method then the overridden method with the super class method will be executed.

Where as Java 8 interface default methods are completely different. It provided a choice to the developers to implement the method in the implementing class or not. If the function is not implemented then and only then the default method will be executed.

Possible Use Case :

The most important use case for this new feature in the JDK libraries is the possibility to extend existing interfaces without breaking existing implementers: adding a new abstract method to an interface would require all implementing classes to implement that new method.(Source)

like image 98
Aniket Thakur Avatar answered Oct 28 '22 10:10

Aniket Thakur