METHOD OVERRIDING

package method_overriding;
class A
{
int i, j,k=7;
A() // no need to make this constructor if we have globally defined i and j
{
i = 5;
j = 6;
}
void meth() //use of super keyword return program counter to here
{
System.out.println("The values i ,j and k are : " + i + " " + j+" "+k);
}
}
class B extends A //inheritance is neceszsary for overriding/calling the method by use of super keyword
{
void metd1()
{
super.meth(); //to refer to superclass here A i.e, method overridden
System.out.println("After method overroiding we are here "); //after returning from super class
}
}
public class Method_overriding
{
public static void main(String args[])
{
B b = new B();
b.metd1();
}
}

                   OUTPUT
The values i ,j and k are : 5 6 7
After method overroiding we are here
















No comments:

Post a Comment