Solved

Interfaces. What, Why, Where?

Posted on 2001-09-14
6
190 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 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

16 Experts available now in Live!

Get 1:1 Help Now