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 the use of 'final' keyword in the field declaration. How it is accessed using the objects.

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

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