Thursday, June 17, 2010

Public defender methods - yet another Java 7 proposal

Support of lambdas and closures is the most important language feature that is proposed to ship with Java 7. With lambdas, it will be possible to implement some interesting methods into the old collection classes. Methods like find, select, reject, forEach, include can take a lambda and perform relevant operations on the collection object. However, adding these methods into existing collection interfaces would be a problem as it will break the existing implementations. To address this issue, the proposal of defender method is suggested.

Inherently static nature of Java restricts the developer from extending the functionality without affecting the existing usages. The current implementation of interfaces doesn't allow us to add a new method to interface without breaking all its implementation. Sometimes, there is a strong case for adding new behaviour in the interface so that its applicable to all the implementations. Currently the only way to do this is to change interface into an abstract class and change the code to extend this abstract class. However, we need to change at all the places where this interface is implemented and anyway, this change is not always possible because a class might already have a superclass. The proposal of public defender method suggests a solution.

This proposal allows to provide a default implementation for interface methods. This default implementation will be a static method on helper class and it will be invoked if the class implementing this interface does not provide any custom implementation.

The proposed syntax is -

public interface List {
extension public List select(#boolean(Object)) default ListHelper.select
// other list methods
}

Here, select method from the List interface takes a lambda (which is again a proposal) as argument. The 'extension' keyword signifies an extension method. It must have a default implementation which is provided by static select method from ListHelper class. This method has the same signature as the extension method and it will receive the the extended interface object (list) as the first argument. If the class implementing this List interface does not provide implementation, the select method from the ListHelper class will be used. This method from the helper class which provides the default implementation is called as public defender method, as it defends the classes that does not implement extension method.

Obviously, the specific implementations of the interface class can provide an optimised implementation for extension method.

As mentioned in the Baptiste Wicht's blog, with this approach, it is possible to include the static methods in Collections class to the List interface. This will allow us to invoke methods like reverse() on the list object itself, rather than passing the object to the static method in Collections class. Also, it will allow us to override these methods.

In general, this proposal will allow us to add the relevant methods to interfaces and be dependant on the classes with static utility methods.
so we will be able to do -

list.select(..)

instead of

ListHelper.select(list, ..)

The helper class will still hold the implementation, but objects need not be aware of them.

Along with the ability to extend the already existing interfaces, this proposal will have more profound effect on the programming style. It will be possible to create a interface in which, all the methods are extension methods with default implementation. Then, it will be possible to use this interface as 'mixin' and use it to achieve the effect of multiple inheritence. Also, abstract classes may not be required as the same functionality can be achieved by interfaces with few defender methods.

We can compare this proposal with the extension methods introduced in C# 3.0. Although not exactly same, conceptually both are same. C# extension methods allows us to add methods in the existing classes by defining the static methods in another static class. However, The visibility of C# extension methods is much more controller as the these methods are available only in the classes that import the static class with extension method. This is different from defender methods where extension methods will be available to all the implementations of the interface.

At the moment, defender methods is only a proposal for Java 7 and just like other proposals, we are not whether it will come up with Java 7. but nevertheless, this interesting language feature will improve the extendibility of dying Java language and will allow us to extend the existing library.

64 comments:

Dom Casas said...

Hi there, thank you for your post. I was browsing around the web trying to learn java and I came across your site.

IT said...

Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

abril joseph said...

Your information about Java is useful for me to know more technical information. Really very informative post you shared here. Keep sharing this type of informative blog. If anyone wants to become a Java professional learn Java Training institute in Chennai. Nowadays Java has tons of job opportunities for all professionals.
Java Training Institute in Chennai |
Dot Net Training Institute in Chennai |

Unknown said...

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.Ruby on Rails Online Course Bangalore

Unknown said...

Thank you for offering such a unique information really helpful for learners one of the recommended blogs...
Java Training in Chennai

evergreensumi said...

I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.nebosh courses in chennai

Anuradha Singhaniya said...

The blog was absolutely fantastic! Lot of great information. Keep sharing more blog. Great job, keep it up. Thanks for this nice post..
If you looking for best advance java training in Gurgaon visit us

Unknown said...

Nice blog... Java Training in Chennai

Unknown said...

Great blog created by you. I read your blog, its best and useful information. You have done a great work.
Java training in chennai

kingsly david said...

Great blog! Really awesome I got more information from this blog. Thanks for sharing with us.
Regards,
Dot Net Training in Chennai

Aruna Ram said...

Good post.It's very nice.Keep it up.More links posted.Thank you for your posting.
java training in chennai
Java course in Chennai
Java Training Institute in Chennai
Best Java Training Institute in Chennai
Java Classes in Chennai
Core Java Training in Chennai

Madhu Bala said...

Best explanation ever, previously I was looking for a info about this technology and now I am Clear. Thanks
Selenium Training in Chennai
Best selenium training in chennai
iOS Training in Chennai
Digital Marketing Training in Chennai
.Net coaching centre in chennai
Big Data Training in Chennai
French Classes in Chennai

Praylin S said...

Very informative post! Thanks for this great effort. Regards.
Manual Testing Training in Chennai | Manual Testing Courses in Chennai | Manual Testing Training | Manual Testing Training Institute in Chennai | Manual Testing Course | Manual Testing Course in Chennai | Manual Testing Training Chennai

ajithajohn said...

Such an interesting blog for this technology really helpful for me.
waiting for new post. Keep updating.
dot net training in chennai
dot net course in chennai
ielts coaching in chennai
machine learning course in chennai
oracle training in chennai
dot net training in t.nagar
dot net training in omr

Harishnath said...

hi Admin,
Very informative post! I can see your great effort. Keep posting.

URL: www.oscaritsolutions.com

rohini said...

Here they shared it’s makes me to appear very insightful,useful, & interesting.
Mobile service center in chennai | iphone service center in chennai | ipod service center in chennai | ipad service center in chennai | 100% genuine mobile parts | Mobile phone display replacement | Mobile display replacement | Mobile service center in chennai | Mobile display replacement | Tablet service center in chennai | Latop service center in chennai | Mobile service center in chennai

sandeep saxena said...



The info is good enough.I preview this type of good article only. Good job
clinical sas training in chennai
clinical sas training
clinical sas Training in OMR
clinical sas Training in Porur
SAS Training in Chennai
SAS Institute in Chennai
SAS Training Institute in Chennai
QTP Training in Chennai

daphnemohara said...

Nice post. Thanks to sharing your information. keep doing.
IOSH Course in Chennai
NEBOSH Safety Course in Chennai
NEBOSH Course in Chennai
Safety Audit Consultants
ISO Consultants in Chennai
Electrical Safety Audit

firstfly said...

Thanks for sharing a piece of valuable information to us. keep on sharing like this.

Best Aviation Academy in Chennai

Best Air hostess Training in Chennai

Pilot Training in Chennai

Airport Ground Handling Training in Chennai

Airport Flight Dispatcher Trainee in Chennai

RTR- Aero Training in Chennai

Aviation Academy in Chennai

Cabin Crew Training in Chennai

Aviation Training Institute in Chennai

sheela rajesh said...

Nice way of expressing your ideas with us.thanks for sharing with us and please add more information's.
JAVA Training in Chennai
Best JAVA Training institute in Chennai
SEO training in chennai
Python Training in Chennai
JAVA training in Annanagar
Java courses in chennai anna nagar

Anonymous said...

Thanks for sharing your valuable information and time.
Java Training in Delhi
Java Training institute in Delhi

shreekavi said...
This comment has been removed by the author.
sridharraj said...

awesome article, content has very informative ideas, waiting for the next update...
Hibernate Training in Chennai
Hibernate course in Chennai
Hibernate Training institute in Chennai
hibernate training in T nagar
hibernate training in Guindy
Spring Training in Chennai
clinical sas training in chennai
DOT NET Training in Chennai
QTP Training in Chennai
LoadRunner Training in Chennai

Anonymous said...

Its very valuable information. Thankyou for sharing it
data science institute in kphb

shemadevan said...

Awesome blog, informative content...thanks for sharing...
LoadRunner Training in Chennai
Loadrunner Training
loadrunner training in vadapalani
loadrunner training in Guindy
loadrunner training in Thiruvanmiyur
QTP Training in Chennai
core java training in chennai
C C++ Training in Chennai
Mobile Testing Training in Chennai
Manual Testing Training in Chennai

Simran Roy said...

Thanks for sharing!!
Best java training institute in Offline & Online at Codingblocks.com.

Technical Assistance said...

Good job. I would like to appreciate your hard work towards delivering the blog in a informative way.
Digital Marketing Institute
Coupondunia
best matrimonial site
redbus ticket online booking

janathan said...

very informative post...
Free Inplant Training Course For ECE Students
INTERNSHIP
INTERNSHIP FOR AERONAUTICAL ENGINERING STUDENTS IN INDIA
INTERNSHIP FOR CSE 3RD YEAR STUDENTS
Free Inplant Training Course for Mechanical Students
INTERNSHIP FOR ECE STUDENTS
INPLANT TRAINING FOR CIVIL
INTERNSHIP AT BSNL
INTERNSHIP FOR 2ND YEAR ECE STUDENTS
INTERNSHIP FOR AERONAUTICAL STUDENTS

janathan said...

i'm happy with ur blog...
foreach loop in node js
ywy cable
javascript integer max value
adder and subtractor using op amp
"c program to find frequency of a word in a string"
on selling an article for rs 1020, a merchant loses 15%. for how much price should he sell the article to gain 12% on it ?
paramatrix interview questions
why you consider yourself suitable for the position applied for

prashanth reddy j said...

A great Blog!! I see a lot of Information relating JAVA...Its good to see such tech info In blogs
Best IT/Software Training Institutes in Kompally Hyderabad | Lasya Infotech

Bhanu Ravi said...

Thanks for sharing this wonderful and useful information...
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore

Training for IT and Software Courses said...

I really enjoy reading this article. Hope that you would do great in upcoming time.A perfect post. Thanks for sharing.SAS Training in Bangalore

Vijaykumar said...

nice article..
Inplant Training in Chennai
Iot Internship
Internship in Chennai for CSE
Internship in Chennai
Python Internship in Chennai
Implant Training in Chennai
Android Training in Chennai
R Programming Training in Chennai
Python Internship
Internship in chennai for EEE

Hi-Tech Laxmi Nagar said...

Really its very useful information that you have shared and looks great post.Thanks for sharing the information with us.

Mobile Repairing Course in Laxmi Nagar

Jenifer said...

Very nice information, it is valuable and useful to so many people. It gives the beautiful knowledge especially to the technical people.

advanced java training institutes in bangalore

advanced java training in bangalore

best advanced java training institutes in bangalore

advanced java training course content

advanced java training interview questions

advanced java training & placement in bangalore

advanced java training center in bangalore

mindreader said...

Thanks for the interesting blog that you have implemented here. Very helpful and innovative. Waiting for your next upcoming article.
Java Training in Chennai
Java Training Institute in Chennai
Java course in chennai
Java Training classes
Java Training
Java programming classes
core Java course

manisha said...

This is good information and really helpful for the people who need information about this.
Data Science Training in Delhi
Data Science Training institute in Delhi

Faizal said...

Excellent works!!!Information's are amazing visit here for more
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

Janu said...

Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.



Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery







Janu said...

Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.



Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery







subha said...

Nice post. Thanks for sharing! I want people to know just how good this information is in your blog. It’s interesting content and Great work. enjy to read this blog
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

Quadsel Systems said...

We provide you with flexible services and complete hybrid network solutions. It can provide your organisation with exceptional data speeds, advanced external security protection, and high-resilience by leveraging the latest SD-WAN and networking technologies to monitor, manage and strengthening your organisation’s existing network devices.

www.quadsel.in/networking/
https://twitter.com/quadsel/
https://www.linkedin.com/company/quadsel-systems-private-limited/
https://www.facebook.com/quadselsystems/
https://www.facebook.com/qspl.quadsel.1/posts/722676211821078

#quadsel #network #security #technologies #managedservices #Infrastructure #Networking #OnsiteResources #ServiceDeskSupport #StorageServices #WarrantyAMCServices #datacentersolutions #DataCenterBuild #EWaste #InfraConsolidation #DisasterRecovery #NetworkingServices #ImagingServices #MPS #Consulting #WANOptimisation #enduserservices

Tanika Co Valda said...

The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.


The Nodejs Training Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

jenani said...

This is very good content you share on this blog. it's very informative and provide me future related information.
Java Training in Chennai

Java Training in Velachery

Java Training inTambaram

Java Training in Porur

Java Training in Omr

Java Training in Annanagar


vanathi said...

its really amazing article and thank for this wonderful article share with us and keep posting like this.
Software Testing Training in Chennai

Software Testing Training in Velachery

Software Testing Training in Tambaram

Software Testing Training in Porur

Software Testing Training in Omr
Software Testing Training in Annanagar

subathara said...

Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog
Digital Marketing Training in Chennai

Digital Marketing Training in Velachery

Digital Marketing Training in Tambaram

Digital Marketing Training in Porur

Digital Marketing Training in Omr

Digital MarketingTraining in Annanagar

ramya said...

Thanks for sharing such a amazing article,it was really useful and i would like to thank you for this article.Am very much intrested to learn new thing like this.keep posting like this valuable information.amazon web services aws training in chennai

microsoft azure training in chennai

workday training in chennai

android-training-in chennai

ios training in chennai

Aishwariya said...


Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.
Amazon Web Services Training in Chennai

selvam said...

Good blog!!! It is more impressive... thanks for sharing with us...
Software Testing Training in Velachery
Python course
German course
SQL course in Chennai

Vijayakash said...

I really enjoyed this article. I need more information to learn so kindly update it.

RPA Training in Tambaram
RPA Training in Anna Nagar
RPA Training in Velachery
RPA Training in T nagar
RPA training in Porur
RPA Training in OMR
RPA Training in Chennai

Vijayakash said...

"Valuable one...thanks for sharing..
graphic design courses in tambaram
graphic design courses in Porur
graphic design courses in Chennai



chitra said...

Informative blog... Thanks for sharing and keep updating
Ethical Hacking Course in Chennai
Ethical Hacking course in Bangalore

salome said...

interesting to read and useful article.keep up the good work
Angular training in Chennai

high technologies solutions said...

You write this post very carefully I think, which is easily understand to me. Not only this, other post is also good. As a newbie this info is really helpful for me. I think people should go through it. Thanks to you. if you looking for the best SAS Training Institute in Delhi So Contact Us-9311002620 Or Visit Our Website-https://www.htsindia.com/Courses/business-analytics/sas-training-institute-in-delhi

Dreamsoft Consultants said...

Happy to read this type of valuable content ,like the way of presenting your knowlege.If anyone want to get experience in Delhi can contact me at - 9599119376
Experience Certificate In Delhi
Experience Certificate In Bangalore
Experience Certificate In Hyderabad
Experience Certificate In Noida

bruce wayne said...

Great blog.thanks for sharing such a useful information
Selenium Training

Jothi said...

Excellent Blog to read. You have shared useful information. Thank you.
SEO Course
SEO Online Course

manasa said...

Remarkable blog liking your unlimited endeavors in thinking of a phenomenal substance. Which maybe propels the peruses to feel invigorated in getting a handle regarding the matter without any problem. This clearly makes each peruse to thank the blogger and trust the comparative innovative substance in future as well…

AI Training in Hyderabad

BK-25 said...

Interesting blog thank you for sharing.

Best software training institute in Chennai.
azure course in chennai
RPA Training in Chennai
DevOps Training in Chennai
Cloud-computing Training in Chennai
Ui-Path Training in Chennai
PHP Training in Chennai
Blue-Prsim Training in Chennai

David Fincher said...


This post is so interactive and informative.keep update more information...
Software testing training in Tambaram
Software testing training in chennai

Matt Reeves said...

This post is so interactive and informative.keep update more information...
Web Designing Course in Tambaram
Web Designing Course in chennai


Block said...

I feel really happy to have seen your webpage and look forward to so
many more entertaining times reading here. Thanks once more for all
the details.

Muskan said...

I like your blog! Your explanation of the proposed defender method in Java 7 is clear and informative. It's great to see how this feature can address the challenge of extending functionality without breaking existing implementations.

Java has endured over time

archijain931 said...

Thank you for sharing this wonderful content. I thoroughly enjoy reading such high-quality material that provides valuable information. The presented ideas are outstanding and innovative, making the post truly delightful. Keep up the fantastic work!
Visit : Mastering Full Stack Development: A Comprehensive Guide