How does an interface work?

I would like to understand how an Interface should be implemented and why. Take for example the Enumeration interface. If I implement it in my class, then I must fill the two empty methods hasMoreElements() and nextElement() with my implementation. What is the practical advantage in this? Could someone give an example where they implement an Enumeration interface?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Using interfaces is good when you know which methods you'd like to perform on an object, but you don't care how those methods are implemented.

blairdyeAuthor Commented:
But if I use an interface i DO need to care about the implementation because simply implementing an interface means i have to implement the methods and these methods are empty until i fill them with functional code. Whats wrong with my logic?
If you use an interface than, yes, you do need to have a class which implements that interface.
if you want to have a class with some methods implemented and some which are not implemented than you can use the abstract-class model.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

blairdyeAuthor Commented:
So an abstract class is in this sense far more useful than an interface because it offers more flexiblility. Where would i use an interface instead of an abstract class? Only when my implementation is not yet known? If so, then i cant see any practical advantage in it, only that it allows me to have a better oversight of my structure.
A Java interface is a collection of abstract behavior that can be mixed into any class to add to that class behavior that is not supplied by its superclasses. Specifically, a Java interface contains nothing but abstract method definitions and constants-no instance variables and no method implementations.
Interfaces are implemented and used throughout the Java class library whenever a behavior is expected to be implemented by a
number of disparate classes. The Java class hierarchy, for example, defines and uses the interfaces java.lang.Runnable,
java.util.Enumeration, java.util.Observable, java.awt.image.ImageConsumer, and
An interface is made up of a set of method signatures with no implementations, making it the embodiment of pure design. By
mixing an interface in with your class, you're encompassing that design into your implementation. That design can then be safely
included anywhere in the class hierarchy because there are no class-specific details of how an interface behaves-nothing to
override, nothing to keep track of, just the name and arguments for a method
After your class implements an interface, subclasses of your class will inherit those new methods (and can override or overload
them) just as if your superclass had actually defined them. If your class inherits from a superclass that implements a given
interface, you don't have to include the implements keyword in your own class definition.
Even a pure abstract class with only abstract methods isn't as powerful as an interface. An abstract class is simply another
class; it inherits from some other class and has its place in the hierarchy. Abstract classes cannot be shared across different
parts of the class hierarchy the way interfaces can, nor can they be mixed into other classes that need their behavior. To attain
the sort of flexibility of shared behavior across the class hierarchy, you need an interface.
many more features are there.....

package  collections;
       public class  LinkedList {
             private Node  root;
             . . .
             public Enumeration  enumerate() {
                 return new LinkedListEnumerator(root);
      class  Node {
          private Object  contents;
          private Node    next;
          . . .
         public  Object  contents() {
              return contents;
          public  Node    next() {
              return next;
      class  LinkedListEnumerator implements Enumeration {
          private Node  currentNode;
           LinkedListEnumerator(Node  root) {
              currentNode = root;
          public boolean  hasMoreElements() {
              return currentNode != null;
          public Object   nextElement() {
             Object  anObject = currentNode.contents();
              currentNode =;
             return  anObject;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.