• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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?
thanks
Blair
0
blairdye
Asked:
blairdye
1 Solution
 
shlomoyCommented:
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.

0
 
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?
0
 
shlomoyCommented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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.
0
 
shaveriCommented:
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
java.awt.image.ImageProducer.
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 = currentNode.next();
             return  anObject;
         }
      }

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now