all right so in the last class uh
we have seen uh inheritance which is one of the most important objectoriented
programming concept so today we will see some more Concepts which are also related to
inheritance so the mainly the topic is Method overriding method overwriting so this concept is
uh related to inheritance so let us see what is Method overriding and after that we will also
compare this with method of overloading okay there are two concepts method of overloading we
already discussed earlier which is related to polymorphism right method overloading related
to polymorphism means one thing can have many forms so the polymorphism we can achieve through
overloading and this overriding is related Rel to inheritance concept okay so what is overriding so
the overriding means whatever method we created in the class parent class the same method if you
again recreate in child class which is called overwriting suppose this is my parent class in
the parent class there is a method called M1 and there is some implementation and there are some
parameters and if you extend this parent class into child class this is my child class what will
happen normally this M1 method is also belongs to CH class right so the whatever the method is
implemented same method is also belongs to child class because after extending through inheritance
after extending whatever methods and variables are belongs to parent class and same variables
and methods also belongs to CH class same M1 one method is the same implementation right but
sometimes suppose after extending uh everything from parent to child class if you do not like
the implementation of this method so whatever the implementation they have done in the parent class
method if do not like this method implementation then what you can do is you can write the same
method again in the child class without changing any definition without changing syntax means what
type you should not change name of the method you should not change number of parameters you
should not change order of parameters data type of parameter everything should be same this
part is exactly the same okay so whatever method you already there in the parent class if you
recreate the same method in the child class but you should change the implementation part
only implementation the body of the method you will change so this process is called method
overriding so whatever me method we already there in the parent class if you rewrite the same
method again in the child class just by changing the implementation part which is called method
overwriting okay so in inheritance concept it will not be duplicated because if you write the
same method two times in the same class then it is duplicated okay for example same class if you
write M1 M1 with the same definition this these two are the duplicated but what we are doing now
is we are extending the C1 into C2 and in the C2 we are writing the same method with a different
type of implementation so that is the reason which is called over ring it cannot be duplicated so
writing means whatever the method which we already created in the parent classes after extending
into jail class if you recreate same method without changing the definition just by changing
the implementation part is called overwriting okay which is called overwriting so why this concept is
required in inheritance sometimes we may not like the implementation whatever implementation they
have already done the parent class maybe that a old implementation old logic so after extending it
if you want to change that logic or if you want to recreate this method according to your requirement
your own requirement you can do some change in the child class okay that will not impact again in
the parent class parent class method is Still Remains the Same only in the child class you are
changing the implementation of the same method this is called overwriting but what are the rules
of overriding here is in the overriding concept we should not change the Declaration part this
is called declaration suppose when I say void M1 here parameters and here the implementation so we
should not change this particular part so return type should be same method name should be same
number of parameters order of parameters data type parameters everything should be same only the
implementation should be changed then it is called as over raing okay simple example I'll tell you to
understand about the overrating in the real real example let's say parent is having some house
right and after parents have gone so the same property same house will come to the child so
after so many days or after so many years then what this child will do he will renovate the same
house that called renovation here so what exactly he is doing he's doing some customization or some
modification right because this becomes very old same thing overriding whatever methods are there
in the parent class we will just try to modify them modification in the sense the definition
we should not modify only implementation we have to modify in the CH class which is called as
overwriting concept okay so let me tell you with example we will try to understand where exactly we
this overring concept is required and how we can Implement along with the inheritance and this
overring is Possible only with inheritance at least we should have two classes one parent and
one jail class then only overing is possible if you have a single class if you create a method
with the same definition that will be duplicated these are duplicate methods but if you create
same method in the parent another method in the child class this is called overwriting so if
you want to achieve method overwriting at least two classes should be there one parent class one
child class without inheritance we cannot achieve overwriting so this concept is purely belongs
to inheritance purely belongs to inheritance with without inheritance without having at
least two classes you cannot achieve writing concept okay so is overriding done for a method
only in parent class yes so whatever methods are there in the parent class you can override
all the parents all the methods into chain class so whatever is there in the parent class
we can over into child class it can be method or it can be variable also suppose in the parent
class I created variable and I have and some data like this now same variable you can create
in the child class also you can change the data this is also variable overriding and if
you have a method in the parent class if you recreate the same method in jail class just by
change the implementation this is called writing concept okay yes so if you extend this child
class to another class yes suppose here there is a C1 and here C2 class here it is C3 class here
you can create one M1 method you can rewrite here and again you can rewrite here if you want so
you can rewrite you can overwrite this method n number of times in child classes anywhere
okay as suppose here you don't want to re re overrate here you can overwrite and the grand
you can override so in this hierarchy you can override this method anywhere okay this is a
concept simple concept you can you don't need to have only two classes more than two classes
also you can have but at least two classes we should have to achieve overring concept one
parent and one child should be there even if you have a more number of childs no problem if
you create a method here and this method we can override in the grand CH classes because this
method is belongs to every class right once you have here you can override it if you do not like
the implementation everybody's understood what is overrating so this third c will inherit which
method same method see until unless you change the implementation the method will be the same
don't confuse again this is again previous class concept see again I'm repeating just listen very
carefully so let's say have four classes here who created M1 method okay and this M1 method after
extending same M1 method will be there in C2 and again after extending same M1 method is C3 again
after extending again same M1 method will be in the C4 so here we were overring here we are
overring okay until unless you override this method the method implementation will be same
in C2 C3 everywhere it is same but here you're overring got my point same method one method
only so here this will not split into multiple classes same method is belongs to C2 same method
is belongs to C1 same method is belongs to C3 same method is belongs to C4 but once you overwrite
this okay the implementation will be changed okay the same method but implementation will be changed
so here this implementation will be changed so but when you create an object of C1 class and
access M1 method the old implementation will be executed if you create an object of C4 class
and then call M1 method the new implementation will be executed this is the concept okay now
let us see how can we achieve this overriding and where exactly it will be useful with
some example so go to eclipse and create new package and today is think day 15 now in this I'm going to create uh one new class
I will name it as over ring demo listen this carefully very important concept overriding demo
even in automation also it is most important I'm taking main method also in the same class if
you want you can take main method in another class no problem so this is the main class which
contains a main method and then I will keep this one side later I will use this for creating
objects and everything right so here I'm going to create one new class called Bank okay and
in this I'm creating One V one method return of rate of interest Roi and it will return double
so initially I'm rning zero this is the method I created in the bank class and when you call this
method it will return zero rate of interest and the return return type I say double decimal
number it will return you can put anything whatever method you want can just I'm creating
sample method so now I will create another class called ICI ICI this extends okay extends bank
so now I have inherited Bank class into is now is this method belongs to ICC or not yes so this
method is belongs to ISA class also right so now I will create another class another class let's
say SBI this is also extends Bank class now the same method is belongs to SBI also right SBA also
having same kind of a method now what I will do is in the parent class implementation is not suitable
for every bank if I look at here here rate of interest is zero but the rate of interest of
this bank and this bank will be different so this implementation is not exactly the same for ICC and
SBI so now what I will do I will again recreate the same method in the child class here and just I
will change implementation part here I will return 10.5 and again the SBI again I'm recreating the
same method but according to SBI I'm changing rate of interest if I look at here the same parent
class method I'm again recreating in the child classes just by changing the implementation so
definition it should be exactly the same it should not change but implementation you can change okay
so this is called overring so whatever method we created in the parent class same method again we
recreated in the child classes and IC and sbas are totally different classes there is no relation
between these two now what type of inheritance it is can anyone tell what type of inheritance
it is yesterday we discussed about four types of inheritances single multi-level hierarchy
and multiple which type of inheritance it is why it is multi what is multi-
level guys have you listen the session yesterday what is multi- level see multi- level means this is the one right
if I look at this structure is it seems like this no right so bank is a class which is extended into
icci and which is also extended into SBI what type of inheritance it is one parent can have multiple
Childs hierarchy inheritance it is not multi level okay multi-level means parent to child again this
child becomes a parent of another class here it is not like that IC SB there is no connection
both are having same parent so it is hierarchy inheritance simple question they have not able
to answer okay so we have override this method into child classes now come to the main class
main methods now here if you create object of this bank and you call this Roi method it will
return zero and if you create an object of IC and through that object if you call this Roi that
will return 10.5 and if you create an object of SBI class then if you call R then it returns
11.5 so whichever implementation you like you can create an object of that particular class and
you can call that method so if you just observe here I'm just creating an object of I I equal
to new I I now by using this object I'm calling Roi okay is it enough just calling method is
enough if you want to capture the data or if you want to get the return return value we
have to store it in another variable or we can directly print it here because this method
will not print it will just return the output system Pinel and i. Roi okay and similarly if
you if you execute this what is an output of this method you can say 10.5 why it is 10.5
because we created object of IC and through that object we have invoked R method so whichever
R method we recreated or we overrided inside this IC the new method will be executed the overed
method is executed always now if you create an object of SBI this particular overrided method
will be executed so I can say SBI SB equal to new SBI another object I created now sb. r i so this
will also the output s sp. Ry right so now when I call this Ry method from SB this will implement
or this will execute this is a method which is also overrided method 11. file as jav application
okay so 11.5 so this is simple concept of writing remember what is overwriting whatever method we
already created in the the parent class in the child classes wherever you want you want to
change the method not only immediate parent class you can change this in whichever class
you want after extending this class you can recreate this method in whichever class you want
in the CH classes okay and once you've overrided this method the implementation is got changed
so whenever you create an object of the class the new implementation will be executed okay
so this is called overriding concept and this concept is purely related to inheritance concept
inheritance means what there is only one parent at least one parent and child should be there
then only we can a overring concept even if you have multiple jail classes no problem you can
implement the method in multiple jail classes also wherever you want to recreate you can do
it but at least two classes we should have at least one parent and one jail class we should have
this is called overriding concept clear everyone but what are the rules we have to follow here we
should not change the definition part declaration we should this is called declaration this is
called implementation or we can say the body of the method okay the body of the method only we
should change in writing we should not change the Declaration part return type name of the method
number of parameters data type of parameters should be exactly the same you should not change
it as per overrating yes same definition different implementation okay understood everyone
so definition means exactly the same again parameters order of parameters data type
everything is part of the definition so definition should not change means what even the
parameters also you should not change here if you take two parameters in the CH class also should
take two parameters and here it is taking two integers here also it should take two integers
that's called overriding concept okay now let us compare writing and overloading very very
important and very confusing topics there is a lot of difference between overwriting and
overloading in the previous class we have seen overloading right what are the rules applicable
in overloading can we do overloading in single class can we do overloading with single class
yes can we do overloading with multiple classes in inheritance yes or no overring we already seen
in the inheritance can we do overloading here okay so we'll see with practical example overriding
along with overloading with inheritance okay with inheritance you will see overriding and
overloading so I will write some P of code here you guys can guess and uh tell me the answer
for that okay so let's create a new class and here I will name it as W uh loading versus
over tring I'm taking main method also just observe it's very simple concept right so I will just have this method in one
side overloading versus over loading now here I'm creating one new class just
observe very carefully creating a new class called ABC my class name is ABC so here I'm creating
one method called what M1 and here I'm taking one parameter okay one single parameter I'm taking
and inside this I'm just printing the value of a so this is a method which is belongs to class
ABC M1 is a method one integer parameter it is accepting no written value it is just printing
the value of a whatever parameter we are passing value same thing it is got printing I'm creating
another method void M2 this will also take one single parameter and this will print the value
of B so now I have created two methods M1 with single parameter M2 with single parameter both are
belongs to class ABC now I'll create another class called xyg and this xyg extends from from ABC
okay now how many methods are there in XYZ how many methods are there in XY Z two methods M1 and
M2 so whatever methods are belongs to parent same methods are belongs to XY Z also okay now what I
will do is I will just create a same method again in the child class I don't change the Declaration
or but I will just change only implementation here instead of printing just a I will print a star
value this a square value a into a a into a so whatever method we created in the parent class
same method again I recreated in the child class just by changing the implementation so what is the
concept it is over ring or over loading overriding or overloading this is overriding it is clear
overriding it's very clear so whatever method we created in the parent class same method we
recreated without changing the Declaration just by changing the implementation okay now in the
same class I will create another method with M2 M2 method and M2 is already there in the parent
class also now again I'm creating M2 here this time I will take two parameters in a comma int B
two parameters I'm taking and here I will print a into b or a plus b some operation I have taken
now just observe is there any difference between the parent M2 method and CH class M2 method yes
yes there is a difference now if I look at this how many methods are there in XY Z totally how
many methods are there in XY Z how many methods are there in XY Z total number of methods in XY Z
three two or four multiple options two methods or three methods or four methods how many methods are
there in XY Z total number of methods okay let us count so the parent class M1 which is already be
overrided okay this is one method and uh from the parent class M2 method is also belongs to XY Z but
again we again created another M2 method so if you recreate this method if you override this method
in the child class there will be only one method but we have not done over over in here so we have
done overloading M2 method is overloaded in the CH class okay so M2 method as it is it is belongs to
XY Z M2 is also belongs to XY Z M2 with one single parameter M2 with two parameters so there are two
M2 methods are there one M1 method is there so totally how many methods are there in XY Z three
methods are there okay so M1 method is only single one because we are overrided overriding means
old method is gone the new implementation only will be there so this is one method but this is
one M2 with two parameters another M2 one with single parameters comes from the parent class so
now is there any difference between this M2 and this M2 yes and now what is the difference you
have noticed between over riding and overloading the overriding we are not changing the decaration
we are just changing only implementation but in overloading we are changing the Declaration
okay so in overloading concept what are those rules only method name is same in overriding and
overloading what is the common point in overriding and overloading what is a common Point only the
method name is same that's it that's only one common point in writing we should not change the
Declaration we change only the implementation in overloading we should change the Declaration
same thing we again should not write right we will change the Declaration implementation is ours
implementation you can have same implementation or different type of implementation according to
the definition you can change the implementation so this is a overloading with inheritance So
within inheritance we can achieve overriding and also overloading both the concepts we can
achieve using inheritance now overriding is purely belongs to inheritance without inheritance
we cannot achieve overring but without inheritance we can achieve overloading right in the last
classes we already seen with single class we can create a number of methods with the same name
so over ring we can achieve only with inheritance overloading we can achieve with or without
inheritance with single or multiple classes also we can achieve overloading concept now
you understood the difference overriding is a inheritance concept overloading is a polymorphism
concept overriding we can achieve only if you have a two classes at least two classes or more
than two also fine with single class we cannot achieve overing but overloading we can achieve
with single class and also multiple class now come to the main class here I will create uh the
object of XY J just observe XY J equal to new XY J okay so is this clear uh is this okay can we
give XY Z lowercase characters as an object is it correct or not yeah because case sensitive
language right uppercase XY Z and lower case XY Z will treat differently so this is XY Z obj XY
Z obj now I'm calling XY Z obj just observe see there are three methods M1 with single variable
M2 with single variable M2 with two variables three methods it is populating M1 M2 uh one M1 M2
come from XY Z and M2 with single parameters come from ABC class now I'm calling first M1 method
if I pass one single parameter I'm passing then XY Z obj Dot and again I'm calling M1 M2 with
single parameter 200 or you can say two here can say 10 here can say 20 now from XY Z obj
dot again I'm calling M2 method with the two parameters 10 comma 20 okay so M1 is overrided
M2 is overloaded M1 is overrated same thing we declared and M2 is overloaded means there are
two M2 methods are there these two M2 methods are not same so we are able to call M2 methods by
passing different type of parameters so when you run this you will get the output like this
so here M1 method what M1 method we do here observe very carefully M1 method from ABC will
just print only a value but M1 method from XY G will print the square a star a into a so if
you look at 100 we are passing we are passing 10 here but we are getting 100 is an output
what does it mean which M1 is got executed M1 from XY Z class is executed that means overed
method is executed always whichever method is overrided the overrided method will be executed
suppose if you create an object of ABC class then you call M1 method which implementation
will be executed the old implementation will be executed if you create an object of XY Z and
if you call M1 method then overrided method is exec uted and now we call M2 method it is got
printed 20 as it is then we pass 10 and 20 it is added two numbers 10 + 20 30 so this is how
we can Implement over ring and overloading with inheritance okay so this is a concept so now
the question is can we overload one method in two CL classes without extending
these classes okay so let me tell you this so what you are saying is there are two
different classes you are seeing there is no connection between two classes so this is
a different class and this is a different class no inheritance right no extension means
no inheritance and here you created one M1 method with some parameters two parameters
here you create overloading is nothing but what you should change the Implement should
change the Declaration but here you can have three or four parameters so if there is no
connection between these two classes this is not called as a overloaded or overrided this is
not called as a overloading you can create same method or you can create a different parameter
same method with the different parameters we cannot call it as a overloading when we will
call overloading within the same class if you create another method with the same name
with a different declaration which is called overloading which is called overloading and if
you have a two classes and there is a extend connection and if you create a same method with
a different declaration that is also overloading but if there is no connection between these
two if you create a same method again or if you change the parameters we cannot say that is
overloading or overriding first of all there is no connection between these two classes right
so we cannot call it as a overloading or we cannot call it as a over reading first
of all because both are two different classes okay so we haven't changed implementation
of M2 with single parameter in XY Z class that means does it consider as overloading method yeah
see observe here we have override M1 method only M1 method we have overrided here M2 method we
have not overrided M2 is not overrided M2 is just overloaded if we still want to override M2
you can still do that can just copy this M2 one more time without changing the Declaration and
here you can change you can call B into B okay now here this M2 is wried Method here this M2
is overloaded method you understood now so this time we've overrided M1 and M2 also but we have
created another M2 with two parameters now this is overrided Method over M2 method this is overloaded
M2 method you can have both no problem everyone is understood this point there is no rule like
if you override we cannot overload like if you overload we cannot override there is no rule like
this depends on your requirement you can override and you can also overload same method just like
M2 here M2 method we overrided and also overloaded okay so this is how we can achieve this
overloaded and overring so now let us compare the points so I will put some points here and based
on that we will try to understand what is Method overloading what is Method overloading comparison
and interview perspective which is very very important what is the thing override and
overloading again you are asking the basic question in introduction I have told I
already told you what is overloading and why we need and what is overring and
why we need with the scenarios I have explained both are not same both are not same both are not same how both are same
see these are the differences listen this so method overriding Possible only in multiple
classes with inheritance we can achieve and method overloading is possible both single
and multiple classes major difference okay and method of overriding is related to what
inherent and overloading is related to what polymorphism and in the method overring we should
not change the signature of the method signature means what the Declaration part written type
name of the method order of parameters all those things but body we should change but in
overloading we should change the signature of the method major difference how come you
say there is no difference both are same thing no only one similarity is Method names
are same in overwriting in overloading the method names are same okay and the third thing
belongs to inheritance and this is a belongs to polymar okay remember this so everyone is understood what is the
difference between method overriding and method overloading in inter if anybody ask
you should able to answer that question what is overring what is overloading what are
the differences because many people will have confusion here a lot of people will confuse
between overriding and overloading overloading is belongs to polymorphism overriding is
belongs to inheritance but still we can achieve overloading with inheritance combination
and overing means same method again we recreate and change the body overriding means we should
change the Declaration of the method that is called overloading one thing can have many
forms but in overwriting we are not writing many forms only same thing we are rewriting
again and again we are not changing the form but in overloading we are changing the form
of the method form means the Declaration part can we access child class method from
parent class object no that is possible only with interface concept but here no if you create
a child class object you can access methods from only child class objects whatever methods and
whenever you say child class that is already extended method from the parent class itself
right parent class method already acquired into the child class so whenever you call
child class method obviously indirectly we are calling the parent class method only
the question itself is wrong you can just observe this is a parent class M1 method
we created now we create another CH class this M1 method is also belongs to here right
after extending so now if you create a child class object call M1 method this will able to
execute with the same object again if you call M1 method what does it mean actually from where
this M1 method is coming into CH class from the parent only right so when you call M1 method
this is got executing indirectly parent method only okay so the question is not correct
actually so the method is actually comes from parent class only so you can execute
the same method by using child class object or parent class object anything is fine but when
you do overriding concept you have to carefully do it when you override M1 into CH class if
you need new implementation then you have to create an object of jail class and then call
this M1 method if you want old implementation then you have to create an object of parent
class and then call M1 method if you have overrided otherwise you can create an object
of any of these classes and you can invoke M1 method okay Arjun are you clear about this point is it possible in multi-level inheritance
for overloading what we have seen is multile inheritance only multi- Lev inhance everything
possible everything is possible it is what type of inheritance doesn't matter in all kinds of
inheritances overloading is possible overriding is also possible in all kinds of inheritances it
is possible in single multi level higher Archy multiple everywhere overloading and overring
is possible so if you create a method M1 with one parameter here and if you create same M1
method two parameters here and if you create same M1 me three parameters here what's wrong in
this you can create n number of methods you can create but here we are changing the Declaration
so what type of concept it is overloading and if you create the same method no no change of
Declaration same method again multiple classes multiple definitions multiple declarations
we have given right multiple implementations we have given that's called overriding in every
class we can override if you want or the method M1 from this parent you can override in the
grandchild also directly you can over from here to here you don't need to touch anything in the
middle classes in all the things it is possible everywhere it is possible in every type of
inheritance overloading and overring is possible okay clear everyone so please confirm in
the chat box before moving to the next concept okay so remember this overwriting and overloading we finished now so we
will discuss one important keyword uh final okay uh before final let me tell one more
keyword uh just a second Super keyword okay super okay so let us start with the final so
then we will discuss super so final keyword so the final keyword we can apply for for variables
uh class variables and also methods all three levels so final keyword we can apply for variables
methods and also class can apply for variables we can apply for methods and we can also apply
for classes all three levels normally class contains what variables and methods right class
is a collection of variables and methods so the final keyword we can apply for variables
methods and class so we we can have final variables final methods final class but when you
apply final keyword what will happen what is an advantage of final keyword so basically the
final keyword uh is especially for uh if you want to apply some kind of a security or privacy
on the variables or methods in the class you can go with the final keybord okay so we'll see one
by one so first we will apply final keyword for the variable and then we will see the difference
and then we will apply on methods then we will apply on the class Final keyword first thing
uh let's create a new class so as I said final keyword we can apply on variables methods and
also classes create uh new class I can just name it as test okay take this or okay final keyboard
This is my class name and taking main method and say finish just observe very carefully so this
is a main class and main method and in this I will create a new class class called test and in
this I will create one variable in some x equal to 100 I'm hardcoding this data x equal to 100
right so now you guys can answer this question I'm creating object of test class test t equal
to new test create an object of test class can we access this variable directly from the
CL directly from the object yes or no can we access this variable X variable directly from
the object from the object yes obviously what's wrong in this we can access everything from
the class using an object right so simply we can object because this is a non-static variable
we have to access only through object there is no other way so I'm ask I'm accessing t.x okay
when I access t.x I'm just printing the value of t.x okay and uh this will print 100 as an
output okay this is clear we created an object of the test class and we have accessed
the value of x that means we are able to access this variable and we are able to print that
value of the variable can we change the value of this variable here initial value of x is
100 we are able to access it we are able to print it similarly can we change the value of
x can we change it or not so before printing t.x can we change why not we can simply change
if you're able to access you can change now I can change t.x equal to 200 here I'm changing
okay by using t.x equal to 200 and after that I'm printing t.x so observe here before changing
we got 100 now we got a 200 what is that mean we are able to access this variable and also we
are able to change the value of the variable through object we able to change the value of that
variable using object at any time we can change no problem because they are simply variable ables
the variable means what the value of the variable we can change at any time whenever you want okay
this is a default Behavior we can just create an object of the class through object we access
the variable and change the value now suppose when you create this variable as a final I'm
applying final keyword for this variable now here it is giving an error what it is an error
the final field test dox cannot be assigned and it is giving one suggestion also remove final
modifier of X it is asking us to remove final modifier of X what does it mean when you create
a variable with the final keyword that value is fixed we cannot change that value so before
applying the final we are able to change the value here it is working fine but after applying
the final keyword when you're trying to change the value it is not accepting so that is the reason
it is asking remove final modifier first remove final modifier and then you can make the change
but if you have a final keyword you cannot change the value got my point so the final keyword we can
apply on the variable if you apply final keyword on the variable we cannot change the value of
the variable that is always constant this is incorrect why it is incorrect because X is final
variable X is final variable so we cannot change the value of variable that's the first point so
when you apply final keyword for for the variables the value of the variable is constant we cannot
change the value of the variable is this clear everyone yes final keyword makes
mutable to immutable yes okay the value of the variable is fixed you cannot
change it anywhere else if you're trying to change this will give an error so
here it is giving an error the final field has cannot be assigned means
you cannot assign the value to the X forget about static I'm not discussing
static here later I will compare there is lot of difference every keyword is having lot
of difference guys don't compare the keywords you can always compare Apple with apple
orange with orange but you cannot compare with apple with orange okay so similarly there
are different keywords are there and static is totally different concept concept here final
is a totally different concept there is no relation between these two once you understand
the final keyword then we will try to compare okay don't uh introduce static keyword here
in in the in between final keyword okay so you will be confused so when explaining about
final just focus only on the finals don't uh again remember other type of keywords so final
variable means what the value of the variable cannot be changed that's it now let us apply
final keyword for the method the first point I hope everybody is clear we cannot change
the value of the variable which is constant now the second Point let us apply final keyword
for the methods then what will happen we will see okay so so what I will
do is I will create another class in Java there are so many keywords are there
five to 10 keywords are there so we can't compare them so see here uh I'm creating another class
uh say final keyword two taking main method and say finish okay observe first and you guys can
tell me the answer for that question so here I'm creating one new class test we already created
now I'm calling as a test one class okay in the test one I'm creating one method called void
y one method simple simple method I say so I just created some method let's say this is m
method from test test one class this is the implementation okay so in the test one class I
have created one method normal method now I will create a child class of test one class test two
which extends from test one okay now the method m is belongs to test two or not m is belongs to test
two or not yes obviously because we are extended test two from test one so whatever method is
belongs to test one which is also belongs to test two that is clear now can we override this
method in the test two or not can we override yes obviously we can override so let us try to
override this method vo M same declaration the body I will change so what I will change it here
this is is m method from test two class okay this is overed Method M1 we successfully overrated
from parent to child class we have overr fine now I will apply final keyword for this method
in the parent class I will apply final keyword for this method so as soon as you apply final
keyword here it is G giving an error in the child class what it is saying cannot override
the final method from test one and what is a suggestion is giving remove final modifier of test
1. Y what does it mean if you create a method with the final keyword we cannot override that method
in the jail class restricted okay if you don't want to override this method in the child classes
you can make that method as a final if you don't want to allow anyone to override your own method
you can just make it as a final that's it okay so if you make that method as a final method you
cannot override that method in the child classes so here this is incorrect why it is incorrect
because because m is final method m is a final method clear everyone so when I apply final
keyword for the variable the value of the variable is constant we cannot change when you
create when I apply final keyword for the method we cannot override those methods in the child
class we have restricted so we cannot override that method in the child class here it is given
giving an error if you remove final keyword we can override this method when you apply final keyword
we cannot override this method this is a second point so when you apply final keyword for the
methods we cannot override those methods in the child class this is the second point now we will
apply final keyword for the class then we will see so here I have a test one class here we have a
test two class and we are able to extend test one into test two there is no problem we are able to
extend when you make the class is final we cannot extend this class into child class if you don't
want to use this class as part of inheritance you can make it as a final so now here it is started
giving an error what it is saying the type test two cannot subclass the final class test test one
means we cannot make test two as a sub class of test one subass in the sense child class so we
cannot use test one as a parent that means once your class is created with a final keyword that
class we cannot extend into child classes any other child class we cannot extend this is also
restricted if you don't want to allow anyone to extend this class into their classes you you can
make it as a final so final classes we cannot extend into child classes this is another point
so this is also here it is incorrect why because why because test one test one is final class
if it is a final we cannot extend that class into child class this is a third point so when
you apply final keyword for the class we cannot extend that class into any other classes that is
called final keyword now you understood when I apply final keyword for the variables the values
of the variables are constant we cannot change when I apply final keyword for the methods those
methods we cannot override in the child classes when you apply final keyword for the class class
we cannot extend that class into child class simple okay overloading works as it is because
overloading we are creating the different methods right we are not recreating there so overloading
works as it is only overriding specifically I'm saying overriding did they say overloading here no
right so specifically over we cannot override the methods overloading will work as it is no problem
with the overloading and other things so when you apply variables when I apply final keyword with
the variable the values are same we cannot change the value of the variable and if you apply final
keyword for the methods we cannot override that method still we can do overload overload is a
totally different method we are creating all together right overloading and over raing in over
raing same method we are creating in overloading we are creating a different method as soon as to
change the parameters and data type of parameters order obviously that is a totally different method
so it is allowed so the methods we cannot override class means when I apply final keyword for the
class we cannot extend that class into child class simple okay so this is all about final keyword
different than so what is the context of static method static variables and static method so I'm
repeating this concept what is the context where we have to use static and non-static non-static
everything by default everything is non-static but when you create a static variables and static
methods what is a context where we will create static methods and static variables yes when
you find a variables or common the values of the variables are common across multiple objects
okay and the method implementations are same for almost every object then you can make them as a
static static means that is not fixed remember this see I have a static variable called x equal
to 100 I have a final variable x equal to 100 what is the difference between these two what
is the difference so static xal 100 means the x value default value is what 100 and even if
you create multiple objects in all objects the value of x is what 100 only in all the objects
x value is 100 only if you want to change this value you can change in the class level you
can change or by object you can access the x value and you can change by object two also you
can change by using object three also you can change this is always you can change but when
you say x equal to 100 you can even though if you create multiple objects you cannot change
the value this is fixed value that is a major difference okay this is a major difference
for the variables I'm talking okay static variable value we can change at any time
but final value we cannot change this is constant okay now compare with method suppose
I have method M1 which is static method and I have another M1 method which is just a final
method the context is totally different when we will apply static keyword for method again same if
the method implementation is same across multiple objects implementation again across objects we can
make it as a static and this implementation we can always change by using any of this object we can
change the implementation of this method whenever you want and you can also override this method you
can also overload this method in the CH classes wherever you want you can overload this method
you can also override this method static M1 you have created in the parent class same strategy M1
you can create in the child class also wading is also possible over loading is also possible every
time but final method is not like this overriding is not possible but overloading other things
are possible only overriding is not possible in the child classes but stating methods overriding
overloading everything is possible is there any difference or not please respond guys is there any
difference you noticed or not between static and final okay now we do not have a static class
static keyword can be used only for variables and methods we cannot apply for the class
but final keyword we can app for the class when you apply final keyword for the class
we cannot extend the class into child class okay static classes we do not have in the
Java static keyword is applicable only for variables and methods so now everybody is clear
the difference between static and final so both keywords are used in the two different context
so they are not comparable actually okay yes how can we static methods can be overden what can
do what's wrong in this whatever static method you created in the parent class see this
is our parent class you created M1 which is a static method right so after extending this
into child class so same static method is belongs to child class also right same static method
so you can just create again same method and change the implementation what is wrong in that
implementation you can change because the method implementations we can change wherever you want
through the object also you can change directly you can change the class or you can directly
extend it and change the implementation in the CH class everywhere it is possible even though it
is static method we can change implementation in the child class we can override you guys can try
all these combinations because everything is not possible to show in the sessions so you guys
can explore all these things it is possible invoke we use class. method name but there is yes
observe this at the time of invoking uh okay just observe this is our parent class in the parent
class there is M1 method which is a static now we have overrided this method in the child class
child class is also having same M1 method same static and there is new implementation this
is old implementation okay now what is the context here if it is a static method we can
directly call by using a class name without creating an object by using a class name we
can directly call M1 method right now if you call M1 with a parent like parent class name do
M1 then it will invoke the parent class method and if you say child class do M1 then it will
invoke the child class method okay here also we can invoke this this methods directly
by using a class name and by using object also we can access the methods we can change
the implementation of the methods just like a class name we are directly accessing right
because these are the common across all the objects so by using class name directly we can
access or by using object also we can directly access okay Arjun are you clear now so static methods also we can
override static methods also we can overload only final method
we cannot override that's only restriction for overloading there
is no restrictions every method we can overload okay so one last keyword
we will discuss and then we will finish so next keyword is is super keyword super
and this super keyword always we will use only in inheritance there is no other place you won't use
it only inheritance super keyword will work okay so what is the super keyword this is again related
to overriding concept and listen very carefully you have to uh pay you have to focus okay and
pay attention on this otherwise it will leads to confusion because we are discussing many things
almost similar topics but slight differences will be there just observe I'm telling the scenario
first and then we will see what is a super and also I will ask a question here you have to
answer this I have one parent class this is our parent class in the parent class I say xal
to 100 I extended this parent class into child class this is our child class in the child class
can we change x value here in the child class yes we can also override variable value I can make
it as a 200 okay so this is a scenario now I will create object of parent class through the
object if I get x value what is the value you will get parent. X means when I create an object
of parent class and accessing x value then you will get 100 that is clear now when I create an
object of child class and through that object if I access x what is the value will get 200 means
when you create an object of child class when you access x value obviously you will get the latest
value because X is 100 is already overrided in the child class so when you create an object of
child class through the object if you access x value you are getting the latest value by default
you will get the latest value overrided value but my requirement is even though I created an object
of child class instead of latest value I want to get the P immediate parent class value object is
what child class object only okay I will create an object for the child class but when you say objx
by default which value you will get 200 you will get by default but instead of 200 I want to get
the parent class value parent class value I want to capture but we should not change the object
object is referring to child only if you create an object of parent class obviously you will get
the x value 100 but here my requirement is what I want to create an object for only child class but
still I want to get parent value okay this is one scenario remember same thing I have a parent
class and in the parent class I will create one method called M1 and some implementation
is there again in the child class this is my child class I extended in the parent class
into child class here again I overrided this method with new implementation same method I
recreated overrided this method now when you create an object for parent class and call M1
method the old implementation will be executed when you create an object of child class and call
M1 method the new implementation will be executed but my requirement is I will create an object of
Ja class I will call M1 method but I would get the output from the parent class class immediate
parent class method I want to invoke through child class object this is my requirement how is
possible how it is possible so the by default the latest variable value we can access by default
the latest method implementation we can access if you create a child class object there is
a default Behavior but still I want to use child class object only but I want to get the old
implementation old variable Value Old in the sense the immediate parent class method implementation
immediate parent class variable value I want to get so this is possible by using super keyword
so super is always representing immediate parent class okay super keyword is representing immediate
parent class so by using super keyword you can access so you guys understand the scenario first
because once you clear about the scenario just a second hold on okay very simple guys if you create
object for parent class okay I can access only parent class variable and
parent class method if you create object of child class I can access child class
variables and child class methods right but what is my requirement is I will
create an object of child class through the topet I want to access variables and
methods from the parent class that is a context now you understood when you create an
object of parent class I can access everything from the parent class parent class variables and
parent class methods when you create an object of child class I can access child class variables
and child class methods but my requirement is if you create an object of child class through
the top I should want to invoke parent class variables and parent class methods means the
world implementation the world value of the variables that is a context okay so with
this ex with the example we will try to understand okay so same thing I will uh
replicate here you will understand by seeing the example so I'm creating
new class okay I'll name it as uh animal I'll name it as animal class so now my voice is clear everyone okay so I'm creating a new class called animal
and here I'm not taking any main method just click on finish so this is animal class in
this I will create one variable called string color equal to White okay I'm just creating one
variable called color and I'm giving some value called White also I will uh this is a class now
I will create one child class of this I can say class dog which extends from animal Now dog is
another class which is extend from animal so in animal class we have only one variable the
value of the variable is what white now here I will try to change the value of the variable
is it possible or not string color equal to Black is it is it possible or not yes because
we just overrided the variable value whatever value we have given in the parent class I don't
like that value so I can just change the value in the P child class now also I will try to print
the value of the color so I can say void display color and here I will just print the value of
the color okay now this is the implementation of the CH class observe now I'm creating another
main class I call it as a test super here I'm taking main method you can take the same class
no problem but I'm just taking another class okay just observe this we created Animal class
which is having one variable and the value of the variable is white next I extended the dog class
from animal and I changed the color of variable and just I created another new method which will
display the value of the color okay now come to the main class here I'm creating an object of
dog class just observe so dog D equal to new dog so dog D equal to new dog this
is a new class which we created dog D equal to new dog now by using
this D I'm calling display just a second okay so by using this D object
I'm calling display color now what is an output of this code when I run this
program what is an output you will get when I run this you will get a black
so obviously just a second you will get a black this is what we are expected right we are
created object of dog class and we are invoking the display color method and this will print
the value of the color so whichever the latest value we have assigned that is got printed fine
because the value is already overrided here so the new value we got it here now my requirement
is I still want to use same object I still want to call same display color method but display
color method is printing the latest value here right but instead of this latest value still I
want to get my V value from the parent class how it is possible when you call this method it is
just printing the latest value but still after overriding also I want to get my old value even
though we have done overriding still I want to get my old value value then what we can do we can
simply say super dot e like this we have to say so when you simply say only color the latest value
you will get when you say super do color you will get the parent class value okay what does Super
will do here is super keyword will invoke the parent class variable instead of the latest it
will invoke the parent class variable okay now if you run this as Java application see this time
you got white understood everyone so when I use super keyword for this variable it will get the
value from the parent class not overrided value if you just print print only color it will give
you the latest value this is the usage of super keyword for the variable we can also apply for
the method I will show you that now so is this clear everyone please confirm in the chart window
before going to the next so super keyword will be used to invoke the immediate parent class variable
that's the first point super keyword will be used to invoke the immediate parent class variable
now in the parent class I will create one method okay one method I say void eat eat and here I
will Implement something here I can say eating nothing I'm not specifying just eating this is a
method same method I will again recreate in the CH class is it overloading or overriding here
I'm changing eating bread eating bread is it overloading or overriding which concept it is
overriding right we have override this method into CH glass fine now let me call D do eat okay
now which method will be invoked D do eat I call eat method from the D object child class method
will execute so whichever new method we created whichever method we have overrided here this
is got executed by default right so when you run this you can see eating bread so the latest
implementation you will get by default okay but my requirement is I don't want to change the
object I will still call eat method but instead of the new implementation I want to execute my
world implementation I want to execute my old implementation then what we can do just comment
the statement and inside the same method you can just say super do e what we are doing here we
are calling Child method only but in the child method we are invoking parent class method we
are invoking parent class method okay now if you execute this will call child class method
only but it will try to invoke the parent class now you can see you will get the output from the
parent class method even though if you commented here uncomment you'll get both because by default
it is calling Child class method so eating bread is coming from here and super do e is invoked
parent class method so that is an output we are getting okay so you can just comment this
so the eat method sorry the super keyword can also be used to invoke the immediate parent class
method that is the second point the super keyword will be used to invoke the immediate parent class
variable the super keyword will be used to invoke the immediate parent class method immediate parent
class method so this is the purpose of using super keyword even though you created an object for
the child class still you are able to invoke the variables and methods from the parent class
that is possible by using super keyword super keyword is always representing immediate parent
class this this point is very very very important immediate ping class means what will it work
multi-level inheritance in multi-level inhance how many immediate appearent classes we will have
so this point is very important I'm not saying in every parent it will work immediate parent means
what if you look at multiple if you look at multi- level in a tense let's say this is one class this
is another class this is another class this is another this is multi level inheritance right
suppose you created variable here okay and here 100 and this variable here you have overrided here
you have overrided this variable now you created an object for this class and by using this object
you can obviously invoke this variable by default but if you look at here through this object if you
want to get this value will you able to get will you able to get no you will will not able to get
why because this is not immediate parent class of this one but which one is immediate parent class
of this one this is a immediate parent okay if you have a value here 100 then instead of this
you will able to get this value because this is the immediate parent but you will not get this
one or if you have here you will not get this one these two are not immediate parent classes
okay understood every one so in multi- level inheritance also it will work but you have to
take either these two or either these two or you take these two then it will work okay immediate
parent class variables immediate parent class method only you will able to invoke otherwise not
okay for example if you create an object of this class this class you can invoke variables and
methods from the immediate parent so you will able to get from here similarly if you create an
object of this class then you able to invoke the variables and Method from the immediate parent
from here okay so this is a concept in case of multiple there will be multiple parent classes see
there will not be multiple parents only one type of inheritance multiple parents will be there
that is that is what type of inheritance multip inheritance see only in the multiple inheritance
there is multiple parents like this no one will be immediate see here in multiple okay in case of
multiple there will be multiple parent classes okay here there are multiple classes multiple
parents and no one will be immediate so it will be not uh so it will not be applicable see all
three classes are immediate parents this one is immediate parent this is also immediate parent
this this is also immediate parent now you have some variable here a equal to 100 you have
another variable e equal to 100 here another variable a equal to 100 so first of all by using
a class we cannot create multiple parents in the last classes we have discussed this okay using a
class concept we cannot create multiple parents only in inheritance it is possible sorry only with
interface it is possible only with interfaces you can create multiple parents but in interfaces
we cannot use super keyword okay we cannot use super keyword with the interfaces super keyword
is always represent class not but interface okay if you have a multiple parents definitely they
are the interfaces there should not be classes okay so the super keyword is not applicable
for multiple inheritance if you have multiple parents if you have multiple immediate parents
not applicable only for class it is applicable okay so these are all Concepts which are related
to inheritance we have discussed so many things and practice it multiple times and get familiar
and understand all the concepts even in automation also these concepts are most important inheritance
is is very very important concept we cannot avoid in even on Automation and over raing over loading
is also most important okay and super final keywords final keyword is not that much important
because a development perspective these are all important interv perspective you have to know what
are all those things okay so practice these things multiple times understand the concepts and we will
continue in the next class so interfaces is the next session okay in the next session we will talk
about abstract class interface how can we achieve multiple inheritance by using interface so all
those things we will discuss in the next class okay yes so I'll stop here for today's session
and tomorrow or next class we will continue
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact