aspose file tools*
The moose likes Features new in Java 8 and the fly likes default methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "default methods" Watch "default methods" New topic
Author

default methods

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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.


Groovy
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Jul 13, 2011
Posts: 84
    
    5

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/.


Richard Reese
Java 8 New Features: A Practical Heads-Up Guide
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
Well, not in that situation anyway.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default methods