This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface" Watch "Interface" New topic
Author

Interface

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello Ranchers,

What is use of interface in java in detail?



Thanks<br />Dinesh
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Please show some effort.

What have you read? What do you know? What doesn't make sense?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
The Sun Java Tutorial -as is so often the case with fundamental language questions- has answers: http://java.sun.com/docs/books/tutorial/java/IandI/index.html
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Effective Java (2nd Ed.) Chapter 4 may help you in this regard


http://muhammadkhojaye.blogspot.com/
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
I am asking when to use interface and when to abstract in java.
Devaka Cooray
ExamLab Creator
Sheriff

Joined: Jul 29, 2008
Posts: 3712
    
  89

Dinesh Tahiliani wrote:I am asking when to use interface and when to abstract in java.


Well you CAN use abstract classes instead of interfaces. But if you decided to leave *all* of the methods in that top-level class without defining it's body, you should use interfaces.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
didn't get you
Devaka Cooray
ExamLab Creator
Sheriff

Joined: Jul 29, 2008
Posts: 3712
    
  89

Dinesh Tahiliani wrote:didn't get you


I meant you should use abstract classes if you need to put some common business logic there. Using interfaces you can mandate it implementers to implement *all* of the methods defined in the interface. Please have a look at this JavaRanch wiki.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Dinesh Tahiliani wrote:I am asking when to use interface and when to abstract in java.




Use Interface when you want to add optional behaviour in class. Consider Comparable interface that allows a class to make sure that its instances are ordered with respect to other mutually comparable objects. Such interface is called a Mixin. You cannot define Abstract Classes as mixin.

The constant interface pattern is a poor use of interfaces

If you have default behavior, then you must use an abstract class Otherwise, use an interface

An abstract class gives you more flexibility when it comes to evolution. You can add new behavior without breaking clients.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface
 
It's not a secret anymore!