METHOD OVERRIDINiG
Whenever the method(functions) in the subclass has the same name and type signaure as that of super class then those methods are said to be method overriding.when those methods are called then the always shows the output related to the subclasses.
One example program will make you understand easily.
Class a
{
Int I,j;
a(int a,int b)
{
I=a;
J=b;
}
Void show()
{
System.out.println(“i=” +i+ “j=” +j);
}
Class b extends a
{
Int k;
B(int a)
{
Super(i,j);
K=a;
}
Void show()
{
System.out.println(“k=”+k);
}
Class c
{
Public static void main(String args[])
{
B ob1=new b(1,2,3);
Ob1.show();
}
}
After getting output what we will find that the method overridind has given the output which represents k.it overrites the value of I and j,so this is only called method overriting.
If you want to access the super class verson of overriting method ,you can do it by using super.
Here is the example program related to the problem
Class a
{
Int I,j;
a(int a,int b)
{
I=a;
J=b;
}
Void show()
{
System.out.println(“i=” +i+ “j=” +j);
}
Class b extends a
{
Int k;
B(int a)
{
Super(i,j);
K=a;
}
Void show()
{
System.out.println(“k=”+k);
}
Class c
{
Public static void main(String args[])
{
B ob1=new b(1,2,3);
Super.show();
Ob1.show();
}
}
With the help of this program we can be able to get the output from super class also.
Dynamic method dispatch
In the earlier virson of programming we show that in method overriding the method called by subclass are override the method of superclass and now we will learn that this method overriding forms the basis of java’s most powerful concept “dynamic method dispatch”.this is the machnism by which a call to method overriding is resolved at run time and not at compile time.
First of all it should be known to you that a superclass reference variable can refer to the subclass object.java uses this fact to resolve the call to overriding method at run time.If a superclass contain the method which is being overriding by subclasses then the different types of objects are refered through the superclass reference variables.
One example related to this concept with satisfy your doubt.
Class A
{
Void callme()
{
System.out.println(“inside a’s callme method”);
}
}
Class B extends A
{
Void callme()
{
System.out.println(“inside b’s callme method”);
}
}
Class C extends A
{
Void callme()
{
System.out.println(“inside c’s callme method”);
}
}
Class dispatch
{
Public static void main(string args[])
{
A=a;
B=b;
C=c;
A=r;
R=a;
r.callme();
r=b;
r.callme();
r=c;
r.callme();
}
}