Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do both the abstract class and interface exist in C#?

Tags:

c#

.net

oop

Why do both the abstract class and interface exist in C# if we can achieve the interface feature by making all the members in the class as abstract.

Is it because:

  1. Interface exists to have multiple inheritance
  2. It makes sense to have interface because object's CAN-DO feature should be placed in an interface rather base abstract class.

Please clarify

like image 883
123Developer Avatar asked Jun 22 '09 16:06

123Developer


People also ask

Why do we need both interface and abstract classes?

Multiple implementations: An interface can extend one or more Java interfaces; an abstract class can extend another Java class and implement multiple Java interfaces. Multiple Inheritance: Interface supports multiple inheritance; an abstraction does not support multiple inheritance.

What do abstract classes and interfaces have in common?

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.

Can we use interface and abstract class together?

It's perfectly normal to use these two together. Consider for instance AbstractList (implementing List ) and AbstractMap (implementing Map ) in the JDK. Further, I can see an argument for doing it that way, specifically that it removes the coupling between the abstract class and the interface.


2 Answers

Well, an abstract class can specify some implemetation, but usually not all of it. (Having said which, it's perfectly possible to provide an abstract class with no abstract members, but plenty of virtual ones which with "no-op" implementations). An interface provides no implementation, merely a contract.

You could certainly argue that if multiple inheritance of classes were permitted, interfaces would be largely pointless.

Personally I don't get hung up on the whole "is-a" vs "can-do" distinction for inheritance. It never gives me as good an intuition about what to do as just playing around with different ideas and seeing which ones feel the most flexible. (Then again, I'm very much a "favour composition over inheritance" guy...)

EDIT: Just as the most convenient way of rebutting lbushkin's third point in his comment... you can override an abstract method with a non-virtual one (in terms of not being able to override it further) by sealing it:

public abstract class AbstractBase
{
    public abstract void Foo();
}

public class Derived : AbstractBase
{
    public sealed override void Foo() {}
}

Classes deriving from Derived cannot override Foo any further.

I'm not in any way suggesting I want multiple inheritance of implementation - but if we did have it (along with its complexity) then an abstract class which just contained abstract methods would accomplish almost everything that an interface does. (There's the matter of explicit interface implementation, but that's all I can think of at the moment.)

like image 54
Jon Skeet Avatar answered Oct 11 '22 20:10

Jon Skeet


It's not a trivial question, it's a very good question and one I always ask any candidates I interview.
In a nutshell - an abstract base class defines a type hierarchy whereas an interface defines a contract.

You can see it as is a vs implements a.
i.e Account could be an abstract base account because you could have a CheckingAccount, a SavingsAccount, etc all which derive from the abstract base class Account. Abstract base classes may also contain non abstract methods, properties and fields just like any normal class. However interfaces only contain abstract methods and properties that must be implemented.

c# let's you derive from one base class only - single inheritance just like java. However you can implement as many interfaces as you like - this is because an interface is just a contract which your class promises to implement.

So if I had a class SourceFile then my class could choose to implement ISourceControl which says 'I faithfully promise to implement the methods and properties that ISourceControl requires'

This is a big area and probably worthy of a better post than the one I've given however I'm short on time but I hope that helps!

like image 25
zebrabox Avatar answered Oct 11 '22 20:10

zebrabox