C#C
C#4y ago
21 replies
Matt

❔ abstract class virtual methods vs default implementation interface

Okay so I’m a bit shaky on this but the way I understand it:


An abstract class tells the code that the implementation of the class is incomplete. Therefore, another class must inherit the abstract base class. By using virtual methods within the base class, these methods can be either overridden or implemented directly. These virtual methods are therefore allowed to have a method body. If an override is not defined, the derived class simply inherits the virtual method’s base class definition.

An interface provides a contract between the base and derived class that all methods must have an implementation. In C#8, an interface method is allowed a method body. If the method is not defined in the derived class, the default implementation is used.

So what really is the difference between the two? Are my definitions incorrect? and are fields easier to use in abstract classes than interfaces?
Was this page helpful?