• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which is more appropriate way to code in this case: to use abstract classes or interfaces?  RSS feed

 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is more appropriate way to code in this case: to use abstract classes or interfaces? Case 1 or Case 2?

CASE 1:

Class Calculation
{
abstract void calculate();

}

Class YearlyCalculation extends Calculation

{
void calculate()
{
// do something
}

}

Class MonthlyCalculation extends Calculation

{

void calculate()
{
// do something
}

}



OR


CASE 2:


Interface Calculation
{
void calculate();

}

Class YearlyCalculation implements Calculation

{
void calculate()
{
// do something
}

}

Class MonthlyCalculation implements Calculation

{

void calculate()
{
// do something
}

}

thanks
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (←click) when you post code.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is more appropriate way to code in this case: to use abstract classes or interfaces? Case 1 or Case 2?

CASE 1:







OR


CASE 2:







thanks
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based only on what you've given, there's really not much difference. The decision to use interfaces or abstract classes really depends on the larger context of the particular business domain in question and the overall design approach.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based only on what you've given, there's really not much difference


Thanks...

Abstract classes and Interfaces are different things then in that case how is it ok to use either abstract class or interface in this case?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:Abstract classes and Interfaces are different things then in that case how is it ok to use either abstract class or interface in this case?

I don't know much about what "this case" means. You did not give any more details than some skeleton interface and class definitions.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this precise case, with the (limited) info given, then Calculation is an interface, as there is no code there at all.
No point "wasting" the single point of inheritance for a class on an abstract class that has no code.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My rule of thumb is to use interfaces over inheritance whenever possible. As Dave said, you only get to inherit from one class, so you want to "save" that for cases when you must use it. And if that "must use" case never happens, so much the better - the class can implement as many interfaces as it needs to.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all..I understood it...also I read that Interface is more appropriate when you are creating API kind of functionality.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read Oracle's guidelines about when to use interfaces and abstract classes and I agree with Ulf.

I have realized that the inheritance ( I don't consider Interface implementation an inheritance thing ) is not very common in the production designs. I'd use inheritance only if there is a very obvious inheritance relationship and if that relationship is powerful enough to define the base character of all the classes that are in that hierarchy. I mean a lot of times we don't see patterns like an Abstract Shape and a non abstract Rectangle or say an Exception and an IOException. A lot of times what we need do is add something that can be named as <Something> with a suffix 'able'. So if your code is adding that sort of a functionality to your implementations, I think it should be an interface. But if the inheritance does come naturally to you for the design, I think it could be a parent class - abstract or not. A lot of times ( from what I have seen based on my very limited experience ) the state and behavior you require to add does not define your class as a whole. I think in these cases, we can use composition and/or delegation.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I said is what I do and use as my guideline and it has helped me get my designs pass the review stage.

I'd love to hear more on this, or corrections, if I am wrong, or suggestions if I there are better guidelines ( there obviously have to be ) to consider.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!