Most of the developers know how to use interfaces as described in books and sample code available on the internet but they fail to ask what is the need of interfaces. Why they were added to Java?
Interfaces had been added to Java language since its inception. The implements keyword is used in conjunction with interfaces and provides a way to override the methods defined in an interface. All the methods of an interfaces are abstract by default. Any variables defined in an interface are public static final by default.
computer interface
While creating design documents for your application , do watch out for which super type should be interface and which should be an abstract class.
Interfaces are used for binding multiple children of same type. The advantages of using interfaces include:
Multiple inheritance
The diamond problem associated with multiple inheritance of concrete methods has been avoided by the use of interfaces. This is because of the fact that the methods in an interface are all abstract.
Hierarchy
Many a times, multiple classes, do fall under one umbrella category but are not very significantly associated with each other in terms of behavior.
For example the Exception class and String class are Serializable (their object state can be persisted). The Exception and String classes are used in completely different scenario and hence it doesn’t make sense to bind them with the help of a common ancestor. But we definitely need to mark these classes as Serializable. In that, case interfaces come handy which only provide definition of a member variable but don’t provide any implementation code.
Difference from Abstract classes
The interfaces are also used to bind abstract classes. Take the example of Collections hierarchy from the java.util package of JDK. The AbstractCollection class implements the Collection interface. The AbstractCollection class is inherited by AbstractMap, AbstractSet and AbstractList classes and these sub classes in turn also implement Map, Set and List interfaces.
All variables are constants
Some developer tend not to use classes for declaring constants (the fields marked as public static final) and prefer using interfaces for declaring constants.
An abstract class with all methods as Abstract?
Abstract classes could also provide similar behavior as provided by interfaces (when all the methods of an Abstract class are abstract).
But one feature of Java is that only one class can be the direct parent of another class. Hence if you need to have multiple non-significant behavior and one significant behavior, interfaces are the only rescue. This is because of the fact that a class can implement multiple interfaces.
It is worth mentioning here that the interface keyword should not be confused with @interface annotation. The @interface annotation is used for defining custom annotations for an application.
This comment has been removed by the author.
ReplyDeleteInspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
ReplyDeleterpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune
Very well written blog and I always love to read blogs like these because they offer very good information to readers with very less amount of words....thanks for sharing your info with us and keep sharing.
ReplyDeletePython training in marathahalli | Python training institute in pune
This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me..
ReplyDeleteData Science training in kalyan nagar | Data Science training in OMR
Data Science training in chennai | Data science training in velachery
Data science training in tambaram | Data science training in jaya nagar
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteDevops Training courses
Devops Training in Bangalore
Best Devops Training in pune
Devops interview questions and answers
This post is much helpful for us. This is really very massive value to all the readers and it will be the only reason for the post to get popular with great authority.
ReplyDeleteAWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery
Very interesting post! Thanks for sharing your experience suggestions.keep t up!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
I’m planning to start my blog soon, but I’m a little lost on everything. Would you suggest starting with a free platform like Word Press or go for a paid option? There are so many choices out there that I’m completely confused. Any suggestions? Thanks a lot.
ReplyDeleteweb designing training in chennai
web designing training in omr
digital marketing training in chennai
digital marketing training in omr
rpa training in chennai
rpa training in omr
tally training in chennai
tally training in omr
Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable
ReplyDeleteso, here’s my fine read.thanks..
data science training in chennai
data science training in tambaram
android training in chennai
android training in tambaram
devops training in chennai
devops training in tambaram
artificial intelligence training in chennai
artificial intelligence training in tambaram