File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interfaceand class confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interfaceand class confusion" Watch "interfaceand class confusion" New topic

interfaceand class confusion

sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

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.

Thanks & Regards
Sumit Kothalikar
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

sumit kothalikar wrote:Is an interface is class or like a class ?
i mean to say can we say that interface I is a class I.

No, end of the day every one is human being, but still there are difference between man and woman
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446

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
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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.
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

thanks Prasad and Bob now i got why interface is not a class but it is a contract for the class
and it does not have constructors means it can't be extended by any class.
Himanshu Mall

Joined: Jun 28, 2010
Posts: 22

Start thinking in terms of "type" instead of "class and interface" and you ll see that all your confusion will just vanish.

P.S Both interface and class are TYPES.

Himanshu Mall
I agree. Here's the link:
subject: interfaceand class confusion
It's not a secret anymore!