Link to home
Start Free TrialLog in
Avatar of blairdye
blairdye

asked on

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
Avatar of shlomoy
shlomoy

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.

Avatar of blairdye

ASKER

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of shaveri
shaveri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial