Super keyword

Example to illustrate the use of super keyword in invoking the constructor of the base class as well as an ordinary method in the base class.

class w1
{ int p,q;
w1(int p, int q)
{ this.p =p; this.q=q; }
void show()
{ System.out.println(“Value of p is : “ +p);
System.out.println(“Value of q is : “ +q); }
}

class x1
{ int r,s;
x1(int p, int q, int r, int s)
{ super(p,q);
this.r =r; this.s=s; }
void show()
{ super.show();
System.out.println(“Value of r is : “ +r);
System.out.println(“Value of s is : “ +s); }
}
class w1x1demo
{
public static void main(String[] args)
{ x1 x1 = new x1(10,20,30,40); x1.show(); } }
output :
Value of p is : 10
Value of q is : 20
Value of r is : 30
Value of s is : 40

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.