51 Program to demonstrate Overriding in Java

Code:

class SuperClass
{
int a,b;

SuperClass(int a)
{
this.a = a;
b = 20;
}
void display()
{
System.out.println(" a =  "+a);
System.out.println(" b =  "+b);
}

}

class SubClass extends SuperClass
{
int a,b;

SubClass(int a ,int b)
{
super(a);
this.a = 30;
this.b = b;
}

void display()
{
System.out.println(" SuperClass:  ");
super.display();

System.out.println(" SubClass:  ");
System.out.println(" a =  "+a);
System.out.println(" b =  "+b);
}


}

class Overriding
{
public static void main( String args[] )
{
SubClass   s2 = new SubClass(10,20);

s2.display();

}

}


Output:


Previous
Next Post »