Hi friends, What interface and when we use it? Thanks, Elahe
Thanks,
Elahe
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
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
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.
For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
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
Joined: Dec 10, 2001
Posts: 7023
posted
0
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!