• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Diff between Abstract Class and Interface

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic