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.

Similar Messages

  • What is the use of Generic class in java

    hi everyone,
    i want to know that
    what is the use of Generic class in java ?
    regards,
    dhruvang

    Simplistically...
    A method is a block of code that makes some Objects in the block of code abstract (those abstract Objects are the parameters of the method). This allows us to reuse the method passing in different Objects (arguments) each time.
    In a similar way, Generics allows us to take a Class and make some of the types in the class abstract. (These types are the type parameters of the class). This allows us to reuse the class, passing in different types each time we use it.
    We write type parameters (when we declare) and type arguments (when we use) inside < >.
    For example the List class has a Type Parameter which makes the type of the things in the list become abstract.
    A List<String> is a list of Strings, it has a method "void add(String)" and a method "String get(int)".
    A List<File> is a list of Files, it has a method "void add(File)" and a method "File get(int)".
    List is just one class (interface actually but don't worry about that), but we can specify different type arguments which means the methods use this abstract type rather than a fixed concrete type in their declarations.
    Why?
    You spend a little more effort describing your types (List<String> instead of just List), and as a benefit, you, and anyone else who reads your code, and the compiler (which also reads your code) know more accurately the types of things. Because more detail is known, the compiler is able to tell you when you screw up (as opposed to finding out at runtime). And people understand your code better.
    Once you get used to them, its a bit like the difference between black and white TV, and colour TV. When you see code that doesn't specify the type parameters, you just get the feeling that you are missing out on something. When I see an API with List as a return type or argument type, I think "List of what?". When I see List<String>, I know much more about that parameter or return type.
    Bruce

  • The use of interface in abap object

    hi,
    what is the use of interface in class? can have a simple example with explanation? i have read from help but not quite understand its purpose.
    thanks

    Hi El,
    Interfaces are pure abstract classes. The interface methods will have only definitions but no implementations.
    The classes which will implement interfaces has to provide the implementation to the methods.
    Interfaces will provide the common definition and the classes which implements these interfaces will provide them different implementation to the methods as per their requirements. This achieves Polymorphism of Object Orientation.
    REPORT zbc404_hf_events_3 .
    INTERFACE lif_employee.
      METHODS:
        add_employee
           IMPORTING im_no   TYPE i
                     im_name TYPE string
                     im_wage TYPE i.
    ENDINTERFACE.
    CLASS lcl_company_employees DEFINITION.
      PUBLIC SECTION.
        INTERFACES lif_employee.
        TYPES:
          BEGIN OF t_employee,
            no  TYPE i,
            name TYPE string,
            wage TYPE i,
         END OF t_employee.
        METHODS:
          constructor,
         add_employee      "Removed
            IMPORTING im_no   TYPE i
                      im_name TYPE string
                      im_wage TYPE i,
          display_employee_list,
          display_no_of_employees.
      PRIVATE SECTION.
        CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,
                    no_of_employees TYPE i.
    ENDCLASS.
    CLASS lcl_company_employees IMPLEMENTATION.
      METHOD constructor.
        no_of_employees = no_of_employees + 1.
      ENDMETHOD.
      METHOD lif_employee~add_employee.
      Adds a new employee to the list of employees
        DATA: l_employee TYPE t_employee.
        l_employee-no = im_no.
        l_employee-name = im_name.
        l_employee-wage = im_wage.
        APPEND l_employee TO i_employee_list.
      ENDMETHOD.
      METHOD display_employee_list.
      Displays all employees and there wage
        DATA: l_employee TYPE t_employee.
        WRITE: / 'List of Employees'.
        LOOP AT i_employee_list INTO l_employee.
          WRITE: / l_employee-no, l_employee-name, l_employee-wage.
        ENDLOOP.
      ENDMETHOD.
      METHOD display_no_of_employees.
      Displays total number of employees
        SKIP 3.
        WRITE: / 'Total number of employees:', no_of_employees.
      ENDMETHOD.
    ENDCLASS.
    Check this link for some more examples on OO ABAP
    http://www.erpgenie.com/sap/abap/OO/eg1.htm
    Thanks,
    Vinay

  • 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

  • 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

  • Can you help me understand the use of the word POSITION in TR and CFM?

    Hi,
    I am trying to have a view of typical BI reports in TR and TM/CFM so through my research I came to the following link:.
    http://help.sap.com/saphelp_nw2004s/helpdata/en/62/08193c38f98e1ce10000000a11405a/frameset.htm
    My problem on this link and other postings on this site seem to be the same. Can you help me understand the use of the word POSITIONS in these context:
    1. Our client has asked for financial transaction reports in BW, such as position of Borrowings, Investments and Hedge Operations (TM data).
    2. I have a requirement on, some reports related to Money Market (Fixed Term Deposits, Deposits at Notice) something on FSCM-Treasury and Risk Manager. These reports will be similar to that of Loans, i.e. Position statement, flow statement, etc.
    3. The set of position values for a single position or a limited amount of positions can be reported by transactions TPM12 and TPM13 in R3.
    4. 0CFM_C10 (Financial Positions Cube)
    Do you have some simple report outputs to help clarify how the word POSITION is used in such environments?
    Thanks
    Edited by: AmandaBaah on Feb 15, 2010 4:39 PM

    If I future buy 10 shares in company at £1 per share - at the end of the day my potential value is £10
    The next day the shares drop tp £0.9 per share - I have a negative position - my shares are only worth £9
    I haven;t bought them yet - but I have a negative position - ie if things stayed as they are - I am going to realise (ie end up with)  a loss
    Now you can use this for loans and foreign exchange banks as well...

  • Why class builder allows to develop abstract final class ? What is the use of such class in ABAP?

    I am new to ABAP. I tried creating abstract class and found that class builder allows development of abstract final class. What is the use of such class in ABAP?

    Hi,
    Does not compile:
    This one do:
    Inheritance:
    Regards.

  • What is the use of valuation class in Finished Material Master?

    Dear Expert
    What is the use of valuation class in Finished Material Master?
    Thanks of Advance

    hi,
    this is to inform you that,
    generally 7920 - will be the valuation class for the finished goods.
    Allows the stock values of materials of the same material type to be posted to different G/L accounts & vice versa.
    this is an integration field which identifies the G/L accounts & goods movement types.
    balajia

  • What is the use of interface in the adobe forms

    Hi friends,
    can any one tell,
    what is the use of interface in the adobe forms for desigining the form.

    The main purpose of the form interface is to send the application data to the form.
    The form interface is created separately from the form, which consists of the form context and layout. When you create a form, you must assign it to a form interface.
    please have a look at the link below for more info:
    http://help.sap.com/saphelp_nw70/helpdata/EN/96/6ee0d5b39640d68fc0078fc575114a/frameset.htm
    http://help.sap.com/saphelp_nw70/helpdata/EN/f2/21021b911f4c0cae11459a4ce0bc62/frameset.htm
    hope this helps,
    harman

  • What is the use of Inner class.?

    hello everyone.........
    I want toknow that what is the use of Inner class...?

    Judging from what's seen around here? Mainly obfuscation.

  • What is the use of CVI_BDT_ADAPTER class ?

    Hi Freinds,
    what is the use of CVI_BDT_ADAPTER class ? can any one explain ?
    thnx n regards
    Vijaya

    Hi,
    This class is used for following cases:
    1.modify fields depending on customer status
    2.modify fields depending on vendor status
    3.to check user has the authorization
    to view/change/create data in the specified sales area

  • What is the Use of Inner classes in Interface.

    Hi All,
    Most of us we know that We can define inner classes in the interface. Like
    public interface MyItf{
         Demo d = new Demo();     
         class Demo{
              Demo(){
              //some additional code here
    }Now I have following question in my mind:
    1. An Interface is pure abstract. Then why inner classes inside the interface?
    2. In what scenario, we can utilize these inner classes of interface?
    Plz Share your views on this...
    Thks for ur replies in advance.

    This we cando in defining Demo Class outside.That's no argument. You could write the programs in other languages, so why use Java? Just because you can use a top-level class instead, it's no argument against using an inner class. You also can make all attributes public... you don't o that either (I hope).
    Ok Also
    tell me how to pass an Object in inner class Demo. to
    the method of Interface.
    public abstract TheInterface.Demo doSomething(TheInterface.Demo d);
    Can u give some real time situation where this
    concept can be used.There are only very, very few. Just because it's possible, it doesn't mean it needs to be done or is done often.

  • Whats the use of interface

    I know interface is use for multiple inheritence but is there anyohter reason instead of this?

    Hi,
    The power that interface gives you is not in the implementation but in the design. One should always have a blueprint specially when you are trying to make code reusable. If the blueprint is right then you can keep the signatures clean and separate from the implementation. The interfaces publish how a class or a set of classes would behave and hence declares the functionality rather than stressing on the implementation.
    So you can treat interfaces as a contract that the classes that implement teh interface are bound to provide. So you get hold of interfaces you know what all you can do when you call methods which implement them. Interfaces are mostly shared when you would like to integrate components and for that matter even putting together some bigger functionality from small set of classes.
    Interfaces also define some common set of behaviour that the classes that implement the interface are bound to provide. It captures the similarities across unrelated classes without forcing a class relationship
    Hope this helps
    Aviroop

  • In business object what is the use of Interface

    hi ppl,
             In each business object there is a interface what can we do with that. When i used wizards like dynamic parallel processing it asked for wizards what is the actual use of interfaces.

    Hi Dheepak,
    I have an overview on interfaces. Someone pls correct me if i am wrong here.
    Interfaces are generally used to group a set of attributes and methods that can be used across different business objects. You can create your own interfaces in SWO1 and when you include these interfaces in business objects you get all these attributes and methods in the business objects (Inheritance). Its basically for re-usability of the code(Reusability). Apart from that if required you can implement your own code for the methods thus maintaining the same name with a different logic for your BO (Polymorphism).
    For example assume that you have a "Display" method and a few attributes in an interface. The "Display" method is used to display a particular transaction. Now you can include this interface in any BO and all these attributes and methods will be available in that BO with their respective implementation code. Now if you want the method to form a different action in any particular BO you can go ahead and redefine this code in that BO. This redefinition will not affect other BO's that had implemeted this same interface. All other BOs which had implemented this interface will continue to function as per the implementation code defined in the interface. This makes sure that the same method works in different ways in different BO's (Polymorphism)
    Supertype<-->Subtype delegation will allow us to inherit the supertype/subtype methods/attributes but it does not allow us to change the underlying code in individual BOs. Hence polymorphism cant be acheived here
    Thanks,
    Prasath N
    BO - Business Object

  • I don't understand the design of inner class private member

    This is a question about the java language specification of inner classes.
    In the java langage specification document, we read
    If the member or constructor is declared private,
    then access is permitted if and only if it occurs
    within the body of the top level class (�7.6)
    that encloses the declaration of the member.
    This allows following code :
      public class PrivateTest {
        public PrivateTest()
          Hello hello = new Hello();
          System.out.println(hello.secret);
        class Hello
           private String secret = "This is a secret";
      } wherein accessing the private secret field is allowed
    from into the PrivateTest enclosing class.
    My questions are :
    a) It seems that private methods or constructors of
    inner classes have no meaning, we could also declare
    them as public. True or false ?
    b) Is there any reason that Java bypass this private
    mechanism ?
    c) Why is the above definition not written with
    "first enclosing" instead of "top level" ?
    Thanks in advance

    Private methods and constructors of an inner class can only be accessed within the outer class. Other classes can't instantiate it or use the private methods.
    You can also make your inner class private, so it is not possible to refer to the class from outside (and thereby another way of preventing it from being instantiated).
    So it does matter which access modifiers you use.
    I think top level is more precise than first enclosing, because you can have inner classes in inner classes, which are still available for the top level class (haven't tested this).

Maybe you are looking for

  • How do I format links? And where can I find easy help info?

    How do I override the blue color added when I add a hyperlink? (I found the modify page properties/links drop-down to remove the underline, but I can't figure out how to default to the original text color and only show link during rollover, or add my

  • IMessage sender issues

    I share my apple account between several devices. For some reason lately when I use iMessage it is changing the user name or the sender name from the user that it should be (me). Anyone have any ideas how to fix this so it shows as the correct sender

  • Using t3s with applets

    Hi - We have a few applets accessing EJB's on WebLogic 5.1 (SP 6) which utilize "t3" as the protocol and don't have any problem getting Contexts and issuing requests. However, once we switch to t3s, we get an error indicating that the server can't be

  • Submit as PDF

    Hello, I read some posts on the forum, regarding the "Submit as PDF" which said that, for this option to work, either the form need to be Rights Extended or the end-user should use Acrobat. I just want to make sure whether there are any updates to th

  • How can I replace my iphone?

    I got my iphone 5 in December 26 so it's still under the 1 year warranty. My power button stopped working and I'm thinking of getting my phone replaced. I'm just wondering how will they re connect it to my server provider? And will it cost extra?