-6

Why can't I access the parent class variable with the super keyword?

With the below code, the output is:

feline cougar c c   
class Feline {
    public String type = "f ";

    public Feline() {
        System.out.print("feline ");
    }
}

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

    }

    void go() {
        type = "c ";
        System.out.print(this.type + super.type);
    }

    public static void main(String[] args) {
        new Cougar().go();
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
immortal
  • 19
  • 7
  • 1
    You *are* accessing the parent class variable. The one that you just set to `"c "`. – shmosel Aug 18 '17 at 19:37
  • I was thinkin that he f should be accesed from class feline – immortal Aug 18 '17 at 19:40
  • 1
    There's only one variable, and it can only have one value. That value was `"f "` until you changed it to `"c "`. – shmosel Aug 18 '17 at 19:41
  • @shmosel: That's what it looks like to me. That's what all of the other duplicates state it is, too. I want to see a convincing argument as to what you think it is instead before you peter out and not reopen the question, though. – Makoto Aug 18 '17 at 19:45
  • if I again declare as srting type = c in subclass then I am getting the f output so I am bit confused here and whants to know what actually it happning – immortal Aug 18 '17 at 19:47
  • @Makoto Hiding would imply the variable is redeclared in the subclass. It isn't. – shmosel Aug 18 '17 at 19:47
  • @shmosel: Ah. Fair point. I'll reopen it then. – Makoto Aug 18 '17 at 19:47
  • @Makoto can u plzz expain bit more – immortal Aug 18 '17 at 19:50
  • Explain what? Why would you think a variable could hold two values simultaneously? – shmosel Aug 18 '17 at 19:50
  • 1
    @PrashantChaturvedi Do you understand what inheritance means? There isn't a second "virtual parent object" that contains its own data. – azurefrog Aug 18 '17 at 19:51
  • but when I am redeclaring it as string type='c' I get the output as f at this time why it is not having the same value – immortal Aug 18 '17 at 19:53
  • Maybe you should post an example code for each scenario you are trying to describe in English. – takendarkk Aug 18 '17 at 19:57
  • class Feline { public String type = "f "; public Feline() { System.out.print("feline "); } } public class Cougar extends Feline { public Cougar() { System.out.print("cougar "); } void go() { String type = "c "; System.out.print( this.type + super.type); } public static void main(String[] args) { new Cougar().go(); } } here I get the output as feline cougar c f – immortal Aug 18 '17 at 20:02
  • That's because you now have 2 completely different variables, why would they have the same value? – takendarkk Aug 18 '17 at 20:16

1 Answers1

1

The answer to the original question is simple: There is only one variable called type. Its initial value gets overwritten by c. Remember that there is only one object, so one variable. Prashant's code creates a second variable and obviously that one doesn't overwrite the original string in the parent class.

MoNo
  • 11
  • 1