subclass not updating variable taken from base class

Why is my output not giving expected result i.e. 25? I know my question is silly but I am new in Java programming. the output is

run:
5
0
0
BUILD SUCCESSFUL (total time: 0 seconds)

But according to me expected answer is 5 as I have passed 5 in arguement.

class A {
    int a;

    public void setA(int a) {
        this.a = a;
    }    
}

class B extends A {    
    public int multi() {
        int multi = a * a;
        System.out.println(multi);
        return multi;
    }    
}

class test {
    public static void main(String[] args) {
        A obj1 = new A();
        obj1.setA(5);
        B obj2 = new B();
        int c = obj2.multi();
        System.out.println(c);        
    }    
}
Jon Skeet
people
quotationmark

why output is not giving expected result i.e. 25

Because you have two different objects, each with an independent a field. You're setting the value to 5 in one object, but then calling multi() on the other object, so it's using the default value of the field (0).

If you use the same object for both parts, you'll get the right answer:

B obj2 = new B();
A obj1 = obj2; // Now obj1 and obj2 refer to the same object
obj1.setA(5);
System.out.println(obj2.multi());

people

See more on this question at Stackoverflow