OOP – What is virtual method?

  • Virtual method is a method whose behavior can be overridden in derived class.
  • Virtual method allows declare a method in base class that can be redefined in each derived class.
  • When a virtual method is invoked, the run-time type of the object is checked for an overriding member.
  • The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
  • By default, methods are non-virtual. You cannot override a non-virtual method.
  • You cannot use the virtual modifier with the static, abstract, private or override modifiers.
  • Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
  • It is an error to use the virtual modifier on a static property.
  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
Share this:
Share

Leave a Reply

Your email address will not be published. Required fields are marked *