aspose file tools*
The moose likes Beginning Java and the fly likes Use of Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use of Interface" Watch "Use of Interface" New topic
Author

Use of Interface

Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
I have been studying Interfaces and I have been trying to understand the use of Interfaces in software design. I already understand that using polymorphism Interfaces can help because using the reference of an object of an Interface one can call an object of a subclass that has implemented the interface.

But the question I have is that in an Interface all methods are abstract and have no implementation. If a class implements this Interface then it is agreeing to implement all the methods of the interface. But what is the advantage of doing this? Sure we can implement multiple interfaces, but all the interfaces have methods that have no implementation (dummy methods). Normally if we extend from a super class we can make use of its methods and other members. That is useful. In an interface the members is no implementation. So what's the use of inheriting from an interface and restricting yourself to the implementations of the methods of the Interface?

In the process of thinking about this I think I came to an answer. Please correct me if my thinking or answer is incorrect. I think that this restriction of having to implement all the abstract methods of an interface in a class that is implementing the interface is the OO power of the Interface. Let me illustract. Working with Event Listeners I questioned why a text fields addActionListener method called the actionPerformed method of the ActionListener interface. Then I realized that whatever behind the scene implementation of how addActionListener method calls actionPerformed method is, the key is that the designers of event handling relied on the fact that the interface ActionListener will always have actionPerformed method. So knowing this they can have addActionListener method call a series of other methods in possibly other classes or interfaces, but eventually the actionPerformed method will be called.

So even though interfaces have methods with no implementation, they are like patterns or links that always are there.

Sorry for the long description of how I think Interfaces are useful. Any feedback on this would be appreciated.
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 150
Hi JavaJohny JavaJohny,
2 things in first place: 1) Welcome to the forum! 2) I am NOT a moderator but for sure you will be asked to change your display name to follow the naming convention of this community (you must put your real name/lastname).

To answer your question, I think your understanding of interfaces is correct. There are some advantages from using interfaces, one of them is that you can create arrays of DIFFERENT objects that implement the SAME interface (this is very powerful!), another positive aspect of using interfaces is that you are OBLIGED to IMPLEMENT every method declared in the interface in any concrete class (unless you declare the class abstract): this is very useful to garantee for example that every object that implement the interface will have at least the same functionalities. For example, you have the interface Car with the following methods: getPrice, setPrice, getMaxSpeed, setMaxSpeed, start, stop, isThereEnoughFuel etc...then you create different objects like SportModelXXX, CustomModelYYY etc...in your program you WANT/NEED every concrete implementation of car have AT LEAST the functionalities declared in the interface, this way, you can treat every car regardless of the model itself, just because they all implement the interface Car, this is obviously true only if you are treating these car models for their being cars! I mean you can start, stop, check if there is enough fuel regardless of the model type, just because they are all cars! Additionally, you CANNOT instantiate an object of type Car (Car is an interface!) because it wouldn't make any sense to operate on a generic Car, but every concrete car will have the same functionalities.

I hope I have been clear!
Let me know if this is has been useful.
Giovanni
[ April 29, 2005: Message edited by: Giovanni De Stefano ]

SCJP 1.4
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
All of what you said is right on the mark. If you are interested in some exaples, you should look at the standard Java API. (In fact, you should spend some time becoming familiar with the classes and interfaces that are already available for you anyway. Book mark this documentation since you should refer to it often when you program in Java.) In particular, take a look at Collections framework. You should also be able to find the source code for the standard API classes with your Java installation. Since you mentioned ActionListener and the releated mechanism, you should take a look at the JButton source code (or maybe one of its parent classes). With a little bit of a search, you will find somewhere that it calls actionPeformed() just like you guessed.

One reason that Collections uses interfaces is because there are several classes that behave very similarly. For instance, Vector, LinkedList, and ArrayList all implement the List interface. If you write code to use the List interface, it is easy to switch between LinkedList and ArrayList. In my opinion, this is the most powerful use of interfaces. You can have multiple classes that implement the same interface. This allows you to switch them in and out at will with very minor changes to the original code.


Java API Documentation
The Java Tutorial
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Giovanni De Stefano:
I am NOT a moderator but for sure you will be asked to change your display name to follow the naming convention of this community (you must put your real name/lastname).


Giovanni is right - please adjust your display name in your profile. Details can be found here.

Thanks!


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
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You got it. Interfaces are often called software contracts. If your class implements an interface, you are implicitely agreeing to fulfill all of the responsibilities of that interface. This allows any code that references the interface to rely on the behavior defined by it.

Almost every abstract class I create is backed by an interface. Actually, that wording is a bit misleading. In reality, the interface is the primary element and stands alone. The abstract class serves merely as a starting point for anyone implementing the interface, assuming they don't need to extend some other class.

Java makes use of this technique in the form of the various adapter classes. The assumption with adapters is that while you must implement all methods for a listener, most of them will probably be empty. The adapter saves you the trouble of writing a bunch of empty methods.

I still wish Java had the ability to specify a default implementation for some or all of an interfaces methods. If an implementing class doesn't provide a body, the default body would be copied into the class as if it had been in the class's source file. This would obviate the need for adapters without introducing multiple inheritance issues.

I've always felt that one of the key attributes of a really good developer is laziness. This feature would keep interfaces from penalizing that trait.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

I still wish Java had the ability to specify a default implementation for some or all of an interfaces methods. If an implementing class doesn't provide a body, the default body would be copied into the class as if it had been in the class's source file. This would obviate the need for adapters without introducing multiple inheritance issues.


ummm...in that case, will you not be putting abstract classes into oblivion. If interfaces can have some default implementation for its mthoeds, they are as good as abstract classes plus using interfaces, we can 'implement' more than one interface. In that scenario, why would anyone use abstract classes anymore? And what would be the difference between abstract classes and interfaces?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of Interface