What is the need to introduce interfaces when we already have our own super-set abstract classes?

Abstract classes contain both types of methods - abstract (not implemented), as well as specific (implemented) methods. Although the interfaces contain only not implemented methods. This means that interfaces are a subset of abstract classes. Then why were the interfaces introduced in C # (.Net)? In my opinion, there are two reasons for this:

  • To support multiple inheritance
  • Support inheritance for value types (structs) in C #.

Is there any other reason or some hidden concept that I am missing?

+4
source share
4

, .

( ) is-a. , , , Cat, Dog, Bird Fish, Cat is-a Animal, Dog is-a Animal ..

can-do. , (Invoice, Animal, CustomerProfile). (.. ), Invoice is-a Print , Invoice can-do Print, CustomerProfile can-do Print .

+7

( , , ). , .

, " ", , , , . , , . , . API , , . , , , . :

virtual void Add(T item);
virtual void AddRange(IEnumerable<T> items);

Add():

override void Add(T item) { Console.WriteLine(item); base.Add(item); }

, AddRange()? , AddRange() Add() , . , - (, AddInternal(), Add()), AddRange(), Add().

, , API. ( ), .

+1

: ( " ?" ), ( ) ( " ?" ).

, " ", , ( , CLR ).

" " "". object[] List<int>, delegate event, switch by type generics .., # CLR . .

, - , . # " ".

- , - "" , , -, - "" .

+1

; , , , .

, . , , , .

- , , , , . , . , - , . ?

, , , , ... !

.NET


, : , " " " " , , , , java #, / , ( ). , , , , , - , .

+1

Source: https://habr.com/ru/post/1534051/


All Articles