#1
  1. No Profile Picture
    <(>_~)>
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    315
    Rep Power
    13

    OOP: Interfaces - What's the Point?


    Hi.

    I'm just going over some course notes and I just finished reading "Interfaces and Abstract Classes" for the second time. The examples given are very trivial, so I'm still not really sure what the point of using interfaces is.

    The following is in Java...

    PHP Code:
    interface Emailable {
      
    void sendEmail(String msg);

    PHP Code:
    class Student implements Emailable {
      
    String name;
      
    //...
      
    public void sendEmail(String msg) {
        
    System.out.println("To: " name "@bla.com\n" msg);
      }

    PHP Code:
    class Printer implements Emailable {
      
    String name;
      
    //...
      
    public void sendEmail(String msg) {
        
    System.out.println("To: printer-" name "\n" msg);
      }

    OK, I have two different classes which have a common ability - to reveice some sort of email. Now, what's the benefit of having an interface for this over simply writing a normal sendEmail method for each? Is it basically there to force programmers to use common names for classes/methods/variables?

    o_O
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Is it basically there to force programmers to use common names for classes/methods/variables?
    That it is, among other things.

    But consider this - what if we had a class that takes an object and sends the email. We could have this class accept an Emailable class instead of a Student or a Printer class. For instance:
    Code:
    interface Emailable {
      void sendEmail(String msg);
    }
    class Student implements Emailable {
      String name;
      //...
      public void sendEmail(String msg) {
        System.out.println("To: " + name + "@bla.com\n" + msg);
      }
    }
    class Printer implements Emailable {
      String name;
      //...
      public void sendEmail(String msg) {
        System.out.println("To: printer-" + name + "\n" + msg);
      }
    }
    class MailThings {
      String defaultMsg = "this is the message we want to send out always";
      //...
      public void doTheEmailThing(Emailable mailable) {
        mailable.sendMail(defaultMsg);
      }
    }
    I can now design a class that accepts an Emailable type and ANY class that implements Emailable can be sent to it.
    Code:
    Student me = new Student();
    Printer dansOfficePrinter = new Printer();
    MailThings mail = new MailThings();
    mail.doTheEmailThing(me);
    mail.doTheEmailThing(dansOfficePrinter);
    You can send a Student or a Printer or ANY NEW CLASS YOU EVER MAKE. This is potentially very powerful. It makes code more reusable and makes maintanance easier. It is overkill for a few classes, but when you make an app with hundreds of classes all interacting, it can be a lifesaver.

    This is just one of the ramifications of implementing interfaces and polymorphism. You will learn more as you go along. Just be patient.
  4. #3
  5. No Profile Picture
    <(>_~)>
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    315
    Rep Power
    13
    I'm a bit confused...

    mail is a MailThings object, and it calls its doTheEmailThing method with me (a Student object) / dansOfficePrinter (Printer object) as a parameter. Now what? What does "mailable.sendMail(defaultmsg);" do?

    Or was that ment to be "mailable.sendEmail(defaultmsg);", which would automatically use the sendMail method appropriate for the parameter object?


    Edit: Err... or have you just assumed that we already have some sendMail method implemented somewhere?
    Last edited by CodE-E; March 29th, 2003 at 04:56 PM.
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    No, that was a typo. I was using the sendEmail method you create.
    Code:
    interface Emailable {
      void sendEmail(String msg);
    }
    class Student implements Emailable {
      String name;
      //...
      public void sendEmail(String msg) {
        System.out.println("To: " + name + "@bla.com\n" + msg);
      }
    }
    class Printer implements Emailable {
      String name;
      //...
      public void sendEmail(String msg) {
        System.out.println("To: printer-" + name + "\n" + msg);
      }
    }
    class MailThings {
      String defaultMsg = "this is the message we want to send out always";
      //...
      public void doTheEmailThing(Emailable mailable) {
        mailable.sendEmail(defaultMsg);
      }
    }
    -------------------
    Student me = new Student();
    Printer dansOfficePrinter = new Printer();
    MailThings mail = new MailThings();
    mail.doTheEmailThing(me);
    mail.doTheEmailThing(dansOfficePrinter);
    The point of the interface is - if something implements an interface, and you have a method that accepts that interface, then it does not matter what kind of object it is. The method will still work.
  8. #5
  9. No Profile Picture
    <(>_~)>
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    315
    Rep Power
    13
    Ok, cool, thanks!

IMN logo majestic logo threadwatch logo seochat tools logo