locked
A C sharp basic question!

    Question

  •  

    I wonder the difference between the abstract class and the interface!

    Someone help me!

    Thursday, December 13, 2007 12:49 AM

Answers

  • Hi,

     

    An abstract class is a class that cannot be instantiated, but must be inherited from.

     

    An interface is a contract that classes MUST adhere to.

     

    So both of these types cannot be instantiated.  However a class that is derived from an abstract class may still implement interfaces.

     

    Here are some differences :

     

    When class is derived from interface, it must implement all it's methods. Interface doesn't contain any implementation code, it only contains the declarations of Methods, Properties and events.

    Abstract class may contain full or partial implementation. However one method is abstract. So Derived class can implement only this abstract function and use the base code for others

    A class may implement several interfaces

    but can only extend one abstract class.

    Interface do not have access modifiers. Everything is Public

    Abstract classes can have accesss modifiers.

     

    You can do some more reading over here :

     

    http://msdn2.microsoft.com/en-us/library/k535acbf(VS.71).aspx

     

    http://msdn2.microsoft.com/en-us/library/scsyfw1d(vs.71).aspx

     

     

    HTH,
    Suprotim Agarwal

    -----
    http://www.dotnetcurry.com
    -----

     

     

    Thursday, December 13, 2007 1:34 AM

All replies

  • Hi,

     

    An abstract class is a class that cannot be instantiated, but must be inherited from.

     

    An interface is a contract that classes MUST adhere to.

     

    So both of these types cannot be instantiated.  However a class that is derived from an abstract class may still implement interfaces.

     

    Here are some differences :

     

    When class is derived from interface, it must implement all it's methods. Interface doesn't contain any implementation code, it only contains the declarations of Methods, Properties and events.

    Abstract class may contain full or partial implementation. However one method is abstract. So Derived class can implement only this abstract function and use the base code for others

    A class may implement several interfaces

    but can only extend one abstract class.

    Interface do not have access modifiers. Everything is Public

    Abstract classes can have accesss modifiers.

     

    You can do some more reading over here :

     

    http://msdn2.microsoft.com/en-us/library/k535acbf(VS.71).aspx

     

    http://msdn2.microsoft.com/en-us/library/scsyfw1d(vs.71).aspx

     

     

    HTH,
    Suprotim Agarwal

    -----
    http://www.dotnetcurry.com
    -----

     

     

    Thursday, December 13, 2007 1:34 AM
  •  

    Thanks

    Can a method in an abstract  class be overload? And how about an interface?

    Thursday, December 13, 2007 3:49 AM
  • abstract class is to define class template , use to inherited by other classes

    interface to define class behavior, and use with class

     

    Thursday, December 13, 2007 8:47 AM
  •  xiaobin700 wrote:

     

    Thanks

    Can a method in an abstract  class be overload? And how about an interface?

     

    Hi,

     

    Of course, you can :

     

    Check this example :

     

    Code Block

    // Overloading in abstract class

    public abstract class MyClass

    {

    public void MethodOne()

    {

    }

    public void MethodOne(int i)

    {

    }

    }

     

    // Overloading in Interfaces

    public interface ISomeInterface

    {

    void SomeMethod();

    void SomeMethod(int a);

    }

     

     

     

     

    HTH,
    Suprotim Agarwal

    -----
    http://www.dotnetcurry.com
    -----

    Friday, December 14, 2007 7:54 AM