-1

While going through the answers for "Need of the defender methods" I came across this answer

Can you please elaborate what does the below mean in the accepted answer?

you should be able to switch to Java 8 without having to implement new methods in your class.

ghostrider
  • 2,046
  • 3
  • 23
  • 46

1 Answers1

6

The asker of the original question, M Sach, said that instead of default methods, just put the default implementations in an abstract class.

Eran, the answerer, says that this would break existing code.

For example, suppose you have your own List implementation written in Java 7, which does not inherit from AbstractList. When you migrate your code to Java 8, what happens? There is suddenly a bunch of new methods in the List interface that your own List implementation does not have (the "default" implementation of those methods are in AbstractList, which your class doesn't inherit)! Your code will no longer compile.

This means that when migrating to Java 8, you'd potentially have to write new methods for existing classes.

With default methods however, the default implementation is in the interface itself, so you don't have to write new methods.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • thanks for the answer. So you are saying that in M Sach's world you can have default implementations of methods in abstract class and also there "abstract" declaration in List Interface? But why do we need that in the first place? Can't we have only the default implementation in abstract class and not their abstract method signatures in List? – ghostrider Feb 03 '20 at 04:03
  • 1
    @ghostrider the default methods and abstract methods are different sets of methods. What used to be abstract should still be abstract, right? Remember that the whole issue we are trying to solve here is that in Java 8, they wanted to add some more concrete methods to implementers of certain interfaces, so M Sach suggested that we should just put those methods in an abstract class that’s between the interface and the implementer. – Sweeper Feb 03 '20 at 06:34
  • just to be on the same page, here is the link https://docs.google.com/document/d/1zPP2uhltJERRY81uGxYYZ3oK4DFsVy-2y1R-5shBCos/edit?usp=sharing which describes the class diagram when I migrate to java8 in the case of M Sach's scenario. Is that correct? My question is why do we need to have the test() method in the List interface in the first place? Why not have only the test() in AbstractList? – ghostrider Feb 03 '20 at 06:44
  • @ghostrider oh so that’s what you mean! My previous comment kind of misunderstood you. But as I said, the whole point of this is to have common functionality shared among _interface_ implementers. Their goal was to give all `List` a `forEach` method, for example, so the method not being in the interface kinda defeats the purpose. – Sweeper Feb 03 '20 at 07:10