1

In the below code, why is the super still referring to the subclass variable, and not the super class variable?

class Feline {
    public String type = "f ";
    public Feline() {
        System.out.print("feline ");
    }
}

public class Cougar extends Feline {
    public Cougar() {
        System.out.print("cougar "); 
    }

    public static void main(String[] args) {
        new Cougar().go();
    }

    void go() {
        type = "c ";
        System.out.print(this.type + super.type);
    }
}
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Tehreem
  • 476
  • 9
  • 23
  • Would I assume correctly that the program's output is `… c c `, but you expected `… c f `? – stakx - no longer contributing Oct 08 '14 at 16:51
  • Yes.... The output is c c, but i thought it should be c f. Why is the super.type, not calling the super class variable?? – Tehreem Oct 13 '14 at 04:27
  • When I declare a new variable in Cougar as `String type='c'`, then `f f` is being printed now instead of `c c`. I want to print `c f`, how do I do? Plus can you please tell me why both `this` and `super` are referring to the same thing? – Tehreem Oct 13 '14 at 04:46

4 Answers4

1

What subclass variable? You haven't declared one, so this.type refers to the superclass's variable.

If you declared public String type; in both the superclass and the subclass, it will work the way you are expecting. But right now, the only type variable you've declared is the one on the superclass.

Also, shadowing variables like this is bad practice, as it easily gets confusing as to what type you mean.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • No, this.type is referring to 'c', also super.type is refering to 'c'.... The output I am getting is 'c c', and not 'f f' or 'c f'... why is it so? – Tehreem Oct 13 '14 at 04:28
  • Yes... so its basically whenever we do super or this, it is used in inheritance and used for calling instance variables.. and when super variable and sub variable have same name its called shadowing.. thanks :) – Tehreem Oct 13 '14 at 09:21
0

When type='c' is done in the subclass, it makes the super-class variable hidden, so that the super class variable is no longer available. Hence both this.type and super.type returns the value available ie, 'c', as type='f' is not visible to the code. At the same time, if we change type='c' to String type='c' here we are creating a local variable, and not overriding the super-class variables. Hence the solution for

public class Feline {

public String type = "f ";

public Feline() {
    System.out.println("In 1....feline ");
}
}

public class Cougar extends Feline
{
public Cougar() {
    System.out.println("2.....cougar ");
}

public static void main(String[] args) {
    new Cougar().go();
}

void go() {
   String type = "c ";
    System.out.println(this.type + super.type);
    System.out.println("Subclass type::"+type);
    System.out.println("this.type::"+this.type);
    System.out.println("super.type::"+super.type);
}
}

The output is::::::: In 1....feline 2.....cougar f f Subclass type::c this.type::f super.type::f

In this case a new local variable is created, therefore the super-class variable is not hidden

Tehreem
  • 476
  • 9
  • 23
  • He has not declared subclass variable, hence no chance of super class variable hiding. – Shoaib Chikate Oct 13 '14 at 07:20
  • `type='c'` this hides the super-class variable and not `String type='c'` – Tehreem Oct 13 '14 at 07:29
  • when `type="c"` is done, the super-class variable gets hidden... but when we do `String type="c"` the super variable is not hidden – Tehreem Oct 13 '14 at 07:33
  • thats not declaration. Declaration means we are specifying type to a variable. for example. String type="c". And type="f" means using or changing value of previous declaration. – Shoaib Chikate Oct 13 '14 at 07:35
  • If you check his go() method it doesn't contain String type="c" but contains type="c". Big difference. – Shoaib Chikate Oct 13 '14 at 07:37
  • That is what I am saying! It the Original question there is no `String type="c"` but what we have is `type="c"`. And since `type="c"` is not a new declaration, what it is doing is for this particular sub-class, it is hiding the super-class declaration, thus changing the value of 'type' to "c". – Tehreem Oct 13 '14 at 08:54
  • While in this particular post(on which we are commenting), we are declaring in go()... as `String type="c"`. So what it does is, it creates a local variable for the sub-class, and the super-class variable is also there. – Tehreem Oct 13 '14 at 08:57
  • Suppose String type="f". Now JVM allocates memory say 2020(reference) to type variable when we call new Cougor() and stores "a" value there. Now when go() method is called, then JVM just updates that value of variable type="c" stored at 2020 reference location. So nothing is hidden, we cannot get previous value once go() method is called. – Shoaib Chikate Oct 13 '14 at 09:03
0

Firstly, you don't have subclass variable "type". Hence you are using super(parent) class variable(which is public available for both super and subclass) in the subclass. Hence when you change

type="c"

Super class variable is changed and hence this.type and super.type prints c

Hence in order to get your output, declare "type" in subclass.

public class Cougar extends Feline {
    private String type = "f"; //or something else and see the output.
}
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
  • when `type="c"` is done, the super-class variable gets hidden... but when we do `String type="c"` the super variable is not hidden – Tehreem Oct 13 '14 at 07:34
  • Hiding take place in case of static variable. And if non-static String type="somevalue" is defined in both super and subclass then we say subclass overrides superclass variable. In above case of OP, he has declared String type="c" in only superclass hence it is neither overidding nor hiding. It is called reusing the variable of parent(the main advantage of inheritance). – Shoaib Chikate Oct 13 '14 at 07:43
0

When the object of child class is created, the memory is allocated to instance member of the classes in the hierarchies.

There is only one copy in the memory for instance member type that is assigned for super-class but the visibility of that instance variable is public that why you can access it in child class as well using this.type but internally it's same location in the heap that is allocated for that object.

so this.type will change the value of instance member type defined in super class.

Summary: Child class is not defining a new instance member type instead it's inherited from super class.

Read What is purpose of using inheritance?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76