• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

problem with super keyword

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


inside the doStuff() method i am incrementing the variable by using "this.f++" statement which should increment the variable "f" of class NewConsole which was inherited from the NewConsoleSuper class. So the variable "f" of NewConsole class should have value 11 and variable "f" of NewConsoleSuper should have value 10, so far so good (if i am not wrong). After that when i try to print the values of variable "f" in both the classes i get unexpected values.
The output is : 11

whereas the output should have been : 10

because "super.f" should give value 10 as the value of variable "f" in NewConsoleSuper is 10, but it prints 11.

Further when i try to access variable "f" in NewConsoleSuper by using ...System.out.println(new NewConsoleSuper().f), then it prints 10 indicating that variable "f" in NewConsoleSuper is 10 but super.f prints 11.
 
Marshal
Posts: 76872
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you expect that f will remain unchanged?

Remember that a SubclassObject IS‑A SuperclassObject. Since f is not private (even the Java™ Tutorials now tell you to make all fields private), f will be inherited by the subclass. There should be a code example in this Java® Language Specification (=JLS) section. Beware: the JLS can be difficult to read.
So there is one f and only one f, which is inherited from the superclass. In that JLS example the second point object had the same x and y as its superclass object. So when you change f, there is only one f which is in the subclass and in the superclass, so changes in one are “visible” in the other. Using super. doesn't make any difference.

You could avoid that by redeclaring f but that would cause you lots of other problems and confusion.
 
Campbell Ritchie
Marshal
Posts: 76872
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ikneet singh wrote:. . . System.out.println(new NewConsoleSuper().f) . . .

You are creating a new and different object with a fresh field, so you are getting the value before incrementing.
 
ikneet singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie ... Roger that boss
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic