3

I have a code in which I expect the output to be different from the actual output.. As static variables are reference based, I expect the output to be "superclass" but what I am getting is "subclass".. Code:

class TestClass {
    public static void main(String args[] ) throws Exception {
        A b = new B(); // Since the reference is A, "superclass" should be the output
        b.test();
    }
}
abstract class A{
    static String a = "superclass";
    abstract void test();
}
class B extends A{
    static String a = "subclass";
    void test(){
        System.out.println(a); // Output subclass
    }
}

Please tell me where am I wrong..

3
  • 2
    If you wrote System.out.println(b.a) it would indeed print "superclass" as the variable b has type A (and thus b.a is same as A.a for static variables). You're referring to the variable a from a method inside class B, so it's equivalent to B.a. Commented Apr 21, 2018 at 2:41
  • There are a lot of answers here and elsewhere that claim that static variable are not inherited. They are all wrong, and this is easily demonstrated. Commented Apr 21, 2018 at 3:07
  • You have no business addressing that question to me personally. You have already asked the entire SO community. You also have no business addressing me as 'sir'. I am not a knight of the realm. Commented Apr 22, 2018 at 21:00

3 Answers 3

2

Static variables are not inherited in java. You varibale static String a is static which associates it to a class. Java inheritance doesn't work with static variables.

If you absolutely want the superclass variable you could use:

System.out.println(super.a);

Here is the inheritance what you probably wish to see:

abstract class A {
     String a = "superclass";

    abstract void test();
}

class B extends A {
    void test() {
        System.out.println(a); // Output superclass
    }
}

I remove the static identifier and removed the subclass's implementation of variable a. If you run this you'll get superclass as output.

Sign up to request clarification or add additional context in comments.

5 Comments

Sir can you please elaborate?
edited please see.
Thank you Sir.. But clear me this.. When we are calling static variable of class B "subclass" in this case, is it not violating the fact that static variables are reference based?
"static variables are reference based" I'm not sure what you mean by that? Maybe stackoverflow.com/questions/9898097/… would help?
'Static variables are not inherited in Java' is false. If you think otherwise please cite where it says so in the JLS.
0
A b = new B();

First off, static variables are not inherited in Java. This means that when you create your object as a new B(), even though that class extends class A, it won't keep the definition of your String a.

static String a = "subclass";

Secondly, even if that were the case, you're immediately overriding the value of String a at the beginning of this class B. You specifically set it to be "subclass" just before printing its value, so of course you've overriden the original value with this new one.

Finally, it would be a wonderful idea to try and name things with a little more variety. There being a class A and a String a doesn't help much for readability, for you or the people answering your question.

1 Comment

'Static variables are not inherited in Java' is false. If you think otherwise please cite where it says so in the JLS.
0
public abstract class A
{ static int a = 1; }

public class B extends A
{ static int a = 2; public A() { } }

public static void main(String argv[])
{
    A var1 = new B();

    System.out.println(var1.a)
    // Re-cast the variable to type "class B"
    // This is the SAME VARIABLE
    // It is occupying the SAME MEMORY SPACE
    // This println will produce the same output...
    System.out.println(((B) var1).a)
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.