• Post Reply Bookmark Topic Watch Topic
  • New Topic

Diff between Abstract Class and Interface  RSS feed

 
Manoj Chinta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can anyone tell me diff between Abstract class and interface
why Java introduced Abstract classes and interface
both serve same thing so why two why not anyone ???
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manoj,
Welcome to JavaRanch!
Because Java has single inheritance only -- you can only extend one concrete class -- the roles of interfaces and abstract classes are quite different. Abstract classes provide code, while interfaces provide only type information. Ideally, if a method accepts an object of some type as an argument, and the idea is that there will be many implementation of that type, then it is always best for that type to be an interface, so that the implementors of that type are free to extend any class they want when writing their implementation. An abstract class can provide a "typical" or "default" implementation, but implementors shouldn't be forced to extend any specific class (if it can be avoided.)
Chapter 4 of Joshua Bloch's "Effective Java" has some very good discussions of this and related concepts.
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract class and an interface is not the same thing.Both are different in the case that...An interface can have only method prototypes i.e.,no method bodies. But an abstract class can have method prototypes like interfaces and also can have non abstract methods i.e., methods with body.Thus java gave this functionality of interfaces i.e., of defining method types to the class definions also n hence we got the abstract classes.
i hope this works out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!