Search This Blog

Saturday, January 19, 2013

Why Interfaces are there in Java ..?


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.

9 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Inspiring 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..

    rpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune

    ReplyDelete
  3. 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.
    Python training in marathahalli | Python training institute in pune

    ReplyDelete
  4. 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.
    Devops Training courses

    Devops Training in Bangalore

    Best Devops Training in pune
    Devops interview questions and answers

    ReplyDelete
  5. 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.

    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  6. 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.
    web 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

    ReplyDelete
  7. 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
    so, 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

    ReplyDelete