Write an application that illustrates how to access a hidden variable. Class A declares a static variable x. The class B extends A and declares an instance variable x. display( ) method in B displays both of these variables.

 

AIM: Write an application that illustrates how to access a hidden variable. Class A declares a static variable x. The class B extends A and declares an instance variable x. display( ) method in B displays both of these variables.

 

CODE:

class A{

            static int x = 10;

}

class B extends A{

            int x = 20;

            void display(){

                        System.out.println("X of A: "+A.x);

                        System.out.println("X of A: "+super.x);

                        System.out.println("X of B: "+x);

                        System.out.println("X of B: "+this.x);

            }

}

class p20{

            public static void main(String args[]){

                        B b = new B();

                        b.display();

            }

}

                       

OUTPUT:



Comments

Popular posts from this blog

Write a program in Java to demonstrate single inheritance, multilevel inheritance and hierarchical inheritance.

Write a program in Java to demonstrate use of final class.

Write a java program to create 3 threads using Thread class. Three threads should calculate the sum of 1 to 5, 6 to 10 and 11to 15 respectively. After all thread finishes main thread should print the sum and average.