Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Get the output of the following program. a) class A { public int i; public int j

ID: 3691315 • Letter: G

Question

Get the output of the following program.
a)
class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)   
}
}

b)
class A
{
public int i;
protected int j;
}
class B extends A
{
int j;
void display()
{
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;   
obj.display();   
}
}

c)
class A
{
int i;
}
class B extends A
{
int j;
void display()
{
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;   
obj.display();   
}
}

Explanation / Answer

Note:While executing all these three programs keep each program in a separate folder because there are duplicate classes of A,B in all the three programs .these classes will overide and causes error while executing these programs so keep each program in aseperate folder and then execute the programs separetely.

super_use.java

//rules of super keyword
//1) super.<variable_name> refers to the variable of variable of parent class.
//2) super() invokes the con//structor of immediate parent class.
//3) super.<method_name> refers to the method of parent class.

class A
{                         //class A super class
        public int i;
        public int j;
        A()//constructor with no arguments
       {
            i = 1;
            j = 2;
        }
    }
   class B extends A
{//class B sub class
        int a;
        B()
       {
            super();//this super keyword is used to class super class constructor
        }
    }
   class super_use
{
        public static void main(String args[])
        {
            B obj = new B();//creating object
            System.out.println(obj.i + " " + obj.j);//accessing values
        }
   }

output

1 2

output.java

//rules of super keyword
//1) super.<variable_name> refers to the variable of variable of parent class.
//2) super() invokes the con//structor of immediate parent class.
//3) super.<method_name> refers to the method of parent class.

class A
{//A super class
        public int i;//public variable can access any where
        protected int j;//protected varaivble can access with in the directory only
}
class B extends A
{//B sub class
        int j;
        void display()
       {
          // super.j = 3; (error)super keyword at variable level used to access the hidden variable in super class
            //we cannot assign value to super keyword super.j=3;
            System.out.println(i + " " + j);
        }
    }
   class Output
{
        public static void main(String args[])
        {
            B obj = new B();
            obj.i=1;
            obj.j=2;
            obj.display();//calling display method
        }
   }
output

1 2

inheritence.java

//rules of super keyword
//1) super.<variable_name> refers to the variable of variable of parent class.
//2) super() invokes the constructor of immediate parent class.
//3) super.<method_name> refers to the method of parent class.
class A
{
        int i;//A is super class
     }
class B extends A
{//B is sub class
        int j;
        void display()
       {
           // super.i = j + 1;// error because it is used to refer only variable at super class
            //super keyword at variable level used to call hidden variable at super class
            System.out.println(j + " " + i);
        }
    }
class inheritance
{
        public static void main(String args[])
        {//main method
            B obj = new B();
            obj.i=1;
            obj.j=2;
            obj.display();
        }
   }


output

2 1


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote