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 ???
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.
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.
Thnx
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, the permaculture playing cards