mmc18 mmc18 - 1 month ago 13
Java Question

Does Java have plan that default method (java8) Substitute for Abstract Class?

Does Java have plan that

default method
substitute for
Abstract Class
?
I could not find a real case to use default method instead of Abstract?

Answer

Default methods can't substitute abstract classes, as abstract classes can (and often do) have fields. Interfaces can only contain behaviour and not state, which is unlikely to change in the future as multiple inheritance of state in Java is seen (rightly or wrongly) as evil.

They can also have final methods, which is another thing you can't mimic with default methods.

If anything, interfaces with default methods resemble traits rather than abstract classes, but the match isn't perfect. Using interfaces as traits is something that has to be done very carefully and knowing the limitations they come with. (Such as any implementing class can override a default method, potentially ruining the trait.)

More on this here.

Comments