Java Essentials - Polymorphism function overriding in java
5K views
Oct 24, 2024
Java Essentials - Polymorphism function overriding in java
View Video Transcript
0:00
In this video we are discussing polymorphism function overriding in Java
0:06
In the previous video we have discussed the inheritance concept. So in case of inheritance the base class members will be inherited to the derived class
0:16
So sometimes it may happen that in the derived class where writing or defining one method
0:21
whose prototype is same or signature is same as that of the method which is existing in the
0:28
base class and that is a concept of function overriding and in this case what will happen in the
0:34
in the derive class we're going to define the method with the same signature which is existing in the
0:40
base class it will have a separate different function body so let us go for further discussion on it
0:48
what is function or method overriding so function overriding is basically redefining the class body
0:57
in the child class. So child class means the derived class. The function structure is same
1:03
that means the prototype, the signature of the function will remain the same, but the tax will
1:08
differ for the over functions That means the body of the function will be different but the signature or the prototype of the function alum the function name everything will remain the same if the current reference variable is referred to the child class object
1:24
then the compiler will automatically call the child class method that means from the object of the child class
1:31
if we call that respective method then the method which was defined in the child class that will be called not the
1:38
best class method to run the parent class method explicitly we can use the keyword super keyword and
1:46
the syntax will be like this that is a super dot method name so if i want to call the method which is
1:51
defined in the base class in the super class then you are supposed to call it explicitly using the
1:57
super keyword there is a super dot method name so in this way we can use the super keyword to call
2:04
the base class method also so let us go for one practical demonstration
2:08
on this concept for the better understanding in this demonstration we are discussing
2:13
method overriding in our Java so here we're having one class the name of the class
2:19
is animal only one method is there that is a movement and movement will have only one line statement that is system dot out or print alien animals can move This is about the DREP class that is dog and animal is the base class
2:35
Extents indicate that this particular dog is inheriting the properties from this animal
2:40
So extents is actually denoting the inheritance here you can find that this animal is having public void movement
2:49
Here also within this dog we're defining the same prototype function that is a public void
2:54
movement and that's why it is called method overriding because the same
2:59
method with the same prototype is being defined in the derived class also and it
3:05
is having the respective string to get printed using system dot out print
3:08
printallin frog is another derived class and which is extending this animal
3:13
class so this animal class is the super class also known as a base class and
3:17
this frog is the derived class also known as the subclass and here you can find
3:22
that it is also having the same method method same means the same prototype is there public void movement so here also the
3:31
method is getting overridden so now in this particular method we are having this
3:36
statement to get printed and that is another statement to get printed but in between we are calling the base class method that is a movement so base class whatever the name of the best class it will be referred as super
3:50
so super dot movement that means I'm calling this movement method so there is a
3:55
super dot movement that is a call the movement method of the parent class first and
4:00
then and then we're printing this statement that is the frogs can jump before
4:05
that calling movement method of the parent class will get printed so this is our override method class name of the file is override method and here we're
4:16
having the main function so animal having the object animal is equal to new animal
4:22
animal dot movement we are called we're defining this animal is equal to new dog
4:27
this animal is equal to new dog animal dot movement here animal is equal to new frog
4:32
and animal dot movement we have called it so at first we are calling this animal
4:37
movement then we have called the same but here this animal has got instantiated
4:43
with the dog class object and here animal has got instantiated with the frog
4:49
class object so now let us go for the execution you can find that animals
4:54
can move this particular line got printed from this animal movement here
#Computer Education
#Java (Programming Language)
#Programming