They define behavior that must be present, more like an interface does. SayHello is only hidden, so when I call that from my base class I get my original method.Ībstract methods are implicitly virtual. This is because SayGoodbye is virtual, and can be replaced by derived classes. If I call HelloGoodbye, I get "Hello" and "See you later". When I instantiate DerivedClass and call SayHello, or SayGoodbye, I get "Hi There" and "See you later". See the following example: public class BaseClass This opposed to not virtual methods, that can not be overridden but can hide the original method. If you do, you always get the new behavior. If you don't override, you get the original behavior.
The declaration is in the abstract class (and any class with an abstract method must be an abstract class) and it must be implemented in a concrete class.Ī virtual method is a method that can be overridden in a derived class using the override, replacing the behavior in the superclass. This abstract class then contains the abstract method (which must be overriden), and another method which contains the 'common' logic.Ī virtual method should be used if you have a class which can be used directly, but for which you want inheritors to be able to change certain behaviour, although it is not mandatory.Īn abstract method is a method that must be implemented to make a concrete class. However, if you have such a case, but you know that implementors of that interface will also have another common method (for which you can already provide the implementation), you can create an abstract class. In such cases, you can create an interface which contains a method with this signature. In some cases, you know that certain types should have a specific method, but, you don't know what implementation this method should have.
When you inherit from a class that has a virtual method, you can override the virtual method and provide additional logic, or replace the logic with your own implementation. The virtual method has an implementation. The abstract method has no implementation and thus, classes that derive from that abstract class, must provide an implementation for this abstract method.Ī class can have a virtual method. When a class contains an abstract method, that class must be declared as abstract. With a virtual method, you have a basic plan waiting for you, but can choose to implement your own if it's not good enough. What's the difference between the two again?Ībstract method: sub classes are forced to implement their own FireEscape method. This is what is meant by an abstract method. Management don't care how you escape, so long as you have a basic FireEscape() plan - if they don't you can be guaranteed OHS will come down on the organisation like a tonne of bricks. Another guy will use rocket propulsion technology to fly away from the building. In other words, each person is forced to develop his own FireEscape() method. Management are only interested in such a policy existing. They don't have an overall escape policy. Subclasses can override the parent class' virtual method if the programmer deems it appropriate. In other words virtual methods provide a basic plan, which can be overriden if you need to.
With virtual methods you can do just that: you can override the basic FireEscape() plan with your own version of the plan: But there is a 1% chance that the fire escape is blocked or damaged in which case you are completely screwed and you'll become toast unless you take some drastic action. This plan is pretty good for 99% of the circumstances. This is basically a virtual method called FireEscape()
But luckily we have an instruction manual somewhere here on what to do in case of fire: Every now and again, somewhere in the world, a fire is burning down a sky scraper.