Is an interface is class or like a class ? i mean to say can we say that interface I is a class I.
I am asking this question because i saw Comparator
interface listed in All classes category in java doc.
may it is silly question but i am really confused about it.
well, they are separate keywords so, in my mind, a class is a class and an interface is an interface.
The primary purpose of an interface is to establish a "contract" or an "agreement". The interface specifes the methods that an implementing class must implement. But it doesn't (make that can't) specify implementation details. The interface can specify the method name(s), the parameters required, and the return type..... nothing else. It is up to the implementing class to provide the implementation of the method.
The real benefit of using interfaces is lower coupling through the use of polymorphism. Once a class implements an interface.... a reference variable of the interface type can hold a reference to any object whose class implemented that interface. As long as the implementing class satisfies all of the methods specified by the interface and the client object only references method types known to the interface you can plug any implementer of the interface into the reference and it will work.
SCJP - 86% - June 11, 2009
I think your question is about the abstract class and interface
you once told me that you have confusion regarding abstract class and interface
and you had said that interface is a class but this is not true
but consider following points
1. An abstract class is always a class not interface
2. interface is not a class at all it is a contract to be fulfilled by some class
3. we cannot instantiate abstract class but is still has a constructor and every abstract class extends Object class
4. Now the interfaces do not have constructors and they do not belong to the inheritance tree of the class
Bob gave a nice explanation about the benefits of the interfaces
Happy preparation Sumit
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Joined: Jun 04, 2007
Excellent post Prasad. With your info I would like to add a few more points that may help.
If you are careful, an abstract class can be made to do the same thing AS an interface. It can contain a whole list of abstract classes that ALSO define a "contract" just like an interface. If you give them public access, it will be very similar to an interface. What, then, would be a difference?
An abstract class can do one thing that an interface can NOT do. It can define methods WITH an implementation defined. An interface con not do that. The reason you might want to do this is to define default methods (behaviors) that an extending class does not HAVE to implement but, rather,it can just use the default implementation.
So an abstract class can be designed to do what an interface does BUT
1) an interface does what it does naturally by the way it is designed but ONLY allows one to express methods without implementations (abstract methods that MUST be public)
2) an abstract class can be made to act like an interface BUT will allow you to define default behaviors.