-1

If abstract and virtual methods are not implemented in Base class.What is the need to create them in base class in c#?

  • So you can call them. – SLaks Apr 20 '16 at 02:42
  • To enforce a contract that must (in the case of abstract) or may (in the case of virtual) be implemented by your inheriting classes. – David L Apr 20 '16 at 02:43
  • Why do not we create them in derived class only? – Chamanjit Kaur Apr 20 '16 at 02:49
  • So you can call them from an expression of the base class type. – SLaks Apr 20 '16 at 02:53
  • @ChamanjitKaur "Why not created them in the derived class only"? I mean, you could, but then there's no need for the abstract/base class anyway. I assume you're asking why you need to define it as an abstract method, rather than not define it at all. Because then you couldn't write a method which accepts the base class as an argument. For example `void DoSomething(BaseClass obj) { obj.Run(); }`. Marking the method as abstract means there will be a `Run` method, but the base class doesn't know *how* it's implemented, but it's gauranteed to be. – Rob Apr 20 '16 at 02:54
  • 1
    now i got that..Thank you so much – Chamanjit Kaur Apr 20 '16 at 02:57
  • @ChamanjitKaur No problem :) – Rob Apr 20 '16 at 03:00

2 Answers2

0

You should forget about virtual n abstract and do some abstraction excercises.

For ex, try describing the cars you see everyday and put it on a piece of paper. from there you can see common attributes and behaviors which will be defined on a base class.

dfdsfdsfsdf
  • 653
  • 3
  • 7
0

A virtual method must have some sort of base implementation which can be overridden by an inheriting class. An abstract method doesn't have an implementation but must be implemented by the inherited class in much the same way that a class must implement methods defined in an interface.

Dot net perls has a pretty good analysis of this exact questions

virtual keyword

abstract keyword

Dmitry K.
  • 972
  • 6
  • 16