interface & implements
I went through http://java.sun.com/docs/books/tutor...interface.html
to know about interface and implements...
but couldn't get much of it.....
I never have seen the keywords implements or interface used so far in any java codes.....
where they are used ? will you please kindly explain a bit in more detail ?
The best way to communicate the idea to you is based upon your prior understanding of the possible hierarchy of classes.
Used in defining a hierarchy of classes, an Interface is an "abstract class" which has a set of fields and/or methods, one or more of which is not defined in the Interface. You therefore cannot create instances of an Interface ... directly. You must create one or more classes which "implement" the Interface. To "implement" the Interface, you must define those methods which are declared but not defined in the Interface. If you have done this properly, the class which implements the Interface can be used as if it were an implementation/object of the Interface. Comparable, Comparator, Iterable, Iterator, Collection, Map, and thousands of others in the JDK.
Since we do not have multiple inheritance of classes in Java, the Interface hierarchy permits us to pick and choose the inclusion of common elements or method signatures we need for the class we are designing without having a single parent which implements most of them.
Some Interfaces are just "markers" - they do not have one or more abstract method, but if you implement the Interface then the runtime knows that you have written your implementing class to be "of that type". Cloneable is one such "marker" - telling the runtime that your class can be worked on by the Object.clone() method.
Just as if you were writing a class which is in an inheritance relationship with its parent, the class which implements an Interface can be addressed as an instance of the Interface, and you can fulfill a call to (one of) the Interface's method(s) by calling that object's implementation of the Interface's method(s). An object of a class which implements the Map interface is a Map, and any of the common methods of Map will be called by calling the object's implementation of that common method. Your calling code can call on of the Map's methods (let's say, the containsKey() method) and allows you the flexibility to write your own implementation of that method for your particular purposes, or modify or even replace that implementing class, without the calling code needing to be modified or having to know anything about your particular implementing class other than the fact that it will send back the needed action/information when the call to the interface method is sent.
Last edited by nspils; 11-20-2007 at 08:02 AM.
By Michael Welch in forum .NET
Last Post: 03-27-2002, 11:05 PM
By Trey Hutcheson in forum .NET
Last Post: 03-27-2002, 09:13 PM
By Michael Welch in forum .NET
Last Post: 03-14-2002, 03:43 PM
By Vlad Ivanov in forum .NET
Last Post: 12-15-2000, 03:58 PM
By Kevin Burton in forum .NET
Last Post: 10-09-2000, 11:29 AM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center