interface & implements


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: interface & implements

  1. #1
    Join Date
    Mar 2007
    Posts
    7

    interface & implements

    Hi
    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 ?

    Thank you

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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 07:02 AM.

Similar Threads

  1. Replies: 2
    Last Post: 03-27-2002, 10:05 PM
  2. Replies: 6
    Last Post: 03-27-2002, 08:13 PM
  3. Problem with struct that implements interface
    By Michael Welch in forum .NET
    Replies: 5
    Last Post: 03-14-2002, 02:43 PM
  4. Replies: 0
    Last Post: 12-15-2000, 02:58 PM
  5. Interface leaks in C#?
    By Kevin Burton in forum .NET
    Replies: 12
    Last Post: 10-09-2000, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center