Click here to Skip to main content
14,865,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can you please tell me the scenario when to use abstract class and when to use interface?

i got one nice unsolved interview question is that
if you have one abstract class and one interface with the same methods
which one you will use and why?

Both interfaces and abstract classes impose a contract to derived classes.
An abstract class, however, does implement some methods, so, if your derived class needs to inherit such base functionality you have to use it.
#realJSOP 22-Jun-10 8:28am
An abstract class can also not implement any methods at all (beyond the abstract methods it's defining, of course).
CPallini 22-Jun-10 9:01am
Yes, I find it hardy useful, however.
This codeproject article will explain it to you:

Abstract Class versus Interface[^]
CPallini 22-Jun-10 14:29pm
As a side note, your answer is obviously better than mine. Cannot understand why you got only 4.
#realJSOP 22-Jun-10 15:57pm
I think it's because it required reading something more than just a few words.

Basically there is no implementations in an Interface.While we using an interface , you should implement the methods/properties inside your class.i.e;an interface method is doing nothing until you implemented in your class.The implementation class is responsible for the total implementation in case of an interface.

Abstract class

An absract class is a base class that behaves like a normal class except these have no independent existance, i.e;it is not possible to create an instance of an abstract class directly.The instance can be created using the derived class only.The methods/properties may/mayn't be implemented in the derived class except for abstract members.The abstract members should be implemented in the derived class.

Thus an interface looks similar to an abstract class with only abstract members.

public abstract class SampleAbstract
   public abstract void Show();
   public abstract void Calculate();

is almost similar to

public interface SampleInterface
    void Show();
    void Calculate();
My biggest problem with interfaces actually has something to do with Visual Studio rather than structure itself.

If you right click and "Go to Definition" in any class that inherits from an interface, you go to the interface, not the class you really wanted to see. Then you end up doing a global search for the inherited class. This annoys me.

But there is the one advantage to interfaces: If you inherit from an interface, you can inherit from the inherited class. If you use an abstract class, you can only inherit once.

If you want a hard and fast scenario, I would say this: if you are developing a commercial DLL that a user will implement, or you know the general structure of what you are planning, but not how it will be fleshed out, then use an interface.

If you have a good idea of what you want and you don't plan on inheriting that class more than once, use an Abstract class.

Ryan McBeth
hammerstein05 22-Jun-10 15:44pm
"Then you end up doing a global search for the inherited class. This annoys me."

I agree that it's frustrating, but loose coupling means you don't really bind it to a solid implementation until you need, right? It would be helpful if "Go To Definition" popped up a list of classes that implement this interface.
Ryan McBeth 22-Jun-10 15:48pm
That would be nice. Maybe something for Visual Studio 2010. I'm glad to see that I'm not the only one who is annoyed by this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900