2

I am dealing with some past exam papers and here I am not sure of the output. I think I am not clear about extends and super.

public class Superclass{
    public boolean aVariable;

    public void aMethod(){
        aVariable = true;
    }
}

class Subclass extends Superclass {
    public boolean aVariable;

    public void aMethod() {
      aVariable = false;
      super.aMethod();
      System.out.println(aVariable);
      System.out.println(super.aVariable);
    }
}

I think that the second output would be true since it would refer to the super class and it is an object. However, I am not sure of the first output. Would it be just a value and print false or it is also an object?

Siddharth Kumar
  • 86
  • 2
  • 13

4 Answers4

1

The output will be:

false
true

Because in your Subclass aVariable is false by default (so assignation aVariable = false; is needless). Read more about Primitive Data Types default values.
And in Superclass you initialize aVariable as true by invoking the superclass' method using the keyword super: super.aMethod();. Read more about Accessing Superclass Members.

Take a look on demo.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • Thx for your demo but when I paste it to eclipse it just don't work and assumed that I do not have a main method and run main method from other classes as before – James Richard Lee Jan 05 '17 at 13:03
  • so, invoking super.aMethod(); doesn't change aVariable value to true on SubClass ? – Ankit Jan 05 '17 at 13:05
  • Press right click on your class name in the **Project Explorer** and select **Run As... → Java Application**. – DimaSan Jan 05 '17 at 13:06
  • @James: Yes you had made public class as Superclass but main method is declared on SubClass. If you remove public keyword on SuperClass, it should work – Ankit Jan 05 '17 at 13:06
  • @ankitjava `super.aMethod();` it changes. I meant that condition `aVariable = false;` is needless here. – DimaSan Jan 05 '17 at 13:08
  • so what effect does the first super.aMethod() in the subclass has? – James Richard Lee Jan 05 '17 at 13:13
  • `super.aMethod();` calls the `aMethod()` of the **Superclass**, where you assign its `aVariable` to true. That's why the second line of output if `true`. If you remove this line the output will be `false false`. – DimaSan Jan 05 '17 at 13:15
1

Since they're both scoped to their own class block, having them with the same name doesn't matter. As it looks like now, you set aVariable to false, the call to the super doesn't change that, except for creating another variable (new reference) with the same name and sets it to true. So the expected output would be

false
true
Rob van Dijk
  • 722
  • 6
  • 15
1

Output will be:

false
true

super.aMethod() will execute making aVariable = true of SuperClass

aVariable of SubClass will remain false.

Siddharth Kumar
  • 86
  • 2
  • 13
  • but why does the super.aMethod() before the println does not have an effect on aVariable? – James Richard Lee Jan 05 '17 at 13:10
  • 1
    as being explained above, super.aMethod() changes value of aVariable for SuperClass while value of aVariable for SubClass remains same – Ankit Jan 05 '17 at 13:28
0

I conducted scientific experiment (copy-pasted and ran) and it prints false true

More here:

If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?

Community
  • 1
  • 1
Marek Dudek
  • 171
  • 1
  • 13