• 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

inheritance method calls

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i have 2 classes, Top and ClassB which extends Top

and create an instance of ClassB:
ClassB b = new ClassB();

my results are:

"variable value = variable is initialized in B Class"


Then if i change in ClassB - String variable = null and run it gives me:

variable value = null

But my problem is when i dont have ClassB inheriting from Top and run with
String variable;
or
String variable = null;

both gives me output of
variable value = null

So what is happening when ClassB inherits from Top?
I know that the B constructor is calling super, so does that mean its calling setVariable (in Top?) but as its overridden in ClassB, then that is whats being called and setting the String variable?
I hope that makes sense
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. That is exactly what is happening.
When you create an instance of ClassB the constructor of superclass is called.
That constructor calls setVariable(). Which setVariable() does it call? You are right again. It calls a method from ClassB.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The moral of the story: don't allow constructors to call overridden methods.
And how do you do that? One of two ways:-
  • 1: A method called from the constructor should have private access.
  • 2: A method called from the constructor should be marked final.
  • You need No 1 or No 2, not necessarily both.
     
    Ciaran Reid
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks, that makes sense; and understandable not to allow constructors to call overridden methods.
    But one thing thats still not 100% with me though -

    why is it that if i set String variable = null in ClassB, that it gives me output of variable value = null, but when its not set to null, it gives me proper output?

    Inside the super constructor there is the call to setVariable, which we have established is using setVariable in ClassB, and inside setVaraiable its setting variable = "variable is initialized in B Class";
    But why isnt this being set when String variable = null;


    Or is it simply that my example is not valid because a constructor is calling an overridden method?
     
    Ranch Hand
    Posts: 954
    4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    why is it that if i set String variable = null in ClassB, that it gives me output of variable value = null, but when its not set to null, it gives me proper output?



    because you are calling overridden method , which will use the value of the variable of same class only like:



    It will use null value and show you null only
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ciaran Reid wrote:
    why is it that if i set String variable = null in ClassB, that it gives me output of variable value = null, but when its not set to null, it gives me proper output?

    Inside the super constructor there is the call to setVariable, which we have established is using setVariable in ClassB, and inside setVaraiable its setting variable = "variable is initialized in B Class";
    But why isnt this being set when String variable = null;



    Instance variable initializations are done after the super constructor completes, but before the code in the constructor (except of the super part) executes. This means that when you have set "String variable = null in ClassB", the variable is set by the setVariable() method, when it is called from the super constructor, and then, set back to null during the initialization of the instance variable.

    Henry
     
    Ciaran Reid
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    wow, now that is something I havn't read before, thanks Henry. Really needing my thinking cap on here.

    And thanks indeed to all contributors, greatly appreciated
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The details are in the Java Language Specification; that might not be the correct section. It might not be easy to read.
     
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    and create an instance of ClassB:
    ClassB b = new ClassB();


    Which class is this instance created.
    where is the main class code. I wonder where is the call to the setter methods from constructors.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic