Method Overriding
- If a parent’s class method is re-implement under the child class exactly with the same signature we called it as method overriding.
How to override parent’s class method under child class?
- To override a parent’s class method under child class, first that method should be declared by using virtual modifier in the parent class.
- Declaring a method as virtual under a class is going a permission for its child classes to override the method.
- Every virtual method of a parent class can be overriding by its child classes by using the override modifier, if required.
Note : In method overriding parent class define a method in it and gives it to the child class for consumption and at the same time declaring that method as virtual it is giving the permission for child class for changing the behavior of that method if,the child class is not satisfied with the parent class method behaviors.
Add the class ‘LoadParent.Cs” and write the following code
Add a class "LoadChild.cs" and write the following code
In the above case the Test method of the parent class is overload under the child class and now we are using the child class instance we are able to call both parent and child classes methods, because in overloading all the methods can be used.
In above example we override the Show () method under the child class, now in place of parent Show () method, child class Show () method get executed.
No comments:
Post a Comment