Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Just JAVA book - Interface vs class

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, peter, when should we apply interface ? should it replace class ? thank you !
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
peter, do you mind to brief the chapter 11 - interface in your just JAVA book ? thank you !
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alvin chew:
hi, peter, when should we apply interface ? should it replace class ? thank you !


We should apply interface, when we need multiple inheritance... They work differently from classes, so they should not replace class... Since Java does not allow multiple inheritance from classes, interfaces can mimic multiple inheritance...

Just my 2 cents..
 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

When deciding to use interface,I think we should be correct at the first time.


If we need to add any methods to this interface in future it should be overridden in all classes implementing this interface,which can be over head.

I feel abstract class will be a suitable choice than interface in situations like this.

Chandrasekhar S
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is also interesting
http://www.coderanch.com/t/99122/patterns/Should-there-ever-abstract-class
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you should be used Interface when you want create blueprint model of project. (Interface is like Abstract class But it's diferent.Interface VS Abstract Class)

Example in BookStore system you want to create MainSystem1, 2, 3, 4 all is business process for BookStore and each system can access to another system.

So

If you want to separate each system and hide implementation of each system, you should create Interface to define all method/behavior to access in each system.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic