• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How the output came like

 
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I wanted to ask that the Output coming is-> feline cougar c c  and not feline cougar c f why??  The type variable gets inherited in the Cougar class but go() method changed it from "f" to "c". So this.type=c //OK
but super.type="c" why???  Why not "f"?
 
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inheritance is code reuse because the this keyword can be used in a subtype to directly access a member declared in a supertype (if the member is not overridden or hidden in the subtype).

So type = "c " in Cougar directly accesses the type field in Feline.
 
O Shea
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was fine but I can't understand about. Why after accessing super.type gives "c" and not "f"
 
Daniel Cox
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When go() runs,
  • type = "c " directly accesses the type field in Feline and sets its value to "c "
  • this.type accesses the type field in Feline which has a value of "c "
  • super.type also accesses the type field in Feline which has a value of "c "

  • this and super behave the same because they are used to access a member that is inherited from Feline and not overridden or hidden in Cougar. You'll notice a difference if they are used to  access a member that is overridden or hidden in Cougar.
     
    O Shea
    Ranch Hand
    Posts: 207
    3
    Oracle MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for nice explaination. One thing -> Changes made by child class will be reflected in Parent class-> Is this applicable only for instance variables? What about static members and overriden methods?
     
    Daniel Cox
    Ranch Hand
    Posts: 234
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, it applies to any member in Feline (as long as the member is inherited by Cougar and is not overridden or hidden in Cougar).

    Any usage of this.member in Cougar will access a member in Feline (as long as the member is inherited by Cougar and is not overridden or hidden in Cougar).

    This is why inheritance is called code reuse.
     
    Ranch Hand
    Posts: 39
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Any usage of this.member in Cougar will access a member in Feline (as long as the member is inherited by Cougar and is not overridden or hidden in Cougar).



    “this” is used to refer current class instance variable right? In the above code, current class Cougar and also if this is used to access parent class variable then why super is used ?
    Even super does the same job right?

     

    When I replace the code in Cougar with above code. It will give feline cougar f f.
    Here, the type variable is hiding the type variable in the parent class, right ?
     
    O Shea
    Ranch Hand
    Posts: 207
    3
    Oracle MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    variable type present in Feline class is inherited in Cougar class

    Here above actually you have defined a local variable of same name as the instance variable type which is inherited in Cougar class. So when you are accessing (this.type) gives current class instance variable type value which is 'f'(Directly inherited from base class). Because that local variable which has same name present in go() method is local and its scope is limited to that method only that's why (this.type) gives current class instance variable type value which is 'f' . super.type gives parent class's type variable value which is 'f' .

    But I can't understand 1 thing that



    In above code we have changed value of the inherited variable "type" from parent class from 'f' to 'c' . So (this.type) gives current class instance variable(type) value which is 'c' because we change it inside method go() . OK , but why that change is also reflected in parent class since super.this also gives 'c' . But How???
     
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is only one variable. That's Feline.type. The fact that Cougar inherits the variable and can access it through this.type, doesn't mean that suddenly there are two variables. If there's only one variable, how can it hold two different values?
     
    O Shea
    Ranch Hand
    Posts: 207
    3
    Oracle MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No there are no 2 variables there is only 1 variable , but when we declare a new variable inside go() method then there will be 2 variables see->



    Now output came-> feline cougar f f c

    c is because of local variable we have created inside go() method.

    But my doubt is that -> When we change the value of type instance variable in child class then how its value is also changed in parent class. Since (super.type) give 'c' instead of 'f' . But how?? Is this is possible that-> any changes made in instance variable which is inherited by child class is also reflected in base class instance variable?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15510
    363
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    O Shea wrote:Is this is possible that-> any changes made in instance variable which is inherited by child class is also reflected in base class instance variable?


    Of course, because super.type and this.type refer to the same variable: Feline.type.
     
    O Shea
    Ranch Hand
    Posts: 207
    3
    Oracle MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ya got it. Both reffering to same . Thanks for clearing doubt.
    reply
      Bookmark Topic Watch Topic
    • New Topic