2

I am confused about why I am getting "ABBA" as answer and why is the answer not "BBBA" a.c should have given me the value "B" but its giving "A" can anyone please explain why?

class main{
    public static void main(String []args){
        A a=new B();
        B b=new B();
        System.out.println(a.c+" "+a.getValue()+" "+b.getValue()+" "+b.getSuperValue());
    }
}

class A {

    protected char c='A';

    char getValue(){
        return c;
    }
}

class B extends A{

    char c='B';

    char getValue(){
        return c;
    }

    char getSuperValue(){
        return super.c;
    }
}
Federico klez Culloca
  • 24,336
  • 15
  • 57
  • 93
Alphonse Prakash
  • 616
  • 6
  • 13
  • Because there is no polymorphism for fields in java (only for methods): see https://stackoverflow.com/a/15513709/1041641 – TmTron May 07 '22 at 08:28

0 Answers0