This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interfaces" Watch "Interfaces" New topic


Smitha Prasad
Ranch Hand

Joined: Apr 02, 2002
Posts: 41
What is the use of having interfaces when we can dirtectly implement the required method in a class?
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
Because Java doesn't support Multiple inheritance a class can not extends more than one class.
To support some form of it Java has "interfaces". Interfaces don't have any data memebers only method definitions. So lets say that I created an interface that included the method draw. I would know that all classes that implements a class would have the draw method. So I can treat those Objects generically.
Hope this helps.

SCJP 1.4<br /><a href="" target="_blank" rel="nofollow"></a>
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Ibrahim Hashimi:
Interfaces don't have any data memebers only method definitions.

Interfaces can have "data members".

Note that the data member of the interface is (implicitly) final - you can't change it (go ahead and try - I dare ya).
As a general tip for finding lots of great information, try a search on the JavaRanch forums for topics that might have been discussed previously. One is sure to find lots of useful information. The search page link is at the top right of this page.
Good Luck.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Smitha Prasad
Ranch Hand

Joined: Apr 02, 2002
Posts: 41
thanx a lot dirk and ibrahim
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Just an extra resource for you to learn about inrefaces:
Interfaces Tutorial

SCJP Tipline, etc.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
One of the reasons is for maintanence purpose.
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
A good example of the benefits of Interfaces is employed by the utility classes. An example.

The map reference variable is declared as a Map, but points to a specific implementation: HashMap. Recall that the keys in a HashMap are not returned in any order. Now suppose we want to impose some order on the keys in our Map. All we have to do is change the first line to:

Since we declared map as a Map, the other lines of code do not break, and we now have a Map which returns its keys in a specific order.
[In actual fact, this example works if the keys we add to the Map implement Comparable. Otherwise we would need to supply an implementation of the Comparator interface in the TreeMap constructor.]
Hope this helps
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I agree. Here's the link:
subject: Interfaces
Similar Threads
"Top Ten Topics that Everyone Thinks are on the SCJP Exam, but Aren't "- Bert Bates
interface extends interface ?
Bridge Design Pattern...
Does every interface extend class Object?
EJB 3 Client View