Why do we need interfaces?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 22835 Views - Last Post: 31 March 2009 - 08:33 PM Rate Topic: -----

#1 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 116
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Why do we need interfaces?

Posted 18 February 2009 - 04:20 AM

I understand the idea of a C++ header file and implementing the methods in it. You need to have the header file to include in other files. But seeing as java classes don't need headers, why do they need interfaces? I've read that they are useful for packaging instance variables, but if they contained methods, and all of the classes that implemented them had the method definitions as well, would it matter if you included the interface at all?
interface Animal
{
void live();
void reproduce();
}

class Dog implements Animal
{
void live()
{
//definition
}

}



But seeing as promising to define the method when including an interface, when the method is mentioned anyway, is sort of redundant. If you didn't use the interface, there would be no difference in the class or its methods.

Could someone please help me understand this?
Thank you!!!

Is This A Good Question/Topic? 1

Replies To: Why do we need interfaces?

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Why do we need interfaces?

Posted 18 February 2009 - 04:36 AM

Implementing an interface allows a class to become more formal about the behavior it promises to provide also they form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

This post has been edited by mostyfriedman: 18 February 2009 - 04:37 AM

Was This Post Helpful? 1

#3 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 116
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Why do we need interfaces?

Posted 18 February 2009 - 04:43 AM

But if the methods appeared in the source code, wouldn't it not matter that they were also in the interface?
Also, aren't there some things where you have to implement empty interfaces like Clonable, to use use certain methods in the right ways?
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Why do we need interfaces?

Posted 18 February 2009 - 04:52 AM

yes, but the idea about interfaces is, it tells the user what kind of operations can be done on the objects of that certain class without having the user concerned about the implementation of these methods..for example if you open the java API and check out class String, you will find a list of methods..these methods are the interface of the class, and these are the operations that could be done on a String..yet the user doesnt know anything about the implementation of these methods
Was This Post Helpful? 1
  • +
  • -

#5 koki  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 02-January 09

Re: Why do we need interfaces?

Posted 18 February 2009 - 05:06 AM

the important of interface is to organize your code ... by collecting all related methods in one interface and then implement those methods in any class implement this interface.
it really useful if your class is very large and have a lot of methods.

the important of interface is to organize your code ... by collecting all related methods in one interface and then implement those methods in any class implement this interface.
it is really useful if your class is very large and have a lot of methods.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4879
  • View blog
  • Posts: 11,270
  • Joined: 16-October 07

Re: Why do we need interfaces?

Posted 18 February 2009 - 05:34 AM

Not sure if anyone's explicitly hit on it; you can implement many interfaces, but only extend one class. The one class thing drives C++ programmers nuts, but it's a very sane design decision and other OO languages have followed it.

This means I can have a class that's part of a complex hierarchy that can work with other objects without breaking that framework. As with most things in object oriented programming, it's does really make that much sense on a small scale. When you get into larger projects, the advantages become more clear.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2978
  • View blog
  • Posts: 19,031
  • Joined: 14-September 07

Re: Why do we need interfaces?

Posted 18 February 2009 - 07:51 AM

Like baavgai said, java doesn't support multiple inheritance (like C++). Thus, we "fake" it with multiple interfaces.
Was This Post Helpful? 0
  • +
  • -

#8 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 116
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Why do we need interfaces?

Posted 18 February 2009 - 09:36 AM

Ok, so the idea of interfaces is similar to C header files, with the same idea of naming methods without actually implementing them. Thanks guys!!
Was This Post Helpful? 0
  • +
  • -

#9 koki  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 02-January 09

Re: Why do we need interfaces?

Posted 18 February 2009 - 10:03 AM

i believe that we can implements more than one interface in java...
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4879
  • View blog
  • Posts: 11,270
  • Joined: 16-October 07

Re: Why do we need interfaces?

Posted 18 February 2009 - 11:29 AM

View Postcrazyjugglerdrummer, on 18 Feb, 2009 - 10:36 AM, said:

Ok, so the idea of interfaces is similar to C header files, with the same idea of naming methods without actually implementing them. Thanks guys!!


Not quite. An interface has no real analog in C, in C++ the closest thing is a pure virtual class.

Here's some more silly classes:
abstract class Animal {
	void live();
	void reproduce();
}

class Dog extends Animal {
	public void live() { /* */ }
	public void reproduce() { /* */ }
}

class Cat extends Animal {
	public void live() { /* */ }
	public void reproduce() { /* */ }
}

abstract class Virus {
	void infect();
	void reproduce();
}

abstract class Ebola {
	public void infect() { /* */}
	public void reproduce() { /* */}
}



Now, let pretend I want call an object's reproduce() method.

I can do this:
void breed(Animal item) { item.reproduce(); }
void breed(Virus item) { item.reproduce(); }
ArrayList <Animal> things;



Now, if these disparate classes have methods in common and I can use them to the same purpose, perhaps an interface makes sense.
interface IBreeder {
	void reproduce();
}

abstract class Animal implements IBreeder { //...
abstract class Virus implements IBreeder { //...
void breed(IBreeder item) { item.reproduce(); }



It does make more sense on larger scales. Take a look at common objects like streams or writers to get an idea.
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Why do we need interfaces?

Posted 18 February 2009 - 01:34 PM

Why do we NEED interfaces? Well we probably don't NEED them but they do come in ever so helpful.

At a very basic level they allow us to specify a features of a class. For example if a class implements Comparable than I know that it has the compareTo() method. I also know that this object is compatible with various methods that use the compareTo() method. For example if I make a List that is Comparable then I can use Collections.sort() to sort my list.

They also allow for abstraction. I may want to implement an algorithm that searches a string for a particular word. Now I COULD just have the argument be a String -- but since strings are an immutable class they are not always the best place to store textual data (especially while it is being processed) so if I step back and make take an argument of CharSequence then I now know that I can pass in Strings or CharBuffer, or StringBuffer -- So my method is now more abstract and more useful.

Interfaces let us decouple the implementation from the interface. This has solved a great number of problems dealing with versions. Allowing newer code to plugin to older code without having to recompile the whole project.

In Java you can only have one parent class -- but you can implement many interfaces. That means that I can create a class that is Compareable, Serializable, CharSequence, DataInput, And DataOutput. Meaning that I created a string that can be both written to (DataOutput) and read From (DataInput), I can make comparisons and I can serialize it (for persistance or to transmit over a communications channel). I can do all of this and have still derive the class from any base class I want.

Interfaces allow me to abstract objects so that I can have a collection of say Animals without careing what kind of Animals are in the collection.

Interfaces for the key to many design patterns.
Interfaces are key to most application frameworks.
Interfaces increase abstraction allowing us to make more and more generalized designs (and thus fancy application frameworks).

While you could probably go all day without writing an Interface -- they really are dead useful.
Was This Post Helpful? 1
  • +
  • -

#12 voidobscura  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 18-February 09

Re: Why do we need interfaces?

Posted 18 February 2009 - 07:42 PM

Interfaces are used mainly for organization - they also allow Java programs to be read easier, rather than be messy without clearly named methods and instance variables. Also, interfacing your class with an interface allows for a compile and runtime bond between the two. Some interfaces, like KeyListener and ActionListener are incredibly helpful to coders rather than writing all new methods and implementing them with no prior guidance.

--

thinksplode.com - the place to share ideas.

This post has been edited by voidobscura: 18 February 2009 - 07:44 PM

Was This Post Helpful? 1

#13 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 116
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Why do we need interfaces?

Posted 31 March 2009 - 05:29 PM

okay, NOW I think I get it :). It seems a little weird that they removed multiple inheritance from java. I admit that its very hard to get it to work correctly and should often be avoided, but it can be a powerful feature, and languages usually don't remove things that have the potential to be useful....But it seems like interfaces work fine as a substitute.

Thanks all!!
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8014
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Why do we need interfaces?

Posted 31 March 2009 - 05:35 PM

View Postcrazyjugglerdrummer, on 31 Mar, 2009 - 04:29 PM, said:

okay, NOW I think I get it :). It seems a little weird that they removed multiple inheritance from java.

No.. Sun Engineers realized that multiple inheritance was defeating OO design. and was a p in the a.. so they didn't remove it from Java
they just decided not to implement it at first design
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Why do we need interfaces?

Posted 31 March 2009 - 06:58 PM

View Postvoidobscura, on 18 Feb, 2009 - 09:42 PM, said:

Interfaces are used mainly for organization


Actually this is wrong. Interfaces are primarily used for abstraction -- separating implementation from interface. If you are just using them for "organization" than the understanding has not yet "clicked" for you. They CAN help in organization, but that is hardly even the tip of the iceberg.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2