Access the instance variables by using „this‟ and super keywords.

 /* Access the instance variables by using super keywords */


import java.io.*;

import java.util.*;

class A

{

   int i=10,j;

   void showij()

   {

System.out.println("In class A, i = "+i+" and j = "+j);

   }

}


class B extends A

{

   int i,k;

   void showik()

   {

System.out.println("In class A, i = "+super.i);

System.out.println("In class B, i = "+i+" and k = "+k);

   }

}


class Super_Overriding_3B

{

   public static void main(String args[])

   {

Scanner s = new Scanner(System.in);

    A obj1 = new A();

    System.out.print("Enter i value : ");

    obj1.i = s.nextInt();

    System.out.print("Enter j value : ");

    obj1.j = s.nextInt();

obj1.showij();


    B obj2 = new B();

    System.out.print("Enter i value for class B : ");

    obj2.i = s.nextInt();

    System.out.print("Enter k value : ");

    obj2.k = s.nextInt();

    obj2.showik();

    }

}

Comments

Popular posts from this blog

Design GUI to handle Choice Control event.

Write a Java program that reads an integer number (between 1 and 255) from the user and prints the binary representation of the number. The answer should be printed as a String. Note: The output displayed should contain 8 digits and should be padded with leading 0s(zeros), in case the returned String contains less than 8 characters. For example, if the user enters the value 16, then the output should be 00010000 and if the user enters the value 100, the output should be 01100100 (Hint : You may use String.format() method for the expected output)