Tutoriallearn.com
Easy Learning
Search Tutoriallearn.com :
class A {
int n;
void setdata(int k) {
n = k;
}
void showdata (){
System.out.println("Value of n = "+ n);
}
}
class B extends A
{
int m;
void setdata(int p, int q) {
n= p;
m = q;
}
// Following showdata() method is overriding showdata() method of super class A
void showdata(){
System.out.println("Value of n is "+n);
System.out.println("Value of m is "+m);
}
}
class demoOverriding {
public static void main(String args[]) {
B obj1 = new B();
obj1.setdata(10,20);
obj1.showdata(); // showdata() method of child class B is called
}
}
Value of n is 10 Value of m is 20In this program, showdata() method of child class B overrides showdata() method of parent class A.