Wednesday, June 2, 2021

JAVA program to illustrate the keywords i) this ii) super (using inheritance).

PROGRAM :

//i) this

package com.company;

class thiseg

{

        int a;

        int b;

        thiseg(int a,int b)

        {

        this.a=a;

        this.b=b;

        }

 

        void show()

        {

        int z=10;

        System.out.println(this);

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

        }

    public static void main(String[] arg)

    {

        thiseg ob1=new thiseg(10,20);

        ob1.show();

        System.out.println(ob1);

    }

}

 

OUTPUT :





PROGRAM :

//ii) super

package com.company;

class A {

    int i;

    A()

    {

        System.out.println(" Super class Constructor");

    }

}

class B extends A{

    int i;

 

    B()

    {

        super();

        System.out.println(" Sub class Constructor");

    }

    B(int a, int b)

    {

        super.i=a;

        i=b;

    }

    void show()

    {

        System.out.println(" i of super class"+ super.i);

        System.out.println(" i of sub class"+ i);

    }

}

class supereg2

{

    public static void main(String[] args)

    {

        B ob1=new B();

        B ob2=new B(10,20);

        ob1.show();

    }

}

OUTPUT :



Labels: