[Music] welcome to Nares technologist I'm bangar Raju and uh in this video I wanted to demonstrate about method overriding in cop how are you going to perform this method overriding so what is this method overriding method overriding is an approach of um reimplementing a parent classes method under child class exactly with the same definition what's it it's an approach of reimplementing apparent classes method under the child class with the same signature so what exactly mean to say is there is a class called class one in this assume like we have a method called as a
test we have a method called a test that doesn't take any parameters now I have class two which is going to inherit from class one which is inheriting from class one this is class two inheriting from class one and once class two is inheriting from class One automatically the test method is inherited to class 2 now in class two again I can define a method exactly with the same name and signature if we try to do this this is what we call as method overriding so what is it it's an approach of reimplementing a parent
classes method under the child class with the same signature generally we have a confusion what are the confusion what is this difference between overloading and overriding let me clarify overloading and overriding actually if at all we have overloading on one side and overriding on the other side what is the difference between these two in this case is we Define multiple methods with the same name by changing their parameters by changing their parameters in this case we Define multiple methods with the same name by changing their parameters but what about this in this case we Define
multiple methods with the same name and same parameters there the first difference what we have between overloading and overriding in the first case multiple methods will be having the same name by changing the parameters we Define but here multiple methods means we are going to define the methods with the same name and same parameters second difference this can be performed either within a class as well as between parent child classes also this can be performed either within a class as well as between parent child classes also means within the same class you can overl methods
as well as between the parent child classes also we can overload methods in the sense suppose here I'm going to define a method called show and now coming to the child class also I can define a method called show with a different parameter like this in time or otherwise strings see this can be done within the same class as well as between parent child classes also see right now this method is overloaded in the child class by changing the signature we can do it within the same class as well as between parent child classes also
but when you come here this can be performed only within the only between parent child classes can never be performed within the same class this can be performed performed only between parent child classes can never be be performed within the same class so the point here test method here test method here parent child we can do it only between the parent and child we can never do this within the same class within the same class if you try to define a method like this ambiguity arises so this is not allowed this can be done only
between the parent and child classes okay and the next difference what we have here is while overloading a parent classes method under the child class under the child class child class doesn't require to take any permission from the parent class child class doesn't require to take any permission from the parent class but remember while overriding a parents method under child class child class child class requires a permission from its parent permission from its parent let me show this practically what I mean to tell is suppose in my project I'm going to add a class I'm
going to name this class as load parent load parent in this class let's define public void show I'm going to define a method like this console dot right line pay parents show method is called parents show method is called so this is what I'm just implementing over there next public void test console. right line parents test method is called I have two methods in this class now let us Define a child class for this class load child colon load parent I'm inheriting once I inherit once I inherit the parent class members will automatically become the
child class members static void main create the child class instance load child load code child C is equals to new load chck creating the instance and once after creating the instance once after creating the instance I can call the methods now C dot the first method is a show and the second method is test I can call both the two methods let me go to that execute yeah both the two methods are giving the output for us now the two methods are inherited to the child class fine now the first method what the first method
show I wanted to overload this method under the child class so watch public void show in I console dot right Line This is a child class test method is called the method name is show I'm sorry this is show so in the first case in the first case what we have is parents test method called and here I'll change the messages h child's show method is called okay this show method parents show method is called and child show method is called see we are overloading the show method it can be done within a class it
can be done between parent child class also see this is a parent class under which I have the show method and is a child class again I have a show method I overloaded and now you can come here you can see two show methods available to you we can watch two show methods that are available here one is in the parent class and one is in the child class two show methods fine here I'm just going to pass a integer value and now you can watch two show methods getting called and understand this while overloading
a parent classes method under the child class child class doesn't require to take any permission from the parent class that's what I showed you I did not take any permission from the parent class directly I came to the child class and implemented the things but if you want to override a parents method under child class child class requires a permission from its parent if you want to override a parents method under the child class child class requires an ex permission from the parent what the meaning of it watch this method I wanted to override in
the child class this method I wanted to override in the child class can I override I cannot why if we want to override if we want to override a parents method under the child class first that method should be declared by using the virtual modifier in parent class this is the first important point if you want to override a parents method under the child class first that method should be declared by using the virtual modifier in parent class what I mean to tell is see this method I want to override here okay so this is
a p method now I'm writing a public virtual void test this is how the method should be declared if I want to override so using virtual here what is the meaning you know this method is overridable this method is overridable so remember this method is overridable means the parent class is giving a permission to the child class to overwrite the method can the child class overwrite the method yes it can yes it can overwrite the method okay remember any virtual method of the parent class can be overridden by the child class any virtual method of
the parent class can be overridden by the child class if required by using the override modifier any virtual method which is present in the parent class can be overridden by the child if required by using the override modifier in the sense you can override here this is not a mandatory this is only optional that's why I said you if require and also here can be overridden not must be can be overridden by the child if required by using the over now can the child class override it how public override vo test what the child class
is doing overriding this is what this is how the overriding is performed so you need a permission from the parent how the parent gives a permission by declaring the method as virtual watch now this test method I want to over in the child class if that the case first year this method should be declared by using virtual modifier once we use a virtual modifier what I said this method is overridable but it overridable not asking you to override for sure you can override you may override you may not override it also see I did not
override right now and directly calling the test method now it'll call the parent class test method now concentrate on the test method method is calling the parent class test method I can do it not mandatory if I want I can do it so right now let us do it now I will override this method now okay so public override void test public override void test I'm just trying to call and here I'm writing what console. right line child's test method is called Child's test method is called so right now you can see now you have
implemented a method right now uh the virtual method is going to be reimplemented in the child class now and now again what's the output of this see we don't have two test methods one test method only is present so here it won't show you two test methods when you try to call one only present run and what's the output child's test method is called what is that earlier it showed parent test method called yes that is what overriding means overriding is basically reimplementing a parent class method is reimplemented under the child class here okay see
the fourth difference this will make you much more clear overloading is all about defining multiple behaviors to a method what the meaning of it it's all about defining multiple behaviors to a method what it is you see the show you have two different behaviors for this this is giving you one output and this is giving you another output two different so like this you can have different different behaviors for the particular method multiple behaviors but when you come to overriding overriding is all about changing the behavior of parents method under child class this is not
multiple Behavior changing the behavior what are the changing the behavior see when I did not Define the method in the child class when I did not Define the method in the child class or overhead method in the child class you run this test is calling parents test method which method is executed parents test method but when I overwrite this now in the place of parent a test method now it is showing you child's test method that is what we are changing the behavior basically here it is not multiple behaviors it is completely changing the behavior
so more clearly telling about this particular thing simply parent class defines a methods in it parent class defines a method in it defines a method in it and gives it to the child for consumption gives it to the child for consumption and telling to the child class you can call this method or consume this pent at the same time parent class is also giving a permission to the child class what permission if you don't like the behavior or output of the method if you don't like the behavior or output of the method I'm giving you
a permission change it according to your requirements what is that see the implemented logic is not satisfactory to the child class child class has a chance to reimplement this that is what we have done right now see already the method is implemented in the parent class with some logic and right now by default that logic is executing for us you can see I did not like this logic I did not like this logic I don't want this logic I did not like this you did not like the logic okay don't worry you have a chance
to change the logic how how is it possible like this how is it possible it is possible by override in the method now see I am changing the existing Behavior child's test method but you come to show you have two behaviors two outputs means two different behaviors but here we don't have two different behaviors it is only one Behavior what we just have either parent or either child anyone only will come into picture for us so this is what we just have simply here it will never tell you to override the method if you want
you can do that otherwise you can simply ignore it otherwise you can simply ignore it suppose your friends on your birthday has given you a gift your friends has given you a gift okay like a mobile like a mobile phone or something is given for you as a gift now okay and after giving the gift they just ask your opinion did you like it if you like it okay no problem or you don't like this you don't like this don't worry if you don't like this you have a chance to exchange it you have a
chance to exchange it so here is the bill now take this bill now and go and exchange and get whatever you want so now you have two options before it what is this you can use what is given by your friends as it is or your parents as it is not a friend here let us take parent okay your mother or your father is giving you a mobile phone now to consume on your birthday okay or giving you a mobile phone and your father mother just telling you and mobile phone we got it if you
like it okay not an issue if you don't like it you can go and exchange and get whatever you want okay now you have two options now what are the two option option number one if you like it use the phone what is given by your parents as it is no no no you did not like it you did not like it then go now and exchange it and get whatever you want so you have two options before it this also exactly same the parent class defined a method in it and given to us for
consumption given to the child class for consumption now the child class can consume the method as it is or reimplement the method also because it has given a permission how by declaring it as virtual and this virtual is a permission to the child class to override and the child class is overriding you can understand this what am I doing here overriding and one more thing the shria shria what we doing overloading parents show method in child class overloading parents show method in child class and what are we doing here overriding parents test method in child
class so basically the four differences what we just have you first difference what's it in this case we Define multiple methods with the same name by changing their parameters in this case we Define multiple methods with the same name and same parameters name will be same parameters will be same second difference this can be performed either within a class as well as between parent child classes also but this can be performed only between parent child classes can never be performed within the same class next while overloading a parent classes method under the child class child
class doesn't require to take any permission from the parent class no need to take permission from the parent class but while overriding a parents method on child CLA child CLA requires a permission from its parent how will it take permission how the parent will give a permission the parent will give the permission by declaring the method as virtual if declared as virtual that is a permission given by the parent to the child class to overwrite the method and finally the last one overloading is all about defining multiple behaviors in the sense we have multiple methods
which method you want you can just call the appropriate one but overriding is not a multiple Behavior changing the behavior all about changing the behavior the parent has a method behavior and that is reimplemented or that is completely changed under the child class that's what we call reimplementing this so this is how we can apply overriding in our applications uh in my next video I will just demonstrate a live example how we can just use this overriding in this okay thank you for watching the videos for more videos please subscribe to our YouTube channel Nares
[Music] it