Abstract Class vs. Interface

  • Thread starter Thread starter New Comer
  • Start date Start date
Programmer's:
Legally, you can write code in an abstract class, while you are prohibited
to do so in an interface. So a concrete implementor of an abstract class can
use the abstract class's method code(say to be called before it implements
it). However both can't be instantiated.

A slightly higher level defn:
Interfaces are contracts. They help identify common behaviours of objects.
If you see any object that implements, say inherface "IHuman", you will come
to know that it would have provided implementation for ,say, methods like
"Eat(calorificVal), Sleep(hours)" etc, although each object's implementation
might be very different. (Say IndianHuman:IHuman might throw an exception on
"Sleep(hours)" except when called on a public holiday!)

Back to programmer's:
With the above example in mind, if you want to say that a person can
implement a "Sleep(hours)" and "Eat()" in whichever way he wants, you just
give an interface. But you want all humans to get a sleep for, say, 1 hour
at a minimum(otherwise yell at the person giving him less - in other words,
throw an exception), you write an abstract class with the concrete
implementation for "Sleep()" and abstract method "Eat()". So an abstract
class helps you control certain portions out of a set of defined behaviors.

Phew....so much for a one-line Q!

HTH
Krishnan
Codito Ergo Sum
 
Back
Top