• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default methods

 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some how I don't seem to like the default methods introduced in Java 8. When I started to learn Java interface had no implementation but no they have methods. Didn't Java API designers have any other means to introduce streaming for collections. Can the authors of Java 8 share their opinion. Also do default method support method overloading and overriding.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried to overload or override a default method?
How else are you going to add Streams to the List interface without breaking every implementation which does not extend AbstractList?
 
Richard Reese
author
Ranch Hand
Posts: 84
5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep,

The addition of default methods is a controversial one for many. Time will tell, however, I feel they are a good addition. I don’t have much insight into the developer’s rationale. Maurice Naftalin’s comments are a good starting point for further information about this topic: http://www.lambdafaq.org/what-are-default-methods/.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave a presentation today on Java 8 interfaces and Dates. I made a comment that "if you add a default method to new code, you are doing something wrong." I think the use of adding functional type methods to collections is a good one.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I understand this is the only way to add code to List interface but it looks odd.

Jeanne Boyarsky wrote:I gave a presentation today on Java 8 interfaces and Dates. I made a comment that "if you add a default method to new code, you are doing something wrong." I think the use of adding functional type methods to collections is a good one.


That is a good point. If we need to add a default method then may be it should be an abstract class.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, adding the default method to the abstract class will not always work; some classes will implement the interface without extending that abstract class. And it wouldn't be a default method in an abstract class; it wouild be a pain simple concrete method, probably designed so as to be overridden.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, adding the default method to the abstract class will not always work; some classes will implement the interface without extending that abstract class. And it wouldn't be a default method in an abstract class; it wouild be a pain simple concrete method, probably designed so as to be overridden.


what I meant was if someone is planning to create a new interface with a default method they can very well create an abstract class with a common method. Anway you have made a very good point that method can be overriden.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can, but you can't both create a new interface and a new abstract class unless they both have the method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, not in that situation anyway.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic