Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1
    WTF Groupie Array
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    0
    Thanked 1 Time in 1 Post

    Advantages of interface in java

    What are the advantages of interface in java?

  2. #2
    WTF Lurker Array
    Join Date
    Mar 2011
    Posts
    56
    Thanks
    1
    Thanked 0 Times in 0 Posts


    Main purpose of interface is to provide polymorphic behavior.
    Just it is used to define some general information which can be used for specific purpose as we can define an interface with some attributes and method.

  3. #3
    WTF Lurker Array
    Join Date
    Jul 2011
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts


    interface is like polymorpism to give you the methods for using without objects.

  4. #4
    Junior Member Array
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts


    Very useful information for me.

  5. #5
    WTF Lurker Array
    Join Date
    Nov 2011
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts


    Interface help you to get access the class variable and function which are in public function and class.

  6. #6
    Junior Member Array
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts


    If anyone is searching in the future I'll leave a good example:

    Lets say you get a request to aggregate calendars into one master calendar. This is actually a fairly common request and the common approach might be to create a MasterCalendar object that has many methods for every calendar. It might have a GetOutlookCalendar(DateTime start, DateTime end) and maybe a GetGoogleCalendar(DateTime start, DateTime end). This can end up being a monster class if down the road the requirements change from 2 calendars to 20 calendars. How do we fix this? Interfaces + Polymorphism.

    Lets take out the common functions and add a few more objects to the equation and we'll end up with a pretty elegant solution. What both of those methods (and virtually all calendars) have in common is the GetEvents(DateTime start, DateTime end) so lets make that the interface:

    interface ICalendar
    {
    GetEvents(DateTime start, DateTime end);
    }

    Now we can make an Outlook calendar class that implements this interface like so:

    class OutlookCalendar : ICalendar
    {
    GetEvents(DateTime start, DateTime end)
    {
    //go out to outlook and grab events
    }
    }

    And lets build the same thing for the Google calendar

    class GoogleCalendar : ICalendar
    {
    GetEvents(DateTime start, DateTime end)
    {
    //go out to google and grab events
    }
    }

    Now for the main MasterCalendar object all you have to do is add every calendar you want to implement to an ArrayList and for-each over the collection like so:

    class MasterCalendar
    {
    GetEvents(DateTime start, DateTime end)
    {
    ArrayList calendarList = new ArrayList();
    calendarList.add(new OutlookCalendar());
    calendarList.add(new GoogleCalendar());

    for (ICalendar calendar : calendarList)
    {
    listOfEventsToReturn += calendar.GetEvents(start, end);
    }
    }
    }

    Now if someone asks for another calendar we just need to implement the ICalendar interface and add the new object to the calendarList array. I hope this was helpful!

  7. #7
    WTF Restricted Array
    Join Date
    Nov 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    • Interfaces are classes that help us in implementing partial Multiple Inheritance in Java.
    • It helps us in defining skeleton behavior for our classes.
    • Also, it is used for creating classes that store application level constants



    pdms training centres chennai|pdms training centres chennai

    .................................................. .................................................. ...............

  8. #8
    Junior Member Array
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts


    What is this advantages of interface in java

  9. #9
    WTF Restricted Array
    Join Date
    Apr 2011
    Posts
    272
    Thanks
    0
    Thanked 6 Times in 6 Posts


    • Interfaces are classes that help us in implementing partial Multiple Inheritance in Java.
    • It helps us in defining skeleton behavior for our classes.
    • Also, it is used for creating classes that store application level constants

  10. #10
    WTF Groupie Array
    Join Date
    Sep 2011
    Location
    Tustin, CA
    Posts
    143
    Thanks
    1
    Thanked 1 Time in 1 Post


    Java has become the tool of mainstream programming from web to mobile world everything is now based on java.Java is a platform independent language that is used for various application development .The features of object oriented programming like polymorphism,. inheritance are indeed its core attributes but the concept of java byte code make it the winner as it provides the concept of platform independence.


 
Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •