Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java subclass constructor inherited member

I have a question as to why does this piece of code, when executed, prints out the value 0. I don't exactly understand what happens in the SubClass constructor, and why is it that when I erase the overridden method implicitValue, it prints out 10. Does the SubClass constructor make use of the SuperClass constructor? Thank you.

class SuperClass {
    protected int superClassValue;

    public SuperClass() {
        superClassValue = implicitValue();
    }

    public int implicitValue() {
        return 10;
    }

    public int getValue() {
        return superClassValue;
    }
}

class SubClass extends SuperClass {
    private int subClassValue;

    public SubClass() {
        subClassValue = 20;

    }

    public int implicitValue() {
        return subClassValue;

    }
}

class Example {
    public static void main(String argv[]) {
        SubClass ss = new SubClass();
        System.out.println("The value is " + ss.getValue());

    }
}
like image 880
ShadowStep Avatar asked Jul 09 '26 23:07

ShadowStep


2 Answers

TL;DR

Problem is that implicitValue from SubClass is used by superclass implicit SuperClass constructor (super()) via implicitValue() method, before subClassValue = 20; will be executed in SubClass constructor, so it returns default value of subClassValue which for int field is 0.


Does the SubClass constructor make use of the SuperClass constructor?

Yes, subclass constructor at start always invokes superclass constructor, so code

public SubClass() {
    subClassValue = 20;
}

is same as

public SubClass() {
    super();//superclass constructor
    subClassValue = 20;
}

But lets take a look at your code. You are printing result of getVlaue() method which exists in your superclass

public int getValue() {
    return superClassValue;
}

As you see it returns value of superClassValue. Before you invoke getVlaue() you are creating ss instance, so you are invoking code

super();//superclass constructor
subClassValue = 20;

which means you are invoking constructor of superclass which looks like

public SuperClass() {
    superClassValue = implicitValue();
}

so this.superClassValue is initialized with returned value of implicitValue() method, but because of dynamic binding (late binding) JVM will try to search for implementation of this method starting from actual class of this which is SubClass, and since this class has its own overridden version it will be invoked

public int implicitValue() {
    return subClassValue;
}

but subClassValue wasn't set to anything yet

super();// <-- we are still here
subClassValue = 20;// this line was not executed yet

so subClassValue still has its default value 0, which means that

superClassValue = implicitValue(); //will be initialized with 0;

so

public int getValue() {
    return superClassValue;
}

will return 0.

like image 138
Pshemo Avatar answered Jul 11 '26 13:07

Pshemo


  1. In your case: SuperClass constructor will be called by default.

  2. Logic for new SubClass().

It will call the constructor of the SuperClass first. When the constructor of the SuperClass is being called - A value return from the implicitValue() will be assigned to the variable superClassValue - The method implicitValue() being called is the method Of the SubClass (NOT the implicitValue() of SupperClass as you thought - Polymorphism feature of OOP).

When the implicitValue() of the SubClass is being called, the field subClassValue is not initialized yet so the field subClassValue still be ZERO. That is the reason you see Zero in the output.

  1. When you remove override the implicitValue from the SubClass. the implicitValue() being called is the implicitValue() of the SupperClass --> This is why you see 10 in the output.
like image 29
Loc Avatar answered Jul 11 '26 14:07

Loc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!