0

I have following two classes.

   class A{
    int p;
    A(){p=100;}
    void printMe(){System.out.println("A");}
   }

   class B extends A{
    int p;
    B(){p=10000;}
    void printMe(){System.out.println("B");}
    }

When I create a object in this way,

    A c=new B();
    System.out.println(c.p);
    c.printMe();

I hope it would print 10000 as p value but it prints 100. Why is that?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
chamathabeysinghe
  • 858
  • 2
  • 13
  • 34

0 Answers0