Possible Duplicate:
Why are interfaces preferred to abstract classes?
Abstract class and Interface class?
Please give a real world example for the use of Interfaces. Also, what are the differences between an Abstract Class and an Interface. I mean, what are the benefits of Interfaces over abstract class, because Abstract Classes can also be used for the same purpose as the Interfaces. Then why Interfaces are so important?
Thanks for your comments guys, but still I didn’t get answer for my question. I have asked for a “REAL WORLD EXAMPLE” of java interfaces. In real world, like in our daily life, where we use java interfaces. I think i am clear this time.
In java, the two concepts abstract class and interface sounds similar.
But,
(1)
In interface you can only declare the members, you can’t define anything inside the interface
In abstract class can have both definition and declaration.
(2)
Remember “A class can extend only one class where as it can implement more than one interface”
You can’t extend more than one class in java where as you can implement any number of interfaces. This is one of the advantage of Interfaces over the Abstract Classes.
(3)
A class which extends the abstract class should define all the members from the abstract class. If you don’t want to define all of them you need to declare the sub class also as abstract class.
Where as in Interfaces, if you implement an interface you can use what ever members you want from that interface, so need of declaring the class as abstract or any thing else. This is another advantage of using Interfaces rather than abstract class.