This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Interface - Jargon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface - Jargon" Watch "Interface - Jargon" New topic
Author

Interface - Jargon

Mallesh Kundrappu
Greenhorn

Joined: Mar 02, 2007
Posts: 2
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

Joined: Oct 02, 2003
Posts: 11250
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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 ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
marten kay
Ranch Hand

Joined: Feb 03, 2007
Posts: 178

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 ]

when in doubt put it in parenthesis and stick a dollar sign in front of it, only good can come from this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
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

Joined: Aug 29, 2006
Posts: 132
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.
}
}


Perform for today. Adapt for tomorrow.
Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
the best use of interface generally is declareing Constants which all the application uses it.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
(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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
wood burning stoves
 
subject: Interface - Jargon