Interface classes

can somebody tell me why interface classes are useful.. right now i'm just not convinced, because, like an abstract class, interfaces only contain method headers that are to be used in derived classes. Why write another class to store the method headers when you still need to code the header again except this time there's code? Instead, can't u just write that same method in another class without implementing an interface in the first place, isn't that less coding?
I'm just confused about abstract and interface classes, and don't know when to use them.
Can someone explain the concepts...

Firstly interfaces. Look at the Java event model. If a programmer wants to register interest in someone pressing a JButton, he registers his interest with the button through the method JButton.addActionListener(ActionListener l). The parameter here is of type ActionListener which is an interface. This achieves two things. Firstly the programmer can make his whole class implement ActionListener and fill in the method. Alternatively he could make it using an anonymous inner class. Secondly, it is an issue of security - within the JButton whatever object is sent to the addActionListener method, the JButton can only "see" the ActionListener part of it - ie. the JButton can only call the actionPerformed() method.
Let us look at another use of Interfaces - the java Collection API. Apache have various implementations of such interfaces as Map, Set, List that differ in some way from HashMap, HashSet, Hashtable, ArrayList that Java provides. Usually the difference is performance-related, such as a Map that is used for fast-lookups in a multi-threaded environment but assumes that writes are done seldomly (org.apache.commons.collections.FastHashMap being a case in point). Suppose you have written your code everywhere thus.
public void someMethod() {
  HashMap m = new HashMap();
  callMethod( m );
private void callMethod(HashMap m) {
  //do something here
}And then when you come to profile your application, you find that it is too slow and would be benefitted by using FastHashMap - you will have to go through every single place you have written HashMap in your code and change it. However, if you had originally done...
public void someMethod() {
  Map m = new HashMap();
  callMethod( m );
private void callMethod(Map m) {
  //do something here
}You won't - you will only need to change where the HashMap was Constructed. Going a bit further, had you used a Factory object to return you the new Map implementation, then wouldn't have to look through the code at all - you could merely go straight to the factory Object where all Construction is done and change it there.
A further use of interfaces is to define an API for vendors to implement. One example of this is JDBC. The interfaces (as with the Collections API) provide a contract of behaviour.
Abstract class are slightly different - suppose you were coding an application for a Company - you might have classes representing Employee, Manager, Customer etc etc. These will have some properties that are shared (ie. all will have contact telephone numbers, not all will have salary, or bonus etc. So you might have the hierarchy
Person (abstract : attributes address, telephone, email)
   |
   |--> Employee (attributes salary, employeeNumber)
   |          |
   |          |--> Manager (attributes bonusPayment, staff[])
   |
   |--> Customer (attributes accountNumber)You would make the Person class abstract as it makes no sense to be able to instantiate a Person, only concrete sub-classes. Its subclasses can inherit its attributes (if you so wish) and also functionality (you could define a public void sendEmail(String message) on Person to be inherited by its subclasses), again if you so wish.
I hope this helps.

Similar Messages

  • How do  I use a variable from an Interface class?

    Right now I have three classes. First class is called Game and it extends my second class call Parent and also implements my third class call Source. Source is an interface class. I have a variable that I want to use in my Source class named Checker. Now, How do I go among using that variable from my Game class? What should the code look like?
    ex.
    public class Game extends Parent implements Source
    need help badly....

    ok, what I forgot to tell you guys is that my variable
    in my interface class is a boolean type(true or
    false). It is set to true now. But I want it to change
    to false when a user triggers a button in the Game
    class. How do I do this? You don't because you can't. If you have a varaible declared in an interface it must be static and final. It cannot, therefore, be changed. Better head back to the drawing board.

  • How do i hide a column in table interface class

    Hi,
    I need to hide a column in Abap table interface class. When i tried to hide from web item properties it didn't work so i am thinking to hide first column in the table interface class
    Thanks,
    mala

    In the WebApplication Designer start Tools -> View Definition -> Based on a Query. Choose your query, change layout to your requirements and save the view. Now just use the view in your webtemplate.
    Pls assign points if answer is useful
    <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/u/37289">vitaliy</a>

  • Understanding the use of interface class?

    Hello,
    This question referes to java servlets, but the questions is about java design.
    My wrox java server pages text, includes an example where some user data is posted to servlet.
    To process the data posted and store this, they created a interface class and another class AdminManager which implements the interface.
    In the servlet they create an object from the interface class, and call the AdminManager methods through the interface object.
    In the book this was the only example which used a interface. And I wondered if this design approach was often used, or should an interface normally be used if several classes would implement the interface.
    Thanks

    It depends. You might want to create an interface even if you only have a single implementation of that interface in mind, because you want to make it easy to create more implemenations later.
    Also keep in mind that not all implementations of an interface are strictly for production use. For example, you might want to define an interface, define an implementation of that interface for production use, but then also define a "mock" version of that interface for testing, prototyping, etc.
    Generally I feel that if you have a sufficiently complex set of types and relationships between those types, it makes a lot of sense to express those relationships purely as interfaces, on a high level. Then provide implemenations as a separate step in the programming process.

  • Bean problem: No interface classes found

    Hi ;
    I try to create a adapter . according to tutorial "How To Create Modules for the J2EE Adapter Engine"
    https://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/f013e82c-e56e-2910-c3ae-c602a67b918e&overridelayout=true
    Although I did all steps in it , I copied sda files and extract them  then I imported them to the project but I get this error in Netweaver Studio "Bean problem: No interface classes found" while creating ejb file.
    the document says you must close and reopen project when you come accross this error but it doesnt work?
    Is there any one solve this error?
    ps: I use NWDS 7.0.19 , I copied aii_af_lib.sda, aii_af_svc.sda and aii_af_cpa_svc.sda files from PI 7.01

    Hi Tuncer!
    Did you download and "import" these JAR files into your NWDS Build Path  - classpath variables:
    com.sap.aii.af.lib.mod.jar     <bin>/ext/com.sap.aii.af.lib/lib
    sap.comtcloggingjavaimpl.jar     <bin>/system
    com.sap.aii.af.svc_api.jar     <bin>/services/com.sap.aii.af.svc/lib
    com.sap.aii.af.cpa.svc_api.jar     <bin>/services/com.sap.aii.af.cpa.svc/lib
    com.sap.aii.af.ms.ifc_api.jar     <bin>/interfaces/com.sap.aii.af.ms.ifc/lib
    <bin> stands for: /usr/sap/<SID>/<instance-id>/j2ee/cluster/bin in a PI 7.1 system.
    Windows -> Preferences -> Build Path -> Classpath Variables -> New ...
    Choose a name of your choice and then select the JAR files mentioned above.
    This should resolve your problem.
    Regards,
    Volker

  • RE: Hide a column in web report using table interface class

    Hi,
    I want to hide first column in web template using table interface class. Following is the code i used in CAPTION_CELL and CHARACTERISTIC_CELL. Is this correct?
    method CAPTION_CELL.
    *First column
    if i_x = 1.
    add comment tag
    move '<!-- ' to C_CELL_CONTENT.
    endif.
    endmethod.
    method CHARACTERISTIC_CELL
    First column
    if i_x = 1.
    save start-time column
    move I_CHAVL_EXT to L_STARTTIME.
    add comment tag
    move '<!-- ' to C_CELL_CONTENT.
    endif.
    endmethod.
    When i execute the web template it is still displaying the first column. Do i have to code in any other method?
    Thank you,
    Mala Venkatesh

    Hi , the implementation should look like...
    method CAPTION_CELL .
    *CALL METHOD SUPER->CAPTION_CELL
    EXPORTING
    I_X =
    I_Y =
    I_IS_EMPTY =
    I_IOBJNM_ROW =
    I_ATTRINM_ROW =
    I_TEXT_ROW =
    I_IOBJNM_COLUMN =
    I_ATTRINM_COLUMN =
    I_TEXT_COLUMN =
    I_IS_REPETITION =
    I_COLSPAN =
    I_ROWSPAN =
    CHANGING
    C_CELL_ID =
    C_CELL_CONTENT =
    C_CELL_STYLE =
    C_CELL_TD_EXTEND =
    First column
    if i_x = 1.
    add comment tag
    move '<!-- ' to C_CELL_CONTENT.
    endif.
    Second column
    if i_x = 2.
    close comment tag
    concatenate '--> '
    C_CELL_CONTENT
    into C_CELL_CONTENT.
    endif.
    endmethod.
    method CHARACTERISTIC_CELL .
    *CALL METHOD SUPER->CHARACTERISTIC_CELL
    EXPORTING
    I_X =
    I_Y =
    I_IOBJNM =
    I_AXIS =
    I_CHAVL_EXT =
    I_CHAVL =
    I_NODE_IOBJNM =
    I_TEXT =
    I_HRY_ACTIVE =
    I_DRILLSTATE =
    I_DISPLAY_LEVEL =
    I_USE_TEXT =
    I_IS_SUM =
    I_IS_REPETITION =
    I_FIRST_CELL = RS_C_FALSE
    I_LAST_CELL = RS_C_FALSE
    I_CELLSPAN =
    I_CELLSPAN_ORT =
    CHANGING
    C_CELL_ID =
    C_CELL_CONTENT =
    C_CELL_STYLE =
    C_CELL_TD_EXTEND =
    First column
    if i_x = 1.
    save document-item number
    move I_CHAVL_EXT to l_docitem.
    add comment tag
    move '<!-- ' to C_CELL_CONTENT.
    endif.
    Second column
    if i_x = 2.
    close comment tag
    concatenate '--> '
    C_CELL_CONTENT
    l_docitem
    into C_CELL_CONTENT
    separated by space.
    endif.
    endmethod.
    Activate the methods/class and add this in the Web Template!
    for example:
    <param name="MODIFY_CLASS" value="ZHCOLAPP">
    ZHCOLAPP is the table interface class in this case.
    Best,
    Michael

  • Syntax error on keyword "void"; "interface", "class" expected

    When migrating my application for Visual-Age to Web-Sphere 5.1
    The following error occurred:
    Syntax error on keyword "void"; "interface", "class" expected
    for the code:
    * ejbLoad method comment
    * @exception javax.ejb.EJBException The exception description.
    public void ejbLoad() throws javax.ejb.EJBException{
         _initLinks();
    }Thanking you in an advance

    I had missed a closing breacket '}'

  • Default Table Interface Class - 0TPL_BAP_MASTER

    Hi there
    We are trying to determine which table interface class is used on the default 0TPL_BAP_MASTER web template.
    We have created our own web template, but one of the pieces of functionality delivered with the standard template is the sort (ascending/descending) functionality which is included as arrows on the table column headings. We would very much like to use this on our custom template, and believe it to be implemented in the table interface class, but are not sure what that class is.
    Anybody know the name of the class?
    Cheers,
    Andrew

    Hi Deepu,
         How are you?
    I got a error about 0TPL_BAP_MASTER, when we are exicuting webtemplates in my portal. Can you please tell me what may be the error and how to overcome with this error.
    Thanks,
    Surendra.

  • Abstract Classes & Interface Classes

    Dear members of the Sun Community
    My studies are progressing and just 1 period ago we started doing Object-Orinted Programming in Java and I must say I'm quite fond of it. It's become quite clear that OOP is an important aspect of Programming and just can't be missed. We've learned about Inheritance, Polymorfism, Mutators, Inspectors, Uses-Relationships and everything else however now I've come to the point where I got a problem:
    Up until now we have been using normal classes to work with in which you could create objects and in your main program create objects from that class however. We've just learned about Abstract and Interface classes. As far as I'm concerned I'm quite confused with both of them.
    If I am not mistaken (please correct me if I'm wrong) Abstract classes are classes from which you cannot create an object but is only used to make a subclass inherit everything from this superclass.
    I am not quite sure what Interface classes are as they just plain confuse me. Would anyone be so kind to maybe explain what all of this is ?
    Thanks a whole bunch
    Herazio

    Funny enough that already solved the question !
    Thank you so much for the quick reply ^^
    Herazio

  • Who can tell the difference of the abstract interface class?

    someone ask me that he had a abstarct interface class, such as :
    public abstract interface Book{
    I was comfused, who can tell me why use it?

    http://java.sun.com/docs/books/tutorial/java/interpack/interfaceDef.html
    Scroll down to the bottom.
    Drake

  • Interface class

    what is interface class and what is the use of interface class.
    Why should we define the interface class in our program.
    Give me any example.
    Thanx.

    what is interface class and what is the use of
    interface class.
    Why should we define the interface class in our
    program.
    Give me any example.
    I'm glad you asked. See this thread for an excellent example of the Interface class:
    http://forum.java.sun.com/thread.jspa?threadID=730816

  • EJB deployment problem --New remote interface class files are not loaded

    Hi, I face a problem while deploying an ear file in oc4j. I basically get some problems like the method in the remote interface is not implemented in the EJB class. But the implementation is actually available in the EJB class.
    We get this problem when we actually change the signature of the EJB remote methods. The new methods are not referenced by oc4j. Only the old version of remote interface is referenced by oc4j. But the latest version of EJB class is taken. I am using 9.0.3 version of the oc4j. Could you pls help me regarding this problem ?
    Regards
    Solomon

    I have found the solution :-)
    You must add to the ejbCreate() method of the bean class throws javax.ejb.CreateException

  • Interface/class hierarchy and serialization?

    Hi,
    I have an interface, base class that implements this interface and subclasses derived from base class that also implement this interface.
    I also have a method with this interface as an argument. My question is: how to do serialization of this argument in this method without using instanceof operator? Should interface extend Serializable and/or each class/subclass implement Serializable? Is it sufficient to just have common interface extend Serializable without classes/subclasses implementing it?
    thanks,
    sale

    How should I serialize for example if the argument in the method is subclass XYZ? Do I need to use instanceof operator?

  • Implemenet/Extend interface/class in default package

    Filename: DeaultInterface.java
    public interface DefaultInterface {
        // Abstract Methods...
    Filename: ConcreteClass.java
    package com.company;
    import DefaultInterface;
    public class ConcreteClass implements DefaultInterface {
        // Implementation of Abstract Methods...
    }When I compile above java code it gives error "cannot find symbol. symbol: class DefaultInterface".
    Can anyone explain why can't we implement the interface/extend the class in default package(no package)?
    Edited by: 974531 on Dec 2, 2012 11:07 PM

    >
    When I compile above java code it gives error "cannot find symbol. symbol: class DefaultInterface".
    Can anyone explain why can't we implement the interface/extend the class in default package(no package)?
    >
    You CAN implement the interface defined in the default package.
    What you CANNOT do is import it. And that is because the Java Language Specification specifies that named types can only be referenced by their simple name if they are imported and must otherwise be referenced by their fully qualified name.
    See the 7.5 Import Declarations in the Java Language Specification - http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html
    >
    An import declaration allows a named type or a static member to be referred to by a simple name (§6.2) that consists of a single identifier.
    Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name (§6.7).
    A type in an unnamed package (§7.4.2) has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported, and (b) static members of types in an unnamed package cannot be imported. As such, §7.5.1, §7.5.2, §7.5.3, and §7.5.4 all require a compile-time error on any attempt to import a type (or static member thereof) in an unnamed package.
    >
    So you can't reference that type by importing it because you can't import a type that doesn't have a canonical nmae. And you can't just use the simple name because, as the first statement above says you have to import it to use the simple name.
    When you use the simple name of that type without an import the simple name would refer to a class IN your 'com.company' named package.

  • Interface class Initialization

    A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
    1..T is a class and an instance of T is created.
    2..T is a class and a static method declared by T is invoked.
    3..A static field declared by T is assigned.
    4..A static field declared by T is used and the field is not a constant variable (?4.12.4).
    5..T is a top-level class, and an assert statement (?14.10) lexically nested within T is executed.
    Above is from JLS.
    public class Demo {
         static int y;
         int x=instanceMethod();
         int instanceMethod()
              System.out.println("instance method");
              return 5;
         public static void main(String[] args)
              Demo.y=15;
              Demo.staticMethod();
         static void staticMethod()
              System.out.println("static method");
    }In this case the output is static method
    This contradicts points 2 and 3, since in this program static method is invoked and a static field is assigned , yet instanceMethod is not called, i.e not initialized

    Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class. Initialization of an interface consists of executing the initializers for fields (constants) declared there.
    Your class has no static initializer nor class variable initializer...
    Therefore I wouldn't say your example illustrates the statement you mentioned.

Maybe you are looking for

  • Tiger an Final Cut Pro HD

    I upgraded to Tiger last night and now Final Cut Pro HD does not recognize my Canopus Converter nor my mini DV deck. Both FireWire. Need help fast! Need to start on a project.

  • Exception condition "PROFILE_NOT_INITIALIZED" raised., error key: RFC_ERROR

    Dear EP Expert, We are trying to implement ECC 6 with EP 7. When i try to browse a standard webdypro iView provide by the ess business package i.e (record working time). The system return such error. Exception condition "PROFILE_NOT_INITIALIZED" rais

  • Mouse Over values disappear

    Hi, the Mouse Over values for our Xcelsius 2008 sp2 charts disappear.  We do have the feature checked for all charts. The workaround is to make a different combo box selection and the mouse over value seems to start working again. Is anyone else expe

  • IPad mini mail app display rotation

    My iPad mini mail app rotation is locked.  No other app or display is affected.  How do I unlock?  How did it get locked?

  • How i achive this data by writing exact query ?

    Dear Exparts, Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod PL/SQL Release 10.2.0.3.0 - Production "CORE     10.2.0.3.0     Production" TNS for 32-bit Windows: Version 10.2.0.3.0 - Production NLSRTL Version 10.2.0.3.0 - ProductionI