Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff b/w abstract classes and Interfaces

 
Mallik Koppuravuri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

Can anybody explain what is the exact difference b/w abstract classes and Interfaces.
I read that both of them have just the declarations of the methods and no definitions of the methods. Then why do we have two different approaches for the same need.

I will appreciate if any body can quote some examples to explin this.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is asked many time in java forum.
An abstract class can have different flavours of modifiers like public,private etc but in an interface the members can only be public.
A abstract class can have method definitions but an interface cant.
These are very basic difference.
Google for this topic you will get a lot more. The main thing we need to understand is where to use abstract class & where to use an interface.
 
Mallik Koppuravuri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You . I will check the posts.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the most important difference is that a class can directly extend only one other class, but it may implement any number of interfaces.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mallik Koppuravuri:
hi ,

Can anybody explain what is the exact difference b/w abstract classes and Interfaces.
I read that both of them have just the declarations of the methods and no definitions of the methods. Then why do we have two different approaches for the same need.

I will appreciate if any body can quote some examples to explin this.


Also, the definition given for an abstract class is not quite correct. An abstract class CAN contain definitions of any (or all) of its methods, but an interface cannot.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic