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..

  • 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`. – Bubletan Apr 21 '18 at 02: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. – user207421 Apr 21 '18 at 03: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. – user207421 Apr 22 '18 at 21:00

3 Answers3

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.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
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.

Seymour Guado
  • 246
  • 2
  • 13
  • 'Static variables are not inherited in Java' is false. If you think otherwise please cite where it says so in the JLS. – user207421 Apr 22 '18 at 08:28
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)
}