• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract class vs interface  RSS feed

 
Raghuveer Rawat
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone who have idea when we should use abstract class and when interface.
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Tutorial mentions both. It should be easy to figure out the appropriate use of each.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this thread for a "world of nature" example of interface vs. abstract class.
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are two general guidelines that might help:
First: An interface cannot contain any implementation whatsoever. (Thus, all methods are abstract.) An abstract class can contain as much implementation as you desire. (For example, an asbstract class can contain one or more fully implemented methods.) Therefore, if you want to supply a partial implementation, use an abstract class. Otherwise, use an interface.
Second: A class can implement multiple interfaces. It can only inherit one abstract class. Thus, if you want a class to be able to implement two or more "interfaces", they must be interfaces, not abstract classes.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I'm wrong. To me, interface in java is about as close as java gets with multiple inheritance, remember, java doesn't allow multiple inheritance. However, interface allows you to do that in a sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!