Why is there an abstract class and interface in C #?

Why do both abstract classes and interfaces exist in C # if we can achieve the interface function by making all members of the class abstract.

It's because:

  1. There is an interface for multiple inheritance
  2. It makes sense to have an interface, since the CAN-DO object of the object must be placed in an abstract abstract interface class.

Please clarify

+12
c # oop
Jun 22 '09 at 16:48
source share
11 answers

Well, an abstract class may indicate some implementation, but usually not all. (Having said that, it is quite possible to provide an abstract class without abstract members, but many virtual ones that are with the implementation of "no-op"). The interface does not provide implementation, but only a contract.

You could, of course, argue that if multiple class inheritance is allowed, interfaces will be basically meaningless.

Personally, I do not focus on the whole "difference" for inheriting "is-a" vs "can-do". It never gives me such good intuition what to do, how to just play with different ideas and see which ones feel the most flexible. (Again, I really love the "supportive composition by inheritance" ...)

EDIT: just like the most convenient way to refute the third paragraph of lbushkin in his comment ... you can override the abstract method with a non-virtual one (in terms of the fact that it cannot override it) by sealing it:

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

Classes derived from Derived can no longer override Foo .

I in no way suggest that I want multiple inheritance of the implementation, but if we had it (along with its complexity), then an abstract class that simply contained abstract methods would accomplish almost everything the interface does. (There's a question of explicitly implementing the interface, but that's all I can think of at the moment.)

+26
Jun 22 '09 at 16:51
source share

This is not a trivial question, it is a very good question, and I always ask all the candidates whom I am interviewing. In a nutshell - an abstract base class defines a hierarchy of types, while an interface defines a contract.

You can see it as it is vs implements a.
those. Account can be an abstract base account, since you could have CheckingAccount , a SavingsAccount , etc. everything that stems from the abstract base class Account . Abstract base classes may also contain non-abstract methods, properties, and fields, like any normal class. However, interfaces only contain abstract methods and properties that should be implemented should .

C # allows you to get only one base class - one inheritance, like java. However, you can implement as many interfaces as you want - this is because an interface is just a contract that your promises class must implement.

So, if I had the SourceFile class, then my class could choose the implementation of ISourceControl , which says: "I promise to honor the implementation of the methods and properties required by ISourceControl

This is a large area and probably deserves a better post than the one I gave, but I'm too short, but hope this helps!

+14
Jun 22 '09 at 16:59
source share

Both of them exist because they are both very different things. Abstract classes allow implementation, but interfaces do not. The interface is very convenient, since it allows me to say something about the type that I create (it is serializable, it is edible, etc.), but it does not allow me to define any implementation for the members I define.

An abstract class is more powerful than an interface, in the sense that it allows me to create an inheritance interface through abstract and virtual elements, but it also provides some standard or basic implementation, if I want to. However, as Spider-Man knows, this great power carries great responsibility, since the abstract class is more architecturally fragile.

Note: It is interesting to note that Vance Morrrison (from the CLR team) suggested adding default implementations of the method to interfaces in a future version of the CLR. This greatly blurs the distinction between an interface and an abstract class. See this video for more details.

+7
Jun 22 '09 at 16:50
source share

One important reason that both mechanisms exist is that C # .NET allows only one inheritance, rather than multiple inheritance such as C ++. Class inheritance allows you to inherit an implementation from only one place; everything else must be done by implementing interfaces.

For example, suppose I create a class, such as Car and I subclass, into three subclasses: RearWheelDrive, FrontWheelDrive, and AllWheelDrive. Now I decide that I need to cut out my classes on another β€œaxis”, like those who have buttons and without them. I want all push-button starter cars to have the PushStartButton () method and non-button cars with the "TurnKey ()" method, and I want to be able to process car objects (with regard to their launch), no matter what subclass they are there is. I can define the interfaces that my classes can implement, such as IPushButtonStart and IKeyedIgnition, so I have a general way of handling my objects, which differ in that it does not depend on one base class from which everyone gets.

+2
Jun 22 '09 at 17:12
source share

You have already given a good answer. I think your second answer is the real reason. If I wanted to create a Compareable object, I should not have deduced Comparable from the base class. if you think about all the interfaces, think about all the permutations that you used to handle basic interfaces like IComparable.

Interfaces allow you to define a contract around the publicly available behavior that the object provides. Abstract classes allow you to define both behavior and implementation, which is completely different.

+1
Jun 22 '09 at 16:51
source share

Interfaces exist for providing a class without any implementation, so .NET can provide support for safe and functional multiple inheritance in a managed environment.

+1
Jun 22 '09 at 16:52
source share

The interface defines the contract that the implementation class must execute; this is a way of saying that "it does it." An abstract class is a partial implementation of a class that, by definition, is incomplete and needs to be completed. They are completely different.

0
Jun 22 '09 at 16:53
source share

An abstract class can have an implementation, and the interface simply allows you to create a contract that artists must follow. With abstract classes you can provide general behavior for your subclass, because you cannot with interfaces.

0
Jun 22 '09 at 16:53
source share

They serve two completely different purposes.

Abstract classes provide a way to inherit an object based on a specific contract, and also allow you to specify behavior in the base class. This, from a theoretical point of view, provides an IS-A connection in that a particular IS-A class is of a particular type of base class.

Interfaces allow classes to define (or more than one) the contract that they will execute. They allow ACTS-AS or "can be used as a" type of relationship "rather than direct inheritance. That is why, as a rule, interfaces will use an adjective because they are a name (IDisposable) instead of a noun.

0
Jun 22 '09 at 16:53
source share

The interface is used for what the class can do, but it is also used to hide some of the things that the class can do.

For example, the IEnumerable<T> interface describes that a class can iterate through it, but it also restricts access to this single ability. A List<T> can also access elements by index, but when you access it through the IEnumerable<T> interface, you only know about this ability to iterate members.

If a method accepts an IEnumerable<T> interface as a parameter, it means that it has only tried the iteration through members. You can use several different classes with this ability (for example, List<T> or array T[] ) without the need to use one method for each class.

Not only does the method allow several different classes that implement the interface, you can create new classes that implement the interface, and the method will also gladly accept them.

0
Jun 22 '09 at 17:01
source share

Consider the Fetus class, which is excreted by two children ( Apple and Banana ), as shown below:

 class Fruit { public virtual string GetColor() { return string.Empty; } } class Apple : Fruit { public override string GetColor() { return "Red"; } } class Banana : Fruit { public override string GetColor() { return "Yellow"; } } 

We have an existing ICloneable interface in C # . This interface has one method, as shown below, a class that implements this interface ensures that it can be cloned:

 public interface ICloneable { object Clone(); } 

Now, if I want to make the Apple (not Banana ) class cloned, I can simply implement ICloneable as follows:

  class Apple : Fruit , ICloneable { public object Clone() { // add your code here } public override string GetColor() { return "Red"; } } 

Now, considering your argument to a pure abstract class, if C # had a pure abstract class, say Clonable instead of the IClonable interface as follows:

 abstract class Clonable { public abstract object Clone(); } 

Now can you make your Apple class cloned by inheriting an abstract Clonable instead of IClonable ? eg:

 // Error: Class 'Apple' cannot have multiple base classes: 'Fruit' & 'Clonable' class Apple : Fruit, Clonable { public object Clone() { // add your code here } public override string GetColor() { return "Red"; } } 

No, you cannot, because a class cannot be obtained from several classes.

0
Nov 12 '17 at 10:23
source share



All Articles