• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance method calls  RSS feed

 
Ciaran Reid
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • 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
    • 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?
     
    Tushar Goel
    Ranch Hand
    Posts: 934
    4
    • Mark post as helpful
    • send pies
    • 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
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • 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
    • 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: 56536
    172
    • Mark post as helpful
    • send pies
    • 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.
     
    sai rama krishna
    Ranch Hand
    Posts: 536
    1
    • Mark post as helpful
    • send pies
    • 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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!