开发者

Abstract classes and interfaces in C# [duplicate]

开发者 https://www.devze.com 2022-12-21 07:16 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: Interface vs Base class
This question already has answers here: Closed 12 years ago.

Possible Duplicates:

Interface vs Base class

Interface or abstract class?

Hi All,

I am just thinking about the abstract classes and interfaces, I know how they work technically but i don't und开发者_运维问答erstand the real use of abstract classes and interfaces. I mean why should we use abstract classes when we know that we can't create it's object, we have to anyway extend this class to use it so why don't we put everything in the derived class.

Same is with interfaces, we have to implement the interface so why don't we put all the methods which are defined in the interface in the class in which we implement the interface.

Can someone please clear my doubts with some examples ?


Links at Stackoverflow

  • Exact use of Abstract class
  • Interface or abstract class?
  • Abstract classes vs Interfaces
  • Why do both the abstract class and interface exist in .Net?
  • differences between an abstract class and an interface in C#
  • What’s the difference between an interface and an abstract class?
  • When to use an interface instead of an abstract class and vice versa?
  • Help me understand in a practical example the usage abstract classes vs interfaces?

Others

  • Abstract Class versus Interface
  • Should I use an abstract class or an interface?
  • Abstract base class over interface
  • Learn This: When to use an Abstract Class and an Interface
  • Recommendations for Abstract Classes vs. Interfaces - MSDN
  • Working with Abstract classes, Sealed Classes, and Interfaces in C#
  • Difference between Abstract class and Interface in C# (.Net)


Also of note, in C# you can only inherit one class but multiple interfaces. An abstract class is a partial implementation of a class with reusable code for multiple subclasses. An interface is just that, an interface to the object for a specific purpose.

A good example would be objects in a game. If the object is Drawable and Collideable you may have common routines to handle that but no common base class, so you have the class implement IDrawable and ICollidable.


What do you do when there's more than one derived class sharing the same abstract behavior?

For a good example, consider the Stream class in .NET.


I've always used abstract classes as a road-map for derived classes. A lot of times I'll have a series of classes that will all be similar and will be able to share functionality but they'll all be dissimilar enough that they need their own class. So the base class is not fully functional and it has a few methods that are designed for that particular class. So by declaring it abstract, you put the functionality in the namespace for general usage, but you are still able to confine its usage to that set of classes.

Interfaces (to me) are nothing more than a blueprint. I've never NEEDED an interface, but when publishing an extendable API or making a framework within an organization, interfaces are good blueprints for how extendable classes should behave. It helps define a standard within the namespace for the functionality that is expected of a particular set of objects.

I would say neither is specifically necessary, but they are tremendously helpful in segregating your code and defining how it should be used and/or extended.


In my work, I use interfaces when I want to create plugins for specific types of components that need to be treated identically by the application, but their behavior is component-specific. In other cases, I've wanted some common behavior amongst derived classes, so I use an abstract class to define the methods that the derived classes need to implement, but then I only need to implement component-specific behaviors when necessary.


Interface's mean that you can define methods which a class should implement without actually implementing them. An example of why this would be useful is to allow developers to write plugins for your application.

It must be clear which methods can be called on this plugin (such as load, unload execute for example) but how these methods are implemented depends on the plugin itself. At the time of designing the interface, you may have no idea what the code for those methods will actually be.

As far as abstract classes go, whilst similiar, are normally used as basis for similar classes. In the case of the Stream base class, it has a method for reading a block of bytes which it may do by repeatedly calling a method to call a single byte. If you have several classes which derive from this base class, you won't have to repeat this code in each of them; so abstract classes are often used to reduce code duplication


In simple, you use abstract class to model inheritance like Car inherit from Vehicle. Typically you will inherit some of the implementation or class definition too.

Interface is used to model types that may have similar subset of behaviour but not neccessary is-a relationship. Example will be I can have an interface IValidate which has a method Validate and this can be implemented by Car, Student, Account or etc to validate for the data they hold. These types cant/shouldnt really be modeled as is-a relationship

Interface is also used extensively too in language that dont support multiple inheritance as a workaround. Other uses include dependency separation, and exposing services in COM and WCF.

0

精彩评论

暂无评论...
验证码 换一张
取 消