Normal class vs Abstract class vs Interface class
69I have read many articles about normal class vs abstract class vs interface class I think i m cleared now on the difference b/w these 3. I think many of you also cleared about this concept but the real test for all of us is where to use them. When to use which one. I try my best to elaborate these concepts.
First i am going to define the basic definition of these three concept.
Normal class:
Normal class have definition of functions and variables and their declaration as well.
Abstract class:
Abstract class have function and variables with definition and declaration as well like the normal classes. We can not make instance of Abstract Class.
Interface class:
Interface class have functions and variables with only definition. We can not make instance of Interface Class.
Now a million dollar question when to use these classes?
Lets take an example
A Content management system for Media, Media is a generalize form of TV, RADIO & NEWSPAPER
MEDIA
TV
RADIO
Newspaper
Now the decision time
Should we make an instance of Media?
If yes then use normal class
else if no then use either abstract or interface class
if media have function let's say schedule() and it have some default behavior then use abstract class
else if every derived class have its own definition of schedule() use interface class
abstract Class media
{function abstract schedule()
{
/*default schedule behaviour*/
}
}
interface Class media
{function schedule(); } Interface class can give you multiple inheritance where abstract cannot. Abstract class is faster then interface class because interface requires extra in-direction. the major draw back of interface class is that you have to define the functionality in every derived class when every any function added in base class other wise it will give an error.
Give you feedback on this article and give more examples on this topic
PrintShare it! — Rate it: up down flag this hub



