Understanding interfaces

i am new to java programming and am struggling to understand what an interface is and when i would ever use one. Please explain... simply... or direct me somewhere appropriate.
Thanks
J

The Java Tutorial: Learning the Java Language: Interfaces and Packages: Creating and Using Interfaces

Similar Messages

  • Well.... i couldn't understand Interface...!!

    hi dears.....
    I could not understand Interface...
    many books described interface , contains abstract method... and more...
    in package java.sql.*... almost this package consist of interface..
    but where does implement this abstract method of interface... i couldn't find this.
    example1 ...
    statement = connection.createStatement();
    statement : Statement interface... and
    connection : Connection interface...
    connection.createStatement() <- this method is abstract and no class implement...
    example 2 ...
    in java.awt.peer package
    FramePeer has abstract method setTitle(String title)
    but it never implement... i guess..
    please help me... to understand interface..
    hava fun...~~

    It doesn't matter how the interfaces in java.sql are implemented, only that they are. For instance, if I am running code in an AppServer
      DataSource ds = (DataSource) context.lookup("jdbc/MyDB");
      Connection conn = ds.getConnection("usr", "pwd");
      Statement stmt = conn.createStatement();Then I have got a statement object that behaves as the interface java.sql.Statement says it does. If my AppServer is IBM's WebSphere, then the likelihood is that it is some com.ibm... class that is implementing Connection. If it is WebLogic it will probably be a com.bea.wl..... class.
    The whole point of an API is that it is irrelevant.

  • Trying to Understand Interfaces

    Hi all,
    As you might expect, I am new to JAVA. I have read the basics tutorial on interfaces and also the object oriented programming basics tutorial. I understand that you can't implement methods or variables in interfaces, etc. Here is what I am struggling with:
    A friend has written a class that uses the JNDI classes to query an LDAP service for person information and update database information. I notice that the query returns an object of type NamingEnumeration. But this is an interface, not a class. When he gets the items/elements from the Enumeration, he uses a (Attributes) to cast the results to their proper type.
    So, if I am writing a class and want to implement the NamingEnumeration interface over my class... and if I want to retrieve that class by its NamingEnumeration interface, rather than its actual class...how would I create a method that does this.
    My first attempt at creating this class went like this:
    public class DocumentEnumeration extends DocumentCollection implements NamingEnumeration
    If I called the .next method of my class, I would return a type Document. So far I don't know how to retrieve this DocumentEnumeration as a NamingEnumeration, and I received an error in my IDE about the constructor with arguments () is not visible. I did not understand this error either.
    Any help would be appreciated,
    Jeff

    DrClap's responses are all correct and to the point. Let me add a preface. Forget everything you've read, close your eyes and say "interfaces are dead simple!" about a hundred times. They are.
    An interface is a bundle of related capabilities. For example, I am a male homo sapiens (my class). I ski and play tennis - these are things I can do. As a Skier I have methods like buckleBoots(), getOnLift(), getOffLift() and skiDownHill(). All skiers implement these methods.
    TennisPlayers implement a different set of methods: serve(), volley() and so on. Some TennisPlayers may also implement Skier and some Skiers may implement TennisPlayer, but if an object of class LiftAttendant is going to call our getOnLift() method, it doesn't care about serve() or volley(). It knows that we have the handful of methods that concern LiftAttendants.
    Note that many different classes can implement an interface. Certainly some female homo sapiens implement Skier. I've also seen some apes and hot dogs implementing Skier.
    The Runnable interface defines just one method: public void run(). A Runnable is any class that claims it implements Runnable. If the class declaration claims that it implements Runnable, the compiler won't generate a .class file unless the class defines a public void run() method. When you pass a Runnable to a Thread, the Thread knows exactly one thing about the Runnable, that it has a public void run() method which the Thread can call after it is started. The Thread has no clue what it might be running, but because the object is a Runnable it can correctly call whatever.run().
    Why might a well-written method return an Interface type, not a class? Generality. The LiftAttendant need only know that an object has a getOnLift() method. So anything the implements Skier will be fine for the LiftAttendant.
    So an interface defines a set of methods (as few as one) that, taken together, constitute a capability. A class that implements these methods can claim to implement the interface, and other objects that care about that capability can say they work with Skier, or Runnable or whatever, so that they can handle lots of different things, not worrying about all the other things their Objects might implement or be.
    Very simple. Very powerful.

  • Help understand interfaces

    Hi.
    I've never used interfaces before and little experience with classes. And i''ve recently read some stuff about interfaces in AS3 and can't make a good idea about what exactly they are and where should i use them.
    Can someone enlight me on this topic?
    Thanks a lot.

    Hello,
    I recommand you to read Colin Moock's "Essential Action Script 3.0" for the basic understanding of interfaces, then when you analyze "Action Script 3.0: Design Patterns" you will see the practial usage in good OOP practices. Anyway you can always think of interface as an API for the object - If some classes implemets some interface you are sure that objects (diffrent classes) will have interface methods - this is very useful because there's no polymorph in Action Script 3.0. Common usage is to "mark" something with interface: if some classes implemets some interface objects are type of this interface, so you can have i.e. an interface typed argument in method, and then you pass as argument (diffrent class) objects. In Action Script 3.0 interface is set of public methods signatures, also setters and getters. That's my quick impression in this wide topic (interfaces are internal part of OOP).
    regards
    jk

  • How to understand Interface in Java?

    i dont know what interface is.
    because of my poor english, i cant find out what it represent. it feels so abstract that trail discribed interface, package and classes.
    could anyone help me with it?

    >
    I don't know what 's the differences between abstract
    class and interface. Actually, how can I decide which
    one of the two is correct selection when I take one
    generic term?
    An abstract class can have implementation for some or all of its methods. Subclasses that don't override a method from the abstract class will use the abstract class's implementation.
    If a method in in the abstract class is marked "abstract", that means there is NO implementation provided and subclasses are obligated to override the method.
    Interfaces have NO implementation. You might think of them as an abstract class with all abstract methods. (There might be a language lawyer out there who will disagree.)
    My other question is - it is possible to write
    abstract methods without " abstract ". Is is true or
    not?Yes, it is. Methods in an abstract class don't have to have the 'abstract' qualifier. These must have an implementation provided.
    It's possible for an abstract class to have NO abstract methods. You can provide implementation for all of them if you wish.
    Interfaces can't presume a default implementation. They're pure method signatures. All the implementation details are left to the implementing classes. Look at java.util.List and java.util.ArrayList for a good example of this.
    MOD

  • Java inheritance and interface code help required

    please help me I need your 30 mins only,,, working on assignment and I am trying to understand interface and inheritance might not need even 30 mins, please add me on skype *[deleted by moderator]*
    Or add me on
    yahoo msgr *[deleted by moderator]*
    waiting for your help.
    I have no single person friend or I can ask help from who knows java or programming. pretty much want to learn need a friend who can guide me in java.
    unfortunately have to submit assignment tomorrow.
    Thank you in advance for your time and consideration.
    Akee
    *Edited by moderator: EJP on 10/11/2012 10:38: removed your private contact details. This is a public forum, not a personal help desk. Ask your questions here or not at all.*

    Hi,
    there are lot of thread alredy posted please serach
    check following link
    http://help.sap.com/saphelp_nw04/helpdata/en/ce/1d753cab14a909e10000000a11405a/frameset.htm
    XSLT Mapping:
    http://help.sap.com/saphelp_nw04/helpdata/en/73/f61eea1741453eb8f794e150067930/content.htm
    Java Mapping:
    http://help.sap.com/saphelp_nw04/helpdata/en/e2/e13fcd80fe47768df001a558ed10b6/content.htm
    Links of blogs on java mapping...
    /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-i
    /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-ii
    /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-iii
    blog
    /people/sap.user72/blog/2005/03/15/using-xslt-mapping-in-a-ccbpm-scenario
    /people/anish.abraham2/blog/2005/12/22/file-to-multiple-idocs-xslt-mapping(file to xslt mapping)
    /people/pooja.pandey/blog/2005/06/27/xslt-mapping-with-java-enhancement-for-beginners(xslt with java enhancement function)
    Regards,
    Amit

  • SAP newbie - would like to understand integration options

    What are the choices when it comes to integrating SAP (MySAP, ECC5, Oracle Db) with another external ERP platform (Windows based with MS SQL Db).  We wish to create approx 8 interfaces (Sales, Accounts, Stock etc) between a retail ERP and SAP.  I understand that PI could be a choice but this seems expensive and we don't currently have it set up. Any help to understand interface options much appreciated.

    Thanks for your Quick mail,
    I am not able to view .So can you guide me
    Thanks
    priya

  • How java support multiple inheritance by the use of interface.

    As per my understanding, Interface is just having the signatures of the methods not the implementation.
    So How java support multiple inheritance by the use of interface?
    Answer 1: we can institate interface reference by its implemented
    class.
              ����� interface inf...
              ����� class aa implements inf..
              ����� class bb implements inf....
               Now, inf i = new aa();
               inf i = new bb();
    Answer 2: We can extends as many interface as we want in the
    single
               interface.
               i.e. interface infFirst....
               interface infSecond....
               interface infThird....
               Now ,
               interface ingMulti extends infFrist, infThird...
    By above two answers its not prity clear as per the multiple inheritance in C or C++.
               i.e.
               class first{
               method abc();....}
               class second{
               method bbc()......}
               class multi::first::second{
               we can call to abc();.....as well as bbc();
    -Please give your important suggstion on the same.(Hope I explain it well.)
    -Jeff

    The keyword implement is used only for interfaces not
    for abstract class. If i am wrong correct me.I believe your right, but I will double check.
    As for the multiple inheritence think about the following code:
    class Animal {
        //  Animal generic stuff in this class
    interface Eat {
        //  Generic stuff that models eating behavior
    interface Runs {
        //  generic methods that model running behavior
    public class Horse extends Animal implements Eat, Runs {
        //  Stuff specific to a horse
    }The Animal class is generic but has stuff in it common to all animals.
    The Eat interface models behavior that is generic to eating, all living things have to eat something to survive. Herbavore are different from carnivores.
    The Runs interface models generic behavior to running, such as speed. A cheeta definately runs faster than a human.
    This brings us to the Horse class. It extends the Animal class because it "is-a" animal, and it implements the eat and runs interface because they are behaviors a horse has.
    I hope that helps.
    Extending an abstract class is the same as extending a regular class with the exception you MUST override all abstract methods in the abstract class. Thats not too difficult but I believe when designing classes, designing an abstract can be more diffecult than modeling the base class, and generic behaviors in interfaces. JMO.
    JJ

  • Interface WD ABAP.

    Dear Experts.
    I am new with WD ABAP. In this moment I am reading the documentation "NET310 ABAP Web Dynpro".
    I never have understand the concept of Interface in Object-Oriented Programming (OOP).
    Please anyone can help me to understandad this concept?
    Regards

    Hi Carmen,
    it is a huge subject itself to explain  and i am afraid that it is not a right forum to discus.
    To understand interface, you need to understand what is Polymorphism.
    Polymorphism  in a nutshell is that the objects receives same message definition but behaves in a different manner.
    example calculate_fuel method in different classes has the same importing parameter and returning parameters but the calculations could be implemented differently in different classes.
    Interface has only definitions, you need to implement them. in this way interface concept enables to achieve Polymorphism.
    ABAP Objects does not support multiple inheritance. Therefore you can only have a single super-class. However a class can implement multiple interfaces to have kind of multiple inheritance.

  • Do Java interfaces have automatic reference counting?

    I'm actually a Delphi developer trying to understand interfaces and I'm curious about the way Java implements them.
    Here is a simple Delphi psuedo-code snippet:
    var
    // interface variable
    Intf : IInterface;
    // object reference variable
    Car : TCar;
    begin
    // instantiate TCar
    Car := TCar.Create;
    // obtain IInterface interface for Car object
    Intf := Car; // the reference count for Car is now 1
    Intf :=Nil;
    // After setting Intf to nil, the reference count is decreased to 0 and Car is automatically freed.
    Car.Free; // EXCEPTION! Car was automatically freed because the only interface reference had a reference count of 0
    What would happen in Java with the above code?
    Does Java automatically reference count all objects with interfaces?
    I know Java reference counts all objects in general so they are automatically destroyed, but I'm trying to understand the difference in interface implementation.
    Thanks for any help!

    Thanks for the replies!
    Sorry about my confusion with Java...
    Does Java support interfaces? If so, can't you have a interface reference variable like I have in my Delphi code?
    Also, if Java does support interfaces, do they have any other purposes aside from creating a list of functions/methods that can be used as a contract to interact with an object?
    Interfaces seem really useful since you can have an IDriveable interface (for example), and any unrelated classes can implement it... (eg. TBoat class, TCar class, etc) even if the classes do not have a common ancestor class.
    However, in Delphi interfaces also automatically reference count so once you create an interface reference you have to be very careful so the interface doesn't automatically free the object it is pointing to...

  • What good are interfaces

    ? dont get it. why would you make an interface? can't you just declerate all the methods in the classes?

    how experienced is someone that understands
    interfaces? because i started just 1 day with java.
    so should i try to understand interfaces now or wait
    until i understand the rest a bit better?
    (java is also the first language i learn. i know gml
    reasonably but thats a super simple language for
    people that want to make games, but not learn a
    language)I wouldn't worry about them too much at the moment. Learn other things first and then take a look at them, they are a pretty simple concept and something you need to understand rather than learn. You will bump into them often enough if you continue learning the basics (from HelloWorld upwards).
    As mentioned the Head First books are really good and you should aim to learn through books and practical exercises rather than random questions in forums. I understand your curiosity but go at your own pace and in 6 months or so things will be starting to make a lot more sense. There is obviously still a long way to go after this but the first 6 months are the pain barrier IMO.

  • About Connection, ResultSet, Statement interfaces....

    hi ,
    Connection, ResultSet, Statement etc.. all are the interfaces. But when i am writing the codes like,
    Statement stmt= con.createStatement();
    ResultSet rs= stmt.excuteQuery("select * from student_mst");
    where con is the instance of Connection interface.
    Therefore my question is that since all those(Connection,ResultSet,Statement) are interfaces then how the methods createStatement(),excuteQuery(String sql) are being executed where as those methods are only the declarations in those interfaces,nothing else.

    ok.......
    So can u tell me in this case, actualy which class is
    implenting the Statement interface such that
    "executeQuery(String)" is working properly.....??????Hello,
    First you need to understand interface and its working.. I will write one example code for u and I think this may hel how Connection, Statement and ResultSet work
    This is out main Class
    public class Main {
         public static void main(String[] args) {
              Connection con = DriverManager.getConnection();
              Statement stmt = con.createStatement();
              ResultSet rs = stmt.executeQuery("My Query");
              rs.getString();
    }Connection.java
    public interface Connection {
         public Statement createStatement();
    }Statement.java
    public interface Statement {
         ResultSet executeQuery(String sql);
    }ResultSet.java
    public interface ResultSet {
         public String getString();
    }DriverManager.java
    public class DriverManager {
         public static Connection getConnection()     {
              System.out.println("Calling getConnection()...");
              return new ConnectionImpl();
    }Following are the implementstions of the interfaces
    ConnectionImpl.java
    public class ConnectionImpl implements Connection {
         public Statement createStatement() {
              System.out.println("Calling createStatement()...");
              return new StatementImpl();
    }StatementImpl.java
    public class StatementImpl implements Statement{
         public ResultSet executeQuery(String sql) {
              System.out.println("Calling executeQuery()...");
              return new ResultSetImpl();
    }ResultSetImpl.java
    public class ResultSetImpl implements ResultSet{
         public String getString() {
              System.out.println("Calling getString()...");
              return "My String";
    }And the Output is
    Calling getConnection()...
    Calling createStatement()...
    Calling executeQuery()...
    Calling getString()...
    I suppose now you can understand how the Connection, Statement and ResultSet interfaces work

  • EDI - SAP -- Sterling Interfacing

    Hello ,
    I am currently working on IDoc - EDI 810 to sterling & EDI 850  to IDoc .
    I want to understand interfacing using IDOC - EDI 810 . 850 with Sterling & role of XI/PI in case needed .
    Can we send XML files to sterling  ?
    Also want to understand any thing sterling would need us to provide them
    Thanks.

    The only one i know of is True Commerce
    http://www.truecommerce.com/complete_solution/acct_pkg/sap.php
    not sure of what the price is.

  • Concept Of Interface

    Hi,
    As per my understanding interfaces have only method signatures but not code for the methods.And the class implementing the interface needs to define the method.
    Now my doubt is, we use registerOutParameter() method of interface CallableStatement directly.
    How's this possible.
    Thanks.

    Hi,
    First: Your code isn't correct regarding several points.
    See inline:
    Hi,
    Take This is code for example
    DB = new DBConnectionWrapper();What is this DBConnectionWrapper()? I don't know such an element of the standard APIs.
    conn = DB.getConnection();
    CallableStatement conn = DB.getConnection();A callable statement is not returned by getConnection(), but by prepareCall() as you are doing it below.
    strPrepareString = DB.getStrPrepareString("PRC",66);
    csmt = conn.prepareCall(strPrepareString);
    Here we have made an object of interface
    CallableStatement, then we are calling prepare call
    method.
    But as per my understanding the interface contains
    only method signature and no code.Then how this
    works.Coming to your question: It's quite simple - prepareCall() returns a complete object which implements (!) the Interface CallableStatement.
    All things we are executing is always in objects of some classes, but we are accessing the objects via an interface.
    From this point of view the class definition of a class not implementing a specific interface is an interface itsself.
    (I left out static methods in this description, because static methods are breaking the the concept of Interface based programming.)
    Hope this helps
    Martin

  • Could Someone Explain Interfaces in English Please?

    Hi Everyone-
    I'm new to Java. I've been reading "Learning Java" from O'Reilly. From what I understand, Interfaces are sort of a structural framework that lists certain methods. Any class that implements an interface must contain this "structure" and any objects intantiated using this class will be of the interface type? Is this correct? If not can someone explain interfaces in laymen's terms and the what is the usefulness of implementing Interfaces?
    Thanks.
    SA

    I'm new to Java. I've been reading "Learning Java"
    from O'Reilly. From what I understand, Interfaces are
    sort of a structural framework that lists certain
    methods. Any class that implements an interface must
    contain this "structure" ...OK so far...
    and any objects intantiated
    using this class will be of the interface type? Is
    this correct? It depends what you mean by "interface type". An interface can be said to define a type. So a class that implements that interface implements that type, and an object instantiated from that class is an instance of that type.
    If not can someone explain interfaces in
    laymen's terms and the what is the usefulness of
    implementing Interfaces?The utility of interfaces is that, without being tied down to particular implementations, allows types of objects to be defined.
    For example....is there a doctor in the house? Say:
    1) a Doctor is:
    - human
    - has gone to medical school
    - heals the sick
    2) a Norwegian is:
    - a person from Norway (assume we've defined "person" at this point)
    3) a Canadian is:
    - a person from Canada
    So:
    4) a Norwegian Doctor is a person from Norway who fulfills the qualifications to be a doctor
    5) a Canadian Doctor is a person from Canada who fulfills the qualifications to be a doctor
    6) a Canadian who is not a Canadian Doctor, is a person from Canada who does not fulfill the qualifications to be a doctor.
    If your appendix is about to burst, you need a doctor. You don't care if the doctor is Canadian or Norwegian (assume everybody including yourself speaks Esperonto so there aren't any language issues). You do care deeply if the person trying to remove your appendix is a Canadian but not a Canadian doctor.
    If you were establishing a hospital, you'd want doctors. If you were to write this in code, you'd have something like:
    Hospital.hireDoctor(Doctor doc);meaning, that method in the Hospital class will hire a doctor, which is the important part. So this would work:
    Doctor doc = NorwegianMedicalSchool.createDoctor();
    Doctor doc_eh = CanadianMedicalSchool.createDoctor();
    yourHospital.hireDoctor(doc);
    yourHospital.hireDoctor(doc_eh);The point is, you can write your Hospital code without knowing precisely how "Doctor" is implemented -- whether the doctors come from Canada or Norway. You just write it as appropriate given that you have a Doctor. And then when a Doctor is created by a Japanese medical school, it doesn't break your code. This is why interfaces are said to define "contracts" -- an interface defines what something is (i.e., what it does), and you can make your code insist on getting something that does that (fulfills the contract).
    Does this make any sense? Have I offended the Canadians? Probably.

Maybe you are looking for

  • Coloring a Pattern

    Hi, I just used a Basic Graphic_Dots pattern in a piece I'm working on. I can't for the life of me figure out how to color the polka dots? I would really like the dots to NOT be black.

  • Plant is not picking up in Purchase Order

    Hi all,     I am given a fresh system to create some transactional data for R & D purpose. i have created few materials at plant level.    while creating the purchase order the system is asking for the plant and when i give the plant the field is get

  • Latest HR Global Patch

    Hi, As far as i know, These are the steps to follow the HR Global Patches.. My version is 11.5.10.2...I think HRMS Family Pack will be 11i.HR_PF.H... I want to apply HRMS Legsliation patches for Australia (AU)... These are the steps which i want to f

  • PO's ceated in SRM

    Hi Gurus, Which table can I use to find PO's created in SRM. The reason I want the table its because I need the outline agreement and the contract item. Thanx for your help in advance. Edited by: Beatrice Nkomo on May 19, 2009 5:15 PM

  • How to configur the user to get notified when IDOC goes in to error?

    Hello Experts, Please tell me how to notify the users when IDOC goes in to error... please tell me where we can configure this thing,, Thanks Venkatesh