Re: Why I need Interface



On Sun, 10 Aug 2008 14:33:01 -0700, Umeshnath <Umeshnath@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

My query is why I need an Interface, since my abstract class does the same
functionality

[...]
I know using interface we can go multiple inheritance
I am looking in such way the in terms of OOPS in C# what is the significance

If you understand that in C# you can only inherit one base class, I don't understand why you're asking the question.

An abstract class is useful for providing some default or fundamental implementation. But it ties you to a specific base class, preventing you from inheriting any other base class. If you have no implementation you want to provide through inheritance, why lock yourself into it by using an abstract class rather than an interface?

Another important reason to use an interface is that value types can't inherit abstract classes (and you can't make an abstract value type). So if you want for your interface to be able to be implemented by a value type, it has to be an interface, not an abstract class.

Basically, interfaces are more flexible, giving you more options. If you have no need for the extras that an abstract class provides, then an interface is the way to go.

Pete
.



Relevant Pages

  • Re: Abstract class or interface?
    ... I try to make the decision based on the relationship of the derived class to the base class. ... If the derived class "can act like" the type of the base class, I'd lean towards an interface. ... I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Concrete class
    ... >> abstract class with ALL pure virtual functions. ... >> above philosophy then it means that your Shape is an interface. ... > of doing this is to keep the inheritance hierarchy at a low depths" is ...
    (comp.lang.cpp)
  • Re: Abstract Base Classes vs Interfaces?
    ... an abstract class is the only choice. ... You can take any class and derive from it adding in an interface ... > doesn't have to be derived from any particular base class. ... I think ICollection is a good example of the ambiguity between ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Abstract class or interface?
    ... Abstract classes require and imply inheritance whereas interfaces do not. ... An abstract class would be used wherever it was important to enforce some ... aspect of the implementation an interface is used where only the agreement ... > derived classes can only inherit one abstract class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: User Controls (Active X) - Da Process
    ... VB can't do 'real' inheritance. ... VB's 'interface inheritance' is more like interface duplication, ... Even if the class delegates to an internally held base class, ... is duplicated for every level of inheiritance. ...
    (microsoft.public.vb.general.discussion)

Loading