This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface - Jargon

 
Mallesh Kundrappu
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
What is the need for Interface in Java. As per my knowledge with interface we can create method declaration and it can be used by any implementing class. Implemeted class will make definition of the declared methods. Why cant we use classes and extneds this class. We can over ride the method. I assume that we can implement so many intefaces not extend so many classes. Other than multiple inherittance do we have any other benifit in interface... Please clarify my doubt.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only derive from one concrete class. you can implement as many interfaces as you like.

I think most beginners only see the use of interfaces one way. What i mean is, they tend to see concrete classes, and therefore look for how how implementing an interface helps their class.

The real advantages to interfaces comes when you USE your classes. if i have an interface myInterface, and three classes that implement it, i write my code to deal with myInterface objects. I KNOW that all the specific opjects do what they are supposed to do.

But here's the neat part... two years later, somebody creates a NEW class that implements my interface.

i don't have to do a single thing, and my code still works when someone hands me one of these new objects.

someone else can change the implementation of the methods in the classes... and my code STILL WORKS.

THEY are free to change their classes implementation to their hearts content, and my code STILL WORKS.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred's description of polymorphism is exciting and powerful stuff. It's one of the things that sets OO apart. Be sure to take it all in.

As to why interfaces and not just abstract classes ... interfaces are rarely absolutely necessary. Plenty of languages get along without them, so it is apparently possible to design around them. The biggest advantage is that you can implement two or more interfaces where you could not extend two or more abstract classes.

I like to use the most abstract thing that will do the job at any moment. Since we do have interfaces in Java, and they qualify as more abstract than abstract classes, I'll make an interface instead of an abstract class when it works. That's not a hard rule, just a default choice that usually serves well.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
As to why interfaces and not just abstract classes ... interfaces are rarely absolutely necessary. Plenty of languages get along without them, so it is apparently possible to design around them. The biggest advantage is that you can implement two or more interfaces where you could not extend two or more abstract classes.


The languages I know which don't have interfaces (or similar concepts) actually don't need them because they

a) allow multiple inheritance for classes (like C++), or

b) don't need an inheritance relationship for polymorphism to work at all (like Smalltalk, Ruby)
[ March 03, 2007: Message edited by: Ilja Preuss ]
 
marten kay
Ranch Hand
Posts: 178
Java jQuery Postgres Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am an absolute beginner and not clear on interfaces. I hope someone can follow my incorrect logic and point me where I am going wrong

i. an interface is a class with abstract methods, that is, an interface has no implementation within the class, all methods declared in an interface are implemented in the subclasses. Correct?

ii. a class can only extend one other (super) class, but implement many interfaces. As interfaces don't have any implementation, the class needs to implement all the methods defined in the interfaces it implements. Correct?

iii. but when, say in HF java, you implement the ActionListener interface, the ActionListener interface clearly contains some implementation. This to me seems like multiple inherentence? what is wrong here?

iv. Similarly, I look at the Java API documentation, and AbastractMap implements the Map interface, but the Map interface seems to implement methods, this again looks like multiple inherentence.

Where am I going wrong?

help would be much appreciated.

Marten
[ April 05, 2007: Message edited by: marten kay ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47288
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i: An interface is not quite a class. It is like a class with only abstract methods. Yes.

ii: Yes, a class can implement several interfaces, and (unless it is declared abstract) must implement every method from all its interfaces.
Exception: If there are two methods with the same signature in two interfaces it only implements one method.

iii: I haven't read HF Java, so I am not sure what they are saying. Do they give an example with new "ActionListener()" in? You will usually find the methods (in the case of ActionListener there is only one called actionPerformed) have been implemented. You call something by the name of an interface, and you implement its methods. Something with implemented methods isn't an interface, it's a class, so you have changed the interface into an "anonymous inner class."

iv: When you read an interface description in the API, it tells you what the methods are suppposed to do. But you know that methods are all empty in an interface. Go and explore the Java folder on your PC, until you find a file called src.zip. Unzip it. Find Map, by following java->util. Open the Map file with a text editor, and you will see all the methods look like this
public interface Map<K,V> {
// Query Operations

/**
* Returns the number of key-value mappings in this map. If the
* map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of key-value mappings in this map
*/
int size();

/**
* Returns <tt>true</tt> if this map contains no key-value mappings.
*
* @return <tt>true</tt> if this map contains no key-value mappings
*/
boolean isEmpty();
Now compare that with the API.

Look here in the Java Tutorial. You will see that interfaces "are not part of the class hierarchy," and they permit multiple inheritance.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello to all,

First and important thing in interface is "Interface gives property to our class who implements it. It doesn't give behaviour the class who is implemting it."

Explaination -- see the following code.

Interface Sonso{
public void a();
public void b();
}

class Thisnthis implements Sonso{

public void a(){
// here goes your code
}
}
What is the problem in above code? can you guess it...
Problem: here Thisnthis class gives defination to the method a() only. but it is totally unaware of method b().

So the thing is that Interface tells the class Thisnthis that if you are implementing me then you should have to take property that is method b().

class Thisnthis defines method b() like this..

Interface Sonso{
public void a();
public void b();
}

class Thisnthis implements Sonso{

public void a(){
// here goes your code
}
public void b(){
// 1.
// this code is optional
// if you put code in this method it means it gives behaviour

// 2.
// if you wont put any code here , it means interface gives
// property named method b() to your current class

// 3.
// in simple terms if method b() doesn't content any code
// means its (method b()) is not giving any behaviour to your
//current class.
}
}
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the best use of interface generally is declareing Constants which all the application uses it.
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Maybe I'm feeding a troll, but...)

Originally posted by Rahul Shilpakar:
the best use of interface generally is declareing Constants which all the application uses it.


Er, no. Many people would say that's about the worst use for an interface.

It is certainly valid to include constants in an interface, where those constants relate to the concepts and methods defined in that interface.

It is legal, but maybe a bit questionable OO design, to have an interface that only defines constants and nothing else. Generally, constants should be part of some proper class or interface, along with the methods likely to use them.

It is legal, but very bad design, to say that a class implements an interface that only defines constants. First, the "is a" relationship implied by implementing an interface is rendered meaningless. Second, there's great potential for name clashes.
[ April 13, 2007: Message edited by: Peter Chase ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic