File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces

 
Smitha Prasad
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the use of having interfaces when we can dirtectly implement the required method in a class?
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Smitha Prasad
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx a lot dirk and ibrahim
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just an extra resource for you to learn about inrefaces:
Interfaces Tutorial
Corey
 
John Lee
Ranch Hand
Posts: 2545
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the reasons is for maintanence purpose.
 
Neil Laurance
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic