Results 1 to 14 of 14
  1. #1
    Eyeinstyin is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default What is advantage of interface in java?Plz help

    Please explain in dumbest language possible.So i can understand
    check this example

    Java Code:
    interface BaseInterface
     { 
        int i=0;
     }
    interface BaseInterface2
     {
        int i=1;
     }
    class SubClass implements BaseInterface,BaseInterface2
     {
        public static void main(String args[])
         {
             System.out.println(BaseInterface2.i);
         }
     }
    it works fine

  2. #2
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    just it is used to define some general information which can be used for specific purpose as we can define an interface Vechile with some attributes and method as drive(). Now this interface can be implemepented by some another classes as Car, Bus. now these classes define their own way to how to drive them. Isn't a sufficient

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In your example there are two interfaces and a class with the main method.

    An interface is a collection of method definitions, empty bodies, and constant values. So you cannot modify those variable values later in your application.

    Interface form a relationship between the Java class and the outside world. This relationship validate in compile time. So if a class implement and interface, all the methods of that interface must be coded before compile.

    In your case there is no any methods to implement in the interface implemented class. Only have variable, constant variables.

    If you want to read about more, go the the Suns' tutorial. There are nice explanation on this. Good luck!

  4. #4
    Eyeinstyin is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    ty for replies
    Is not the problem of ambiguity still there??
    i actually expect answer likes problems faced without interface and thus solved by using them.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    i actually expect answer likes problems faced without interface and thus solved by using them.
    I don't see how these explanations would have any bearing on your examples as they're not good examples of the beneficial use of interfaces. Probably better than our trying to rewrite a chapter for ya (especially since your question is so general and broad), I'd recommend you read some good tutorials on interfaces and then come back if you have any specific questions. A decent place to start is here: Sun Interface Tutorial

    Best of luck!

    edit: one more tutorial that's somewhat simplistic but briefer than the one above: Game programming Wiki: Java: Tutorials: Interfaces
    I still recommend the first link as the more complete tutorial, but I'd recommend that you read it second.
    Last edited by Fubarable; 01-05-2009 at 07:53 PM.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Interfaces are used to allow a group of classes to say they implement a certain set of methods. Another class can then declare a field of the interface type and use the methods of any of the classes that implement that interface.

    This is more useful for advanced programming; you certainly can write applications without creating interfaces.

  7. #7
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    Basic use of interface
    Suppose you have three classes Ferrari, Toyota and Ford. All these are Cars and can move().
    Java Code:
    Ferrari scaglietti = new Ferrari();
    Toyota yaris = new Toyota();
    Ford focus = new Focus();
    If you wanted to call move() for all 3 objects you should code like this:
    Java Code:
    yaris.move();
    scaglietti.move();
    focus.move();
    But if you had 1 thousand of cars it would be impossible. So you say
    Java Code:
    interface Car { 
       void move();
    }
    
    class Ferrari implements Car { ... public void move() ... }
    class Toyota implements Car { ... public void move() ... }
    class Ford implements Car { ... public void move() ... }
    And then you can:
    Java Code:
    Car[] myCars = { ... }
    
    for(each car in myCars) {
       car.move();
    }
    It is called an UPCAST. This is one of the major goals of interfaces. Of course, there are lots of other things, you'd better read the Sun Tutorial linked by Fubarable

  8. #8
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    The main use of interface is to inherit multiple inheritence.
    i think you are beginner of java.
    i hope you know java does'nt support multiple inheritence,to overcome this ,interface is used often.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by makpandian View Post
    The main use of interface is to inherit multiple inheritence.
    And I disagree with this strongly. I believe that the main use of interface is to loosen coupling between a public contract and the private implementation of the contract.

  10. #10
    Eyeinstyin is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    contract thing sounds convincing, as(i think) there was no such mechanism in c++ i.e interface.
    However most website said multiple inheritance as advantage.So i actually thought that interfaces remove disadvantage of multiple inheritance i.e ambiguity(which is what my example, in earlier post, is)...HOwever this advantage (i think) is only for java not having multiple inheritance with classes.
    I have already visited that sun interface tutorial but with "Multiple Inheritance Disadvantage in Mind" and neglected others.
    And Sorry guys i'm pretty poor at grasping things and thnks for your replies.

    If i'm wrong please correct me.
    Last edited by Eyeinstyin; 01-07-2009 at 09:10 PM.

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Eyeinstyin's last post has the right information; let me try to clarify a bit.

    Java was designed to be a "better" C++. C++ allows you extend multiple classes. This is powerful, but it can lead to problems. The Java designers decided to allow only one class to be extended. To make up for that, they created interfaces. A Java class extends one class and can implement many interfaces. Interfaces do serve as contracts, guaranteeing that the class specifies all the specified methods.

  12. #12
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    I dont say that interface are used only for an alternative of multiple inheritance .
    What i said is ,one of the usage of interface is an alternative of multiple inheritance...

    I agree with fubarable that interface are being used for loose coupling..

    Any by interface we can force a class that should do certain things..

    Thanks Fubarable..
    Mak
    (Living @ Virtual World)

  13. #13
    ankkha is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Re: What is advantage of interface in java?Plz help

    Hi

    What would you do when someone asks you to buy 10 things or more than that which you can not hold it by your hands , certainely you will take help of some bag by which you will not worried about to hold 10+ things by your hand , now you just need to hold only that bag.

    Now think about a situation when your friend suddenly come to your home and ask 5-6 things from that bag and you say yes i have those and i can give it to you but the condition is if you want 5-6 things you also need to carry other things too which ppl called contract which you must need to follow.

    Please let me know how much i m correct to tell you about interface.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: What is advantage of interface in java?Plz help

    This is a very old thread. Please check the date before posting and do not resurrect old threads. Closing this thread.

Similar Threads

  1. please (how to use interface like jbutton in java)
    By sheckoo in forum New To Java
    Replies: 9
    Last Post: 10-29-2008, 07:28 AM
  2. Replies: 7
    Last Post: 09-16-2008, 07:20 PM
  3. interface C programming with Java
    By kapil1089theking in forum AWT / Swing
    Replies: 8
    Last Post: 09-11-2008, 06:09 AM
  4. Interface question in java
    By tony404 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 12:47 PM
  5. Interface java.io.Externalizable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-19-2007, 06:53 PM

Posting Permissions

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