|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
PHP Code:
PHP Code:
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
|
|||
|
|||
|
Quote:
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); This is just one of the ramifications of implementing interfaces and polymorphism. You will learn more as you go along. Just be patient. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
Ok, cool, thanks!
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > OOP: Interfaces - What's the Point? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|