Coding Villa Forum
FAQs
Advertisements

Difference between Interface and Abstract Class?

Iram Online 6/28/2012 12:48:03 PM

Hello Friends!

I want to know about the basic difference between interface and Abstract class in terms of usability. I mean in which scenarios it is best to use Interface instead of Abstract class. Please friends elaborate the difference with coding examples.

Waiting for your guidance friends

Thanks

Share with Friends

Advertisements

Answer

Mehmood Online 6/29/2012 12:21:14 AM

-> If I talk about Object Oriented Programming and Real World mapping then we use

both abstract class and interface for achieving polymorphism.

Poly means several and morphic means shapes.

-> Now if we combine them then we come to know that we could achieve many shapes.

-> For making a plug and play framework , we use polymorphism.


-> There is a bit difference between both but most of us are unfamiliar.

-> Abstract class is often used for same category or type.

i presume that Person is an Abstract class and Cashier is its derived class.

Now see both are of same category so with respect to Object Oriented Design,

we should achieve polymorphism through abstract classes.

When do we require to place an Abstract keyword before the name of a class?

if we have just a single abstract method/member.


-> In other case, we use Interfaces for different types/Category but

behaviour would be same.

Now i presume that Human and Machine can Calculate things.

ICalculator could be an interface between Machine and Human and both can implement this interface.

See now types are not of same type but behaviour is same.

we use prefex "I" before the name of Interface and postfix indicates some action oriented thing.



-> Rather than creating objects of abstract classes/ Interfaces, we create derived class' object

and fill according and pass it to Abstract class/ Interface and call methods (behaviour)

from Parent abstract classes/ Interfaces.



-> A source solution would be provided sooner .

Answer

Mehmood Online 6/29/2012 1:47:21 AM

Get the source from mentioned shared path:

http://sdrv.ms/LjrXdm

http://sdrv.ms/NSB0p4

Source has been developed in Visual Studio 2010 targeting framework 3.5.

if you are unable to execute then add files from add existing items option in to newly created project.

Answer

Mehmood Online 7/1/2012 11:20:14 PM

abstract class could have virtual methods by the combination of abstract one.

But interface just have abstract methods and the class inherited must have to implement its all methods.

Now what is virtual methods?

If a method of as abstract parent class has no body and we don't need to use abstract method

(in c , we called them pure virtual functions) and if some method has body (logic) then we use virtual method.

both abstract and virtual method have to override in child classes. we could skip virtual method because this

is an optional.
Share with Friends

Post reply

 

Enter This Code
Captcha
 

Subscribe

Email me when people reply


Subscribe to the