Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 30th, 2005, 11:53 AM   #1
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
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 >_>
Simon Gray is offline   Reply With Quote
Old Aug 30th, 2005, 12:33 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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.
Polyphemus_ is offline   Reply With Quote
Old Aug 30th, 2005, 12:37 PM   #3
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
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 >_>
Simon Gray is offline   Reply With Quote
Old Aug 30th, 2005, 12:47 PM   #4
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Simon Gray
Hm, could you provide an example, I didn't really get it Doesn't matter if it's in java.
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
Polyphemus_ is offline   Reply With Quote
Old Aug 30th, 2005, 1:07 PM   #5
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
Quote:
Originally Posted by Polyphemus_
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
No, actually it didn't 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 >_>
Simon Gray is offline   Reply With Quote
Old Aug 30th, 2005, 1:10 PM   #6
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Simon Gray
No, actually it didn't 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?
I understand completely what you mean, I think more things of Java are totally unnecessary. I sometimes get the idea full OOP languages like Java want you to write loads of code when you can do the same with a few lines.
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
Polyphemus_ is offline   Reply With Quote
Old Aug 30th, 2005, 1:23 PM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Simon Gray
No, actually it didn't 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.
It's all to do with inheritance. Interfaces let the compiler know that an object has a particular method.

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);
  }
}
Without the "Animal" interface, you'd need two different 'speak' methods.
Arevos is offline   Reply With Quote
Old Aug 30th, 2005, 1:27 PM   #8
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4 Silvanus is on a distinguished road
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.
Silvanus is offline   Reply With Quote
Old Aug 30th, 2005, 2:12 PM   #9
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
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 >_>
Simon Gray is offline   Reply With Quote
Old Aug 30th, 2005, 2:33 PM   #10
Simon Gray
Programmer
 
Join Date: Aug 2005
Location: Ålsgårde, Denmark
Posts: 62
Rep Power: 4 Simon Gray is on a distinguished road
Send a message via MSN to Simon Gray
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 >_>
Simon Gray is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC