Solved

Interfaces. What, Why, Where?

Posted on 2001-09-14
6
199 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 117
simple java question 3 56
eclipse shortcuts 9 57
ejb on wildfly 5 20
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

831 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