Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is an interface?

 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
What interface and when we use it?
Thanks,
Elahe
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces have been discussed a lot here at JavaRanch. You should use the search tool to find other threads about interfaces. Once you browse through those, please come back with some more questions.
Good luck!
Layne
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're familier with C++ then I could say that an Interface is a pure abstract class.
If you're not familier with C++ then let's look at it this way: An interface is a class that has method declarations, but no implementation (i.e. no code) inside any of those methods.
Let's say you have a Geography object. You also have objects called ZipCode, County, and State. Each of those 3 classes extend Geography.
Now, let's assume that you want to sort those objects. You would make each of your objects not only extend Geography, but also implement "java.lang.Comparable". java.lang.Comparable has one method that you must implement called public int compareTo(Object o)
Once you decide how each of these objects (ZipCode, State, County) will be sorted i.e. what does it mean for one object to be "greater than", "Less than", or "Equals To" another object, you will be able to sort any of them.
Now, what if you have a completly different object like "Employee". You may derive that class from completly different classes HOWEVER you may also want that class to run through your sort routine. You can implement java.lang.Comparable in that class as well.
You can pass an object by reference to an interface it implements just like you can pass an object by a reference to it's base class. This allows one method to work on lots of different types of objects as long as they all implement the same interface.
I hope that helps to clear things up for you. Let us know if you have more questions.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooo...after I posted that last message, I decided to go ahead and look around for you. Check this article out: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
It should explain interfaces in as much detail as you want. I haven't read it, but it's from the official Java web site. I think it is safe to assume they know what they are talking about. You can use the search tool at http://java.sun.com for more information about interfaces or any other questions you have about Java. In fact, they have many tutorials there to help you get started (or to keep going from what you already know).
HTH
Layne
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asma,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I update it and I hope it is fine now.
Thanks,
Asma
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic