Tech Point Fundamentals

Sunday, September 6, 2020

Virtual Interface Method | C# 8

Can we declare virtual methods in interface | C# 8


  One of the new and interesting features in C# 8.0 is the support for virtual extension methods which is also called default interface methods. Until C# 8.0, an interface in C# could not contain method definitions. You could only include method declarations in an interface, and the members of an interface were public and abstract by default. 




Today's Agenda:

    1. What is the keyword virtual means, and what is the use of it in C # 8?
    2. Can you have virtual interface members in C# 8?
    3. Can you have a virtual interface member without body definition in C# 8?
    4. Can you have private virtual interface members in C# 8?
    5. Can you have static virtual interface members in C# 8?
    6. Can a class override the virtual interface members in C# 8?
    7. Can you have an override keyword explicitly for an overridden interface member in C# 8?
    8. Can you make an overridden interface member as sealed in C# 8?
    9. Can you make a virtual member as abstract in the derived interface in C# 8?
    10. Can you hide the concrete definitions of base interface members in C# 8?


In this article, we will see all the above points with a live demo.


What is the keyword virtual means, and what is the use of it in C# 8?

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class or interface. So the implementation of a virtual member can be changed by an overriding member in the derived class or interface.


Whenever you are making any member as virtual, you must have to provide the default body definition for the same. Also, you can't use static, abstract, private, or override modifiers for any virtual member.


By default, all the members are non-virtual and you can't override a non-virtual member. When a virtual member is invoked the run time type of the object is checked for an overriding member and the overriding member of the most derived class is called. If no overriding definition is found then the default one will be invoked of the base class or interface.





Virtual Interface Member Example




Can you have virtual interface members in C# 8?

Yes, you can have virtual interface members in C# 8Any interface member whose declaration includes a body is a virtual member unless the sealed or private modifier is used. So by default, all the default interface methods are virtual unless the sealed or private modifier is used.


In the above example BaseInterfaceVirtualMethod(),BaseInterfaceDefaultVirtualMethod() and BaseInterfaceDefaultMethod() all are virtual methods.


public virtual void BaseInterfaceVirtualMethod()

{

Console.WriteLine("This is Base Interface - BaseInterfaceVirtualMethod()"); }



Technically there is no effect if you remove the keyword virtual from the interface member. They are by default virtual but you can have virtual keyword explicitly. If you want any member to be non-virtual then use a sealed keyword.


In the above example BaseInterfaceDefaultVirtualMethod() and BaseInterfaceDefaultMethod() all are virtual methods by defaults.






Can you have a virtual interface member without body definition in C# 8?

No, virtual interface members must have a body definition. If you are making any interface member as virtual then you must have to provide the body definition for the same.


In the above example BaseInterfaceVirtualMethod(),BaseInterfaceDefaultVirtualMethod() and BaseInterfaceDefaultMethod() all the virtual methods have default implementations .


Can you have private virtual interface members in C# 8?

No, you can't make any virtual member as private. Since virtual members are for to be overridden in the derived entity and by making private it will be no longer accessible to the derived entity.

But you can have protected, internal, and protected internal virtual interface members.


In the above example BaseInterfaceDefaultVirtualMethod() virtual methods is protected internal .



Can you have static virtual interface members in C# 8?

No, you can't make any virtual member as static in C# 8.




Can a class override the virtual interface members in C# 8?

Yes, the inheriting class can override the virtual interface member explicitly but not implicitly. Implicit overrides are not permitted. Also, remember that no access modifiers are allowed in explicit overriding.


void IBaseInterface.BaseInterfaceVirtualMethod() { Console.WriteLine("This is Base Class : BaseInterfaceVirtualMethod() overridden"); }






















In the above example, BaseClass has overridden the BaseInterfaceVirtualMethod() method of the inheriting interface.


Can you have an override keyword explicitly for an overridden interface member in C# 8?

No, you can't use the override keyword explicitly for any overridden interface member. You have to do the explicit implementation for overriding.


An override declaration is permitted to explicitly override a particular base interface method by qualifying the declaration with the interface name. Also, remember that you can't use any access modifiers in case of explicit implementation.


In the above example, BaseClass has overridden the BaseInterfaceVirtualMethod() method explicitly and IDerivedInterface has also overridden the BaseInterfaceVirtualMethod() of the inheriting interface explicitly.


Can you make an overridden interface member as sealed in C# 8?

No, override declarations of interfaces members may not be declared as sealed in C# 8.




Can you make a virtual member as abstract in the derived interface in C# 8?

Yes, this is called re-abstraction. A virtual (concrete) method declared in an interface may be overridden to be abstract in a derived interface.


abstract void IBaseInterface.BaseInterfaceDefaultVirtualMethod();


But remember if you are overriding any base virtual member as abstract then you can't provide any definition at all. The inheriting class must have to provide the definition for the overridden abstract member.


In the above example, IDerivedInterface has overridden the BaseInterfaceDefaultVirtualMethod() virtual method explicitly as abstract. Also notice that DerivedClass has provided the implementation for BaseInterfaceDefaultVirtualMethod() of IDerivedInterface.


This is useful in derived interfaces where the default implementation of a method is inappropriate and a more appropriate implementation should be provided by implementing classes.


Can you hide the virtual definitions of base interface members in C# 8?

Yes, if you override any base member implicitly then it hides the base member definition. If you are not providing the new keyword in implicit implementation then the compiler will generate a warning. You can use the new keyword to ignore the compiler warning.


public new void BaseInterfaceDefaultMethod() { Console.WriteLine("This is Derived Interface : BaseInterfaceDefaultMethod() hidden"); }




In the above example, IDerivedInterface has hidden the BaseInterfaceDefaultMethod() virtual method of the inheriting base interface by providing an implicit implementation.





Live Demo


Conclusion

    1. A virtual member may be overridden in the derived interface or class.
    2. You can make an interface member as virtual explicitly in C# 8.
    3. A virtual interface member must have a default body definition.
    4. A virtual interface member can't be private.
    5. You can't have any static virtual interface members.
    6. Inheriting class can also override the virtual members explicitly.
    7. You can't use the override keyword explicitly for overriding any virtual member.
    8. You can't make any overridden interface member as sealed in C# 8.
    9. You can make any virtual member as abstract in the inheriting interface.
    10. You can hide the virtual member definition by implicit implementation in the interface.

Thanks for visiting this page. Please follow us on Twitter, Facebook, LinkedIn and Pinterest for
regular updates.


Video Recommendation

Watch More Videos...

1 comment:

  1. The answer for below query is confusing as I could also override a virtual interface method implicitly in a derived class. I mean this method "BaseInterfaceVirtualMethod()" of interface "IBaseInterface" can also be overridden implicitly in class "BaseClass". Below answer however seems to be partially right for derived interfaces (IDerivedInterface) which can override a virtual interface method - only explicitly. Please explain...!

    Can a class override the virtual interface members in C# 8?
    Yes, the inheriting class can override the virtual interface member explicitly but not implicitly. Implicit overrides are not permitted. Also, remember that no access modifiers are allowed in explicit overriding.

    ReplyDelete

Please do not enter any HTML. JavaScript or spam link in the comment box.