Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

What is use of interface in java in detail?

 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show some effort.

What have you read? What do you know? What doesn't make sense?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Effective Java (2nd Ed.) Chapter 4 may help you in this regard
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am asking when to use interface and when to abstract in java.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4690
336
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
didn't get you
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4690
336
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic