Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Only one question in interface and abstract class

 
Shanmugam nagaraj
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
my question is regarding Interface and abstract class.

I know the difference between the abstract class and interface.
But I need to know in real time application when we use the interface and when we use the abstract class.

What all are the scenario we use this both can any one help me out regarding this pls..

Thanks


Shanmugam N
 
Ricky Clarkson
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is useful for declaring capabilities. An abstract class is useful for inheriting them. Use an abstract class when you need to inherit behaviour (or when inheriting behaviour is the best way of reusing code).

You never need to inherit behaviour, you can always get it in some other way (composition, just calling a static method, etc.), and those other ways are, in my opinion, always better than subclassing. Therefore, never use an abstract class, if you ever work with me.
 
Anita Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ointerface contains methods that must be abstract; abstract class may contain concrete methods.
ointerface contains variables that must be static and final; abstract class may contain non-final and final variables.
omembers in an interface are public by default, abstract class may contain non-public members.
interface is used to "implements"; whereas abstract class is used to "extends".
interface can be used to achieve multiple inheritance; abstract class can be used as a single inheritance.
interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces.
interface is absolutely abstract; abstract class can be invoked if a main() exists.
interface is more flexible than abstract class because one class can only "extends" one super class, but "implements" multiple interfaces.
If given a choice, use interface instead of abstract class.

Anita Singh
 
Anita Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ointerface contains methods that must be abstract; abstract class may contain concrete methods.
ointerface contains variables that must be static and final; abstract class may contain non-final and final variables.
omembers in an interface are public by default, abstract class may contain non-public members.
interface is used to "implements"; whereas abstract class is used to "extends".
interface can be used to achieve multiple inheritance; abstract class can be used as a single inheritance.
interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces.
interface is absolutely abstract; abstract class can be invoked if a main() exists.
interface is more flexible than abstract class because one class can only "extends" one super class, but "implements" multiple interfaces.
If given a choice, use interface instead of abstract class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic