Solved

Interfaces. What, Why, Where?

Posted on 2001-09-14
6
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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