0

My question is not direct difference between intreface and abstract. Please read out complete question

Also following below parameters to understand this : 1. Donot mention interface support multiple inheritance 2. Donot mention abstract class will have non- abstract method, fields

My interface is having only method and my abstract class is having only abstract method.

Is it anything related to "is-a" and "can-do" relationship ?Whats is difference?

sudhir
  • 33
  • 6
  • 1
    Have a look at this thread: http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo. In your special case a siginificant difference is that an abstract class can have a constructor (although it cannot be instantiated) to provide inheritance. – MakePeaceGreatAgain May 20 '16 at 07:41
  • Yes. This answer has not considered my parameter i have mentioned in my question – sudhir May 20 '16 at 07:47
  • You have to be carefull in order to use the term **inheritance** on interfaces. Interfaces do not provide inheritance (at least not in the meaning you think, of course you can simply *extent* an interface by adding members to it which is what inheritance means in this case). However you can *implement* an interface by creating an (abstrac) class which makes the class itself a "can do" instead of an "is a". So a class - abstract or not - sais what your object *is*, whereas an interface tells you what you can do with it. – MakePeaceGreatAgain May 20 '16 at 07:51
  • Conceptually, there are no differences beyond what you mentioned. Both are *is-a* relationships. A detail is that C# offers the extra feature of variance of generic type parameters in interfaces (and not in classes), but that's pretty much it. – Theodoros Chatzigiannakis May 20 '16 at 07:52
  • @TheodorosChatzigiannakis: class BaseNodeGeneric { } and interface IInvariant . I think it is possible. Cane you give some example of classes. Then why micorosoft bring this two concept? – sudhir May 20 '16 at 07:55
  • @HimBromBeere: what i can achieve through interface , i can achieve similar behavior with only abstract method except multiple interface implementation – sudhir May 20 '16 at 07:58
  • @sudhir You can't have `class BaseNodeGeneric { }` but you can have `interface IBaseNodeGeneric { }`, this is the difference I'm talking about. C# and Java interfaces are just language-level constructs of a specific C++ pattern of inheriting a purely abstract class virtually when using multiple inheritance. Generally, interfaces are recommended over abstract classes whenever possible. – Theodoros Chatzigiannakis May 20 '16 at 08:01
  • @Theodoros Chatzigiannakis: I think depend upon scenario what we should use it. Apart out keyword , do you have any differences ? – sudhir May 20 '16 at 08:05
  • @sudhir Beyond (a) multiple inheritance (b) implementation inheritance and (c) generic variance, which we have mentioned already, there aren't any other differences. In my opinion, you can definitely think of interfaces as abstract classes with abstract members only. And they do denote the same kind of relationship. – Theodoros Chatzigiannakis May 20 '16 at 08:12
  • @TheodorosChatzigiannakis: This is my understanding apart from above : :- Basically Abstract class is design for base class. Interface –similar type of subclasses can implement interface or not, if their behavior supports.In case abstract : all sub-classes have to implement the abstract class. – sudhir May 20 '16 at 08:15

0 Answers0