Show the output of the following program class A int x 1 cl

Show the output of the following program. class A {int x = 1;} class B extends A {} class C extends B {int x = 2;} public class classTest {public static void main(String[] args) {A w = new A (); System.out.println(w.x); B u = new B(); System.out.println(u.x); C v = new C(); System.out.println(v.x); A [] a = {new A(), new B(), new C()); for (int i=0; i

Solution

classTest.java

class A
{//class A
    int x=1;
}
class B extends A
{//class B inherits A
}
class C extends B
{//class C inherits B
    int x=2;
}

public class classTest {//main class
    public static void main(String args[])
    {//main method
        A w=new A();//object creation
        System.out.println(w.x);
        B u=new B();//object creation
         System.out.println(u.x);
        C v=new C();//object creation
         System.out.println(v.x);
         A[]a={new A(),new B(),new C()};
       
         for(int i=0;i<3;i++)
             System.out.println(a[i].x);
         }
    }
  


output

1
1
2
1
1
1

 Show the output of the following program. class A {int x = 1;} class B extends A {} class C extends B {int x = 2;} public class classTest {public static void m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site