Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interfaceand class confusion

 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@sumit
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
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@sumit

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic