Labels

Wednesday, June 20, 2007

Interface vs Abstract

Interface vs Abstract

 

Interface

Abstract

1

Interface cannot be instantiated

Same as Interface

2

Inteface cannot contain concrete methods. But still there is no need to declare those methods as ‘abstract’ as in Abstract classes.

Abstract can have both Concrete & Abstract methods.

An ABSTRACT function does not have any implementation, and it must be overridden in any non-abstract derived class, using OVERRIDE keyword.

 

Note : C# does not support the “= 0” syntax to declare abstract functions.

 

Note: If there is any function is abstract in a class, then that class has to be declared as ABSTRACT.

 

 

e.g.

abstract class AbstractPent

{

   public abstract string Show();

 

}

 

class AmericanPent : AbstractPent

{

public override string Show()

{

 return this.GetType().Name;

}

                       

}

3

Do not support Inheritance

Supports Inheritance

4

Cannot contain Fields (Member Vairables) & Constructor.

Otherwise – Methods, Properties, Events & indexers are allowed.

 

//Interface
public interface Vehicles
{
string Engine
         {  
              get;
              set;
}
void Accelerator();
}

 

Can contain anything.

5

Not allowed to specify access modifiers.

By default they are public and cannot be virtual or static.

No Problems

6

An interface definition is also not allowed to contain operator overloads, though that’s not because there is any problem in principle with declaring them— there isn’t; it is because interfaces are usually intended to be public contracts, and having operator overloads would cause some incompatibility problems with other .NET languages, such as Visual Basic .NET, which do not support operator overloading.

 

No Problems

 

 

 

 

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

2 comments:

  1. Hi Arun

    I was going through some of your old (useful) blogs and noticed this entry.

    I thought interfaces also supports inheritance. What do you think?

    Ref-
    http://msdn.microsoft.com/en-us/library/ms173156.aspx

    Thanks

    ReplyDelete
  2. My Freind,

    Thanks for the comments & the reference.
    You are right - Interface does support inheritance.

    Test Code -
    --------------

    namespace WindowsApplication1
    {
    interface Test
    {
    string MyMethod();
    }

    interface AnotherTest : Test
    {
    string YourMethod();
    }

    class Class1 : AnotherTest
    {
    #region test2 Members

    public string YourMethod()
    {
    return "Your Method";
    }

    #endregion

    #region test Members

    public string MyMethod()
    {
    return "My Method";
    }

    #endregion
    }
    }


    Test-

    Class1 obj = new Class1();
    MessageBox.Show(obj.MyMethod());
    MessageBox.Show(obj.YourMethod());

    Regards,
    Arun Manglick

    ReplyDelete