[Music] welcome to nourish technologies and bangarraju and in this video I am going to demonstrate about method hiding what is method hiding in my previous video I was demonstrating about method overriding what is method overriding method overriding is an approach of reimplemented a parent classes method under the child class exactly what the same name and signature exactly with the same name and signature so method overriding is an approach of reimplemented a parent classes method under the child class exactly with the same name and signature then what is method hiding method hiding is also same method
hiding you can also call it as shadowing there's a hiding is also called as shadowing method hiding or shadowing is also an approach of reimplemented really implementing a parent classes method under the child class exactly what the same signature exactly with the same name and signature exactly with the same name and signature then what is the difference what is the difference between the first one and the second one the difference between the first one and second one is in the first case child class range limits child class re-implement its parent classes methods which are which
are declared as virtual which are declared as virtual whereas in the second case child class can implement any parents method even if the method is not declared as virtual even if the method is not declared as virtual in the first case the child class re-implemented parent classes methods which are declared virtual whereas in the second case child class can reimplementation smattered even if the method is not declared as virtual that is the difference let me show you practically I'm going to add a new class in a project our sample project so in this I'm going
to add a class we'll in this class as parent class that is called s now in this class I want to define a method public virtual void just one a simple method like this and do remember this is a virtual method this is a virtual matter and this virtual method can be overridden under the child class can be overridden under the child class here will receive console dot the right line method test one from parent class this is what I am writing exactly in the same way I am also writing one more method public void
just to I am not using any virtual modifier on this one and will write the similar logically similar code here method just to from parent class so like this there are two methods now let us define a child class for this class child class : parent class class child class : parent class and remember once we inherit every member of the parent class will now become a member of the child class which can be called by using the child class instance so now create the child class instance and after creating the child class instance you
can directly call the two methods just one and just consult dot readline child cleanness execute yeah both the two methods are called and if you watch my video of method overriding the previous video there is I explained about why overriding why overriding in overriding parent class contains a method in it and the child class can consume that method parent class contains a method which can be considered by the child class and if at all the child class doesn't like doesn't like the output or behavior of the method child class has a chance to reemployment the
method so right now what's this the method to test one what the output is giving method test one from parent classes some output you're getting I did not like that output I want to change the output if you want to change this output you can overwrite this method you can override this method or you can really implement this method under the child class how is it possible like this public public in the place of virtual now right overwrite void your method is just one so inside of it I want to change my logic what I
want I'm copying this and pasting it over here and will change this as matter from test method test two from child class and if you run this now you watch the output the test one test one is not showing a method test one from parent method a test one from child class this is what we call re-implementing means if a parent class method behavior is not at pair of requirement is not at peril requirement I can change the behavior in the child class child class by using the concept of the reimplementation and these reimplementation is
done by using an approach called overriding okay how to override a method the parent class method declared virtual that method can be overridden this is what the first one method overriding is an approach of implementing a parent class method on the child class exactly the same name and signature okay but what is the method overriding sorry method hiding what is hiding listen now what's the second method what is the second method it test to now concentrate on the test to what is the output method it tested to from parent class you also want to change
it it method test to from child class I want to change this if you want to change this remember this method is not declared as virtual this method is not declared as virtual so what is the problem if the method is declared as virtual then only we can override the method generally we can overwrite the method but the method is not declared as virtual so over waiting is possible only on virtual methods you cannot perform overriding on this particular thing but still I have a chance to change the behavior of this material by using the
second concept that is called method hiding or shadowing means can i reemployment is method on the child class even if not declared virtual yes it is possible how to watch public new void test to public new wild attested oh let me copy this message coming over here and just pasting it here watch it doubt but now changed the output changed so both are doing the same thing only what they are doing reimplemented parent classes method under the child class but what is the difference in the first case you are oh you are able to reemployment
only virtual methods but in the second case you can reemployment any method you like you can reimplemented even if it is not a declared as virtual so here I am using method overriding and here I'm using method hiding our cherie matter hiding our shadowing that is the difference what I just shown for you in both these cases so these virtual matters can be overridden but these methods are just going to be re-implemented by using the concept of metal hiding now here you'll be having a question or the question here we use the new why did
we use new here we use the override why the override is a keyword what we use here to tell we are performing and overriding what the use of new SROI is new really Jie not required delete it now once it executes now also it executes you're getting the same output then why to use new if you don't use new here the compiler will give you a warning a green color underlined here will come and this green underline is going to be a warning what is the warning open the error list in this you can just
see a warning what are the warning the last one child class dot testator hides inherited number parent class star duster to use the new keyword if hiding was intended what a meaning of it see today we intentionally define the method the testicle which is present in the parent class we intentionally defined a method called it just to in the child class which is a defined already in the parent of parent class as it and again we have done it but tomorrow there is chance what is the chance when you are defining a child class you
have forgotten you have forgotten parent class contains a method called if testa two and you have defined a method called a testa to you don't remember that one we taught that you defined it so at the time the compiler is trying to warn you what is the warning array already you have a testator method in the parent class and this new method will hide it from now onwards parent class method will not be active the matter which will be active is only child method be careful so if you are mistaken if you're mistaken better change
the name of it no no you're not mistaken you are intentionally doing it tell me tell me you're in doing it how to tell by using you when I say new it's information to the compiler what are the information the programmer is intentionally trying to define another method with the same name that is present in the parent class that is why we use the new there if you don't use new also the execution takes place but when it is new also the execution takes place but only difference is a warning message that warning message will
not be sure for us in this context so that is why we use the new key body so this is the basic difference between method overriding and method hiding so now read the second point here in the first case child class 3 implements at parent classes methods which are declared as watch was which case overriding whereas in the second case child class can three improvement any parents method even if the method is not declared as virtual any parent matter can be re-implemented here even if the method is not declared as virtual so remember we can
we implement a parent classes method under child class by using two approaches we can reimplementation class's method under charge class by using two approaches the first approach is method overriding and the second approach is method holding out shadow these are the two approaches what we have for implementing a parent classes method under child class in the first case with permission in the second case without permission okay fine now in overriding or hiding in overriding and hiding after performing the real implementation a call to the method by using the child class instance a call to the
method by using a child class instance will always invoke the local methods in both the local methods mix now you see the output is coming from the child class methods not from the parent class if your did notary implemented delete the two methods now the output will come from parent class their simple logic lea the first preference always goes to local you are creating which class instance class child class instance and calling so first preference always goes to local okay now here the professor is always going to local now we'll have a doubt what are
the doubt is after reimplemented the parent classes methods in the child class child class instance is calling the local methods but it's required if required is there any chance for me to call parent class methods also means these two methods from the parent class i want to call them in the child class i want to call them is in some scenarios i want to use parent transmitted in some scenarios i want to use child class methods okay just watch this after re-implementing parent classes methods under child classes the child class instance will start calling the
local methods only that is that is the reimplemented methods the reimplemented methods but but if required in any value if requiring any case we can also call the parent classes methods the parent classes methods from child classes by using two approaches but if required in any case we can also call the parent classes methods from child class by using two a process then the chance of calling the parent classes methods from the child class how can i call option number one directly create the instance of parent class in charge class parent class P is equals
to new parent class and now P dot just one will call parent as method only and P dot does we call parent transmitter on see now these two will invoke parent class and these two will invoke the child class you can watch the output parent check this is one approach how we can go on so first one by creating the instance of parent class under child class we can call parents and methods from child class option number two by using the base keyword also we can call parents methods from child class by using the base
keyboard also we can call parents methods from child class but keywords like this and the base cannot be used cannot be used from static blocks so be careful this keyboard bass keyboard these keywords cannot be used in the static blocks so why I want you know see when I say we can call by using the base keyboard without a instance what you try you try like this you will not create a instance and in the place of the instance name you will try to write base dot test one and then base dot test that is
the reason for warning you why you can this is valid statement but the place where you return the statement is wrong why is it wrong because we cannot use it on the static blocks so what to do see generally to call this particular methods tomorrow we can just perform this one what is it I am writing a new material at public void D test one what are the T test one parent class test one my intention is parents test one okay otherwise let us call parent to test one and now here let us just write
base dot Testament and the same way public void parent tester and here also I am just going to call base dot test you'll be having a confusion now why did I define the two methods very simple these two methods in the child class are going to act as an interface in calling the parent classes methods from child class one what I'm telling you these two methods will act as an interface in calling the parent classes methods from the child class you can call the parent classes methods from the child class with the help of these
two methods by using the child class instance only by using the child class instance solely don't require to create the parent class instance now no need try to be you can watch C dot pair in to test one and the C dot pair into tester to if you just understand the child class instance will call pair into test one control jump SIA and this is going to call the base class test one baseless dismiss your parent class test one so ultimately which methods will execute the parent class method select security watch the first two are
calling the parent class and the next two are calling the child class methods okay these two will call child class these two will call bearing class so like this there is a chance of calling the parent class methods from the child class even after the implementation even after the implementation okay so this is the process how we are just going to perform the method overriding or the method hiding in this place now so method overriding method IDing both are very similar until now you notice that the difference we did not find any difference what is
that implementation wise we have a difference but in the way of calling and executing both are same what is this before implementation parent class methods are called after implementation child class methods are called and again we have two approaches to call the parent classes matters approach number one parent class instance approach number two this base keyboard so everything is working very similar in the two scenarios okay fine now let me explain in a small thing here what's what is the difference between overriding and hiding this is one doubt which will come into mind for us
okay I'll clarify you child Class C is equals to new child class what is the instance of C is instance of child class now parent class P is equals to C what is P P is it reference of parent class created by using child instance can I create a parents reference by using a child instance yes you can okay to understand this to understand this if you are not sure just watch my inheritance videos where three videos in the inheritance in the inheritance video the first video will demonstrate about this one a parent class reference
can be created by using a child class instance and right now both P and C will be pointing to the same memory location just a second C is an instance and P is a reference so here class 1 is our parent class here class 2 is our child class and if you'll notice this particular picture this is a memory which is allocated for the in staunton C and P is now accessing this memory he is now accessing this memory so this is a valid operation we can create a parent class reference by using the child
classes instance so the under person possible okay but do you another important point here what do you know in the inheritance video if you have just and watch that I demonstrated this how to create it but at the same point here there is one more thing to remember what you know using P I cannot call any members that are purely defined in the child class using this P I cannot call any members that are purely defined in the child class see this P can call any members that are defined in the child class this is
rule number three in inheritance please watch my inheritance video to know more about it okay now now coming back so using T I cannot call the child class members okay watch this calling T dot test - when I call P dot test to remember which method gets executed as per the point we discussed like which method parent class method or child class method parent why you cannot call the child class method you can notice it calling you parent method not the child method and at the same time I'm calling P dot the test one okay
which method has to execute again parent why we cannot call the child methods for long this and see the difference it is calling child class method it is calling child class method you will be having a doubt what is the doubt if a parent class reference cannot call the child class method how can it call the method test one it is not calling testa two but it is calling test one is the doubt what you will be having in the mind let me clarify a parent class reference even if creator by using a child class
instance cannot call any members that are purely defined in the child class okay at one time a parent class reference even if created by using the child class instance cannot access any numbers that are purely defined under the child class this is the rule number three of inheritance this is the rule number three of inheritance in my inheritance video just have a look on this okay but now if you cannot call any child class numbers how are we able to call the method a test one okay and now just see cannot call any one method
of purely defined number of a child class invited but can call overridden members of childless can call overridden members of child class means what it cannot call pure child class members cannot be called overridden members can be called so now understand the difference between overriding and hiding here in overriding parent class has given a permission for the child class for implementing the methods and when the child class really implement those methods parent class will identify them parent class will recognize them because the parent class recognizes it was able to call the child class but in
hiding remember without taking any permission from the parent class a child classes started reimplementation they call the child class as a starter implementation without taking any permission a parent reference will not recognize this so in this case which method gets invoked invokes child classes method and in this case invokes parent classes method this is the difference what we have between overriding and hiding because till now whatever I showed you in overriding and hiding both are same both are behaving in the same way but there is a difference what is the difference the difference is in
overriding in overriding a parent class reference can call child classes overhead and members but in hiding a parent class reference cannot call the child class method which is very implemented by using the hiding approach so cannot access any members that are purely defined under child class hiding a member that is a method that is hidden under the child class is considered as a pure child class member but in case of overriding that is not a pure child class member the member is implemented under the child class taking the permission from parent class because it has
taken the permission from the parent class a parent class reference will recognize it that a purely define a child class but can call overhead and members of child class okay because overridden members are not considered as pure child class members but but numbers which are reimplemented by using the approach of hiding or considered as pure child class members and not accessible to parents references that is the difference what we have between overriding and hiding thank you for watching the video for more videos please subscribe to our YouTube channel nourish IT [Music]