Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 29th, 2003, 08:12 AM
CodE-E CodE-E is offline
<(>_~)>
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 315 CodE-E User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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

Reply With Quote
  #2  
Old March 29th, 2003, 12:45 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Quote:
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.

Reply With Quote
  #3  
Old March 29th, 2003, 04:53 PM
CodE-E CodE-E is offline
<(>_~)>
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 315 CodE-E User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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.

Reply With Quote
  #4  
Old March 29th, 2003, 06:48 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
  #5  
Old March 30th, 2003, 06:45 AM
CodE-E CodE-E is offline
<(>_~)>
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 315 CodE-E User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Ok, cool, thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > OOP: Interfaces - What's the Point?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway