Java Program having constructors in inheritance(using super keyword)…

Constructors in Inheritance (use of super keyword).
class ABC
{
                private int a;
                private int b;
                public ABC(int a,int b)
                {
                                this.a=a;
                                this.b=b;                             
                }
                public void display()
                {
                                System.out.println(“a = ” + a);
                                System.out.println(“b = ” + b);
                }
}
class XYZ extends ABC
{
                private int x;
                private int y;
                public XYZ(int a1,int b1,int x1,int y1)
                {
                                //to call base class constructor
                                super(a1,b1);
                                x=x1;
                                y=y1;                                    
                }             
                public void display()
                {
                                System.out.println(x);                  
                                System.out.println(y);
                                // to call base class method
                                super.display();
                }
}
class InheritDemo
{
                public static void main(String args [])
                {
                                XYZ x1=new XYZ(10,20,30,40);
                                x1.display();
                }
}

Add Comment