Method Overriding in Java
In this class, We discuss Method Overriding in Java.
The reader should have prior knowledge of methods in Java. Click Here.
We take an example and understand the concept of method overriding.
Example:
class A
{
int P;
void m1()
{
System.out.println(“class A”);
}
}
Class B extends A
{
int q;
void m1()
{
System.out.println(“class B”);
}
}
class test
{
public static void main(String args[])
{
B ob = new B();
ob.m1();
}
}
In the program, we have two classes.
Class B is extending class A.
class A has method “m1”.
Class B has the method “m1” same signature as in class A.
Methods having the same signature in super and subclass are called method overriding.
Method overriding is allowed in Java.
Two points to understand:
1) Suppose we need to use the method “m1” present in class A; how to access it?
Our next class will discuss how we access the method present in class A.
2) We created an object for class B
When we access the method ob.m1(), the method in class B is accessed.
The execution will check the methods in class B first. If not available, then check in super class A.
Link for playlists:
/ @wisdomerscse
Link for our website: https://learningmonkey.in
Follow us on Facebook @ / learningmonkey
Follow us on Instagram @ / learningmonkey1
Follow us on Twitter @ / _learningmonkey
Mail us @ learningmonkey01@gmail.com
On this page of the site you can watch the video online Method Overriding in Java || Lesson 42 || Java Programming || Learning Monkey || with a duration of hours minute second in good quality, which was uploaded by the user Wisdomers - Computer Science and Engineering 06 July 2023, share the link with friends and acquaintances, this video has already been watched 218 times on youtube and it was liked by 5 viewers. Enjoy your viewing!