C# .NET - abstract class

Asked By shikha ... on 20-May-11 12:29 PM
what is the difference between abstract class and interface
Ravi S replied to shikha ... on 20-May-11 01:18 PM
hi

There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:

Feature

Interface

Abstract class

Multiple inheritance

A class may inherit several interfaces.

A class may inherit only one abstract class.

Default implementation

An interface cannot provide any code, just the signature.

An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties

Core VS Peripheral

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

Homogeneity

If various implementations only share method signatures then it is better to use Interfaces.

If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed

Requires more time to find the actual method in the corresponding classes.

Fast

Adding functionality (Versioning)

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Fields and Constants No fields can be defined in interfaces An abstract class can have fields and constrants defined

refer the link
http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx
dipa ahuja replied to shikha ... on 20-May-11 01:40 PM
1) A class may inherit only one abstract class, but may implement multiple number of Interfaces. Say a class named Car needs to inherit some basic features of a vehicle, it may inherit from an Aabstract class named Vehicle. A car may be of any kind, it may be a vintage car, a sedan, a coupe, or a racing car. For these kind of requirements, say a car needs to have only two seats (means it is a coupe), then the class Car needs to implement a member field from an interface, that we make, say ICoupe.

2) Members of an abstract class may have any access modifier, but members of an interface are public by default, and cant have any other access modifier.

3) Abstract class methods may OR may not have an implementation, while methods in an Interface only have a definition, no implementation
Nikhil Mahajan replied to shikha ... on 20-May-11 11:05 PM

Abstract Class..
1.It cannot defines all the methods
2.It has subclass.
3.Here, Subclass is useless
4.A class can be extend an abstract class

Interface..
1.It defines all the methods
2.It must have implementations by other classes, But there will be no use of that.
3.Only an interface can extend another interface.

For More details..

http://kyapoocha.com/c-sharp-interview-questions/what%E2%80%99s-the-difference-between-an-interface-and-abstract-class-5/

http://www.dotnetuncle.com/Difference/4_abstract_class_interface.aspx

http://forums.msdn.microsoft.com/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd/

Riley K replied to shikha ... on 21-May-11 12:37 AM
Abstract class: Partially implemented.
use:used when u know some of the implementations.Ex-dataset
Here u know that tables can be stored but what dimensions......?
but u know tables have rows n columns and can be implemented.

Interface: these are fully abstract.U can't imlement anything here.
Use:used for assigning methods 2 be implemented.
At the top level u can't decide(or make design decision) on the implementaion always & is taken care by the classes that implement them.

In simple terms

What is Interface ?
  • The behavior of a class
  • All methods of an Interface are abstract methods.
  • They cannot have bodies
  • You cannot create an instance from an interface
  • An interface can only be implemented
  • This is the concept of encapsulation
  • During runtime, actual object instance is associated with the interface type
  • It needs only the interface at the compile time
  • Interface does not contain constructor
  • Multiple inheritance feature can be achieved through interface
  • Cannot be versioned.

Abstract Class
  • It is a class that contains one or more abstract methods
  • It may contain constructor but interface does not contain constructor.
  • In Interface all methods should be public but not in abstract class
  • Abstract class we can put sharable code
  • Its not Possible to create instance of Abstract class
  • It cannot be instantiated on it’s own, it must be inherited.
  • It can use One Overriding method
  • It can specify members that must be implemented in inheriting classes
  • A class can inherit only one abstract class
  • It enables us to place common instance behavior.
  • Offer versioning benefits.
  • Used for sharing common features among verious objects.

When to prefer an interface


Back when I wrote about the http://codeofdoom.com/wordpress/2009/02/04/the-importance-of-composition/, I mentioned that it is extremely useful when you don’t want a massive hierarchical type framework. The same applies to interfaces. This isn’t my example, but its the best one Ive come across. Lets say you have an interface for a Director and another interface for a Actor.

public interface Actor{
   Performance say(Line l);
}
public interface Director{
   Movie direct(boolean goodmovie);
}

In reality, there are Actors who are also Directors. If we are using interfaces rather than abstract classes, we can implement both Actor and Director. We could even define an ActorDirector interface that extends both like this:

public interface ActorDirector extends Actor, Director{
...
}

We could achieve the same thing using abstract classes. Unfortunately the alternative would require up to 2^n (where n is the number of attributes) possible combinations in order to support all possibilities.

When to prefer an Abstract class

Abstract classes allow you to provide default functionality for the subclasses. Common knowledge at this point. Why is this extremely important though? If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Why? Because you can make a change to it and all of the inheriting classes will now have this new functionality. If the base class will be changing often and an interface was used instead of an abstract class, we are going to run into problems. Once an interface is changed, any class that implements that will be broken. Now if its just you working on the project, that’s no big deal. However, once your interface is published to the client, that interface needs to be locked down. At that point, you will be breaking the clients code.