Dennis’s Weblog

The differences between interface and abstract class

Posted on: October 4, 2008

  1. Interfaces are essentially having all method prototypes no definition but Abstract class can contain method definations also.
  2. In short Interface is a abstract class having all methods abstract.
  3. Both abstract classes and interfaces are used when there is a difference in behaviour among the sub-types extending the abstract class or implementing the interface.
  4. When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used. In an abstract class the partially common behaviour is given a concrete implementation. Since there is no common behaviour between an interface and a sub-type an interface does not have an implementation for any of its behaviour.           Here is the point when we want to encapsulate the varies, we should meditate on selecting which struct.
  5. If you create a abstract class writing the abstract keyword in the declaration part then You only can inherit the class. You can not create an instance of this abstract class but can inherit the class and with creating the instance of the derived class you can access the method of the abstract class.
  6. If you use a virtual keyword in a method then you can override this method in the subclass if you wish..
  7. If you create a abstract method then you must override this method in the subclass other wise it shows error in the program.
  8. Abstract class will may or may not contain contructor while interface will not.

references:

a. When to use override and new keywords(http://msdn.microsoft.com/en-us/library/ms173153(VS.80).aspx);

b.virtual keyword:

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.

By default, methods are non-virtual. You cannot override a non-virtual method. So when you call the method  though the dynamic call(superclass), it will still call the original function. But not dynamically call the function corresponding to the type of the object.

You cannot use the virtual modifier with the static, abstract, private or override modifiers.

Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.

  • It is an error to use the virtual modifier on a static property.
  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

The implementation of a virtual member can be changed by an overriding member in a derived class. For more information on using the virtual keyword, see Versioning with the Override and New Keywords (C# Programming Guide) and Knowing When to Use Override and New Keywords (C# Programming Guide).

C:

class A
{
public void  hello()
{
System.Console.Out.WriteLine(“I am A”);
}
}
class B : A
{
public void hello()//It will prompt you to use “NEW” keyword to conceal the A::hello(), but if you miss out “NEW”, it’s the same !!!!
{
System.Console.Out.WriteLine(“I am B”);
}
}

//Main in C#

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: