Solved

Interfaces. What, Why, Where?

Posted on 2001-09-14
6
180 Views
Last Modified: 2010-03-31
Hi,

I have been coding in Java for a while now, but I am interested why Interface classes are used? Could you name an example of using Interfaces.  This is NOT  school project, but every project that I have programmed, I have never created an Interface class.  I am just wondering if it is either my poor identification of classes or that I don't actually need to use them!

Thanks in advance.

Regards,

David.
0
Comment
Question by:david_d
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6484165
But you've used them so that should give you some understanding of there use.
One their main strength is that they allow you to seperate function, from implementation. The interface defines the function or contract but leaves the details of how it is actually implemented to the implementing class. Allowing you to potentially use different implementations without impacting on your application.

They also provide a means to implement multiple inheritance (try implementing multiple inheritance without using interfaces).

0
 
LVL 3

Expert Comment

by:black
ID: 6484180
0
 

Expert Comment

by:inmomi
ID: 6484595
Hi David,

   This explanation may be useful. Interfaces can be used to abstract ur implementation from outside world. They are powerful when u start working on distributed computing technologies like RMI and EJB where ur business logic is hidden from the client side who gets a home object or remote object and makes method calls to get the functionality done.

Regards

Inmomi
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Accepted Solution

by:
k.jones earned 80 total points
ID: 6485177
interfaces -- when to use

There are two important differences between interfaces and abstract classes.

1. Interfaces provide a form of multiple inheritance, because you can implement multiple interfaces.  A class can extend only one other class, even if that class has only abstract methods.

2. An abstract class can have a partial implmentation, protected parts, static methods, and so on, whereas interfaces are limited to public constants and methods with no implementation.

These differences usually direct the choice of which is best to use in a particular implementation.  If multiple inheritance is important or even useful, interfaces are used.

Any major class you expect to be extended, whether abstract or not, should be an implementation of an iterface.  Although this approach requires a little more work on your part, it enables a whole category of use that is otherwise precluded.  For example, suppose we had created an Attributed class instead of an Attributed interface with an AttributedImpl implementation class.  Programmers who wanted to create new classes that extended other existing classes could never use Attributed, since you can extend only one class.  Because Attributed is an interface, programmers have a choice: they can extend AttributedImpl directly and avoid forwarding, or, if they cannot extend, they can at least use forwarding to implement the interface.  You can even provide multiple possible implementations of the interface to prospective users.

Cheers,
Ken Jones
0
 
LVL 1

Author Comment

by:david_d
ID: 6485520
I am sorry, but your proposed answer is does not explain what I am looking for.  It does not describe in enough detail about Interfaces.

The comment k.jones has provided, describes Interfaces in a clear and understandable manner.

Sorry.

David.
0
 
LVL 1

Author Comment

by:david_d
ID: 6485522
Thank you for your well descriptive comment/answer.

Thanks.

David.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now