![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
Aren't interfaces useless?
I tried, I really tried to find a meaning to interfaces, but I just can't =/
As with many things in OOP, interfaces are really only supposed to have a semantical meaning, like encapsulation with public, private and protected is pretty much purely semantical. Making all of your variables public, for example, won't change anything in the compiled program, but instead make it easier to shoot yourself in the foot during coding and compilation. It seems to me, that this concept of a "contract" between the interface and the class to implement certain functions is useless, as your program will never compile if it ever calls non-existant functions, anyway. What is the point of implementing interfaces, besides communicating (to a the programmer using the classes) that this class is sure to have that&that functions? He'll know anyway, won't he?
__________________
Visit my blog, about all that stuff in between your nostrils >_> |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
dunno interfaces in C# are the same in java, but.. i give it a try
interfaces enable other functions to call functions in whatever kind of class. A nice example is the KeyInterpreter. Every class can use it as interface, and the function to call the functions is sure they are actually there. |
|
|
|
|
|
#3 |
|
Programmer
|
Hm, could you provide an example, I didn't really get it
Doesn't matter if it's in java.
__________________
Visit my blog, about all that stuff in between your nostrils >_> |
|
|
|
|
|
#4 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
public class ExampleClass implements KeyListener {
public ExampleClass() { // constructor
addKeyListener(this); // add this class as keylistener. if we don't implement keylistener, you can't use this function.
}
public void keyTyped(KeyEvent e) { // when a key is typed. This function will get called through the KeyListener interface.
System.out.println("A key was typed!");
}
public void keyPressed(KeyEvent e) {
/* we leave this function empty. we have to declare it though,
* because the class has to implement all the functions of
* KeyListener.
*/
}
public void keyReleased(KeyEvent e) { /* same counts for this function */ }
}Didn't check it for errors, but it hope this will give you an idea ![]() |
|
|
|
|
|
|
#5 | |
|
Programmer
|
Quote:
You see, the only reason you used an interface in that code, was because it is required by the addKeyListener()-method you used. In truth, implementing the interface, wasn't really necessary.I can understand if it is somehow required by certain methods, then obviously you'd have to implement it, no question there. But have you ever defined an interface yourself and used it without having the feel that it is totally unecessary? Like it's just extra semantical bloat?
__________________
Visit my blog, about all that stuff in between your nostrils >_> |
|
|
|
|
|
|
#6 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
About creating interfaces myself.. I've never done it, because i didn't need it , and I never use java.. once programmed something in it, but i think the language sucks ![]() |
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
For example: interface Animal {
String noise();
}
class Dog implements Animal {
String noise() {
return "Woof";
}
}
class Cat implements Animal {
String noise() {
return "Meow";
}
}
class Test {
static void speak(Animal animal) {
System.out.println(animal.noise() + "!");
}
static void main(String[] args) {
Dog pooch = new Dog();
Cat percy = new Cat();
speak(pooch);
speak(percy);
}
} |
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4
![]() |
I might be totally wrong about this, so corroboration from someone who knows more would be great, bu I think interfaces in C# are just classes that you can't instantiate directly. They are used to provide the same methods to many different classes. If this is the case, I don't see what's so "useless" about them; I could imagine plenty of instances (pun semi-intended) where one might want to do this.
EDIT: Sorry, missed Arevos's post. I was, as it turns out, wrong. ![]() |
|
|
|
|
|
#9 |
|
Programmer
|
Ah, thank you! That's the first example where I've actually seen a genuine use for interfaces
![]() So the animal variable, by being of the type Animal, lets the compiler know that this object certainly has a noise method and thus doesn't return a compile-time error when you call that method. Very clever! You are the hero of the day in my little universe ![]() Polyphemus: Yeah, I've programmed some Java myself and I did like the syntax, but disliked the way stuff like event-handling was done and the tedious class upon class upon class code you sometimes need to use (like opening and writing files). IMO C# takes all the good from Java and makes it all come together more nicely (namespaces are a godsend for example).
__________________
Visit my blog, about all that stuff in between your nostrils >_> |
|
|
|
|
|
#10 |
|
Programmer
|
Ok, I redid your code in C# (well, it's a tiny bit different) with added comments
![]() using System;
namespace InterfaceTesting
{
// Our interface which describes the function Noise()
// By default this function is public and virtual
public interface IAnimal
{
void Noise();
}
// The Dog class implements the interface and thereby the method Noise()
public class Dog : IAnimal
{
public void Noise()
{
Console.WriteLine("Woof!");
}
}
// Same in the Cat class, though it differs slightly to differentiate it from Dog
public class Cat : IAnimal
{
public void Noise()
{
Console.WriteLine("Meow!");
}
}
// Here we test our two IAnimal classes
public class TestAnimal
{
public TestAnimal()
{
// Declare objects
Dog myDog = new Dog();
Cat myCat = new Cat();
// Use the Speak() method on both objects
// These objects both conform to the IAnimal interface
// and can such be treated as IAnimal objects
Speak(myDog);
Speak(myCat);
}
// This method takes an IAnimal type object for a parameter
// IAnimal isn't an instance of a specific class, but whatever objects implement that interface
public void Speak(IAnimal animal)
{
// So this doesn't produce an error
// Because animal implements the IAnimal interface and MUST have a Noise() method
animal.Noise();
}
public static void Main()
{
new TestAnimal();
}
}
}
__________________
Visit my blog, about all that stuff in between your nostrils >_> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|