Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding object initialization?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider following classes :







this code prints the value of c as 79. i had read somewhere that if constructor in a superclass invokes a method that has been overridden in a subclass, the subclass's implementation of that method will run. If the subclass's method implementation uses instance variables explicitly declared in the subclass, those variables will still have their default initial values. i would like to know the reason behind this ? what is the exact reason ?
 
Henry Wong
author
Marshal
Pie
Posts: 21378
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:Please consider following classes :







this code prints the value of c as 79. i had read somewhere that if constructor in a superclass invokes a method that has been overridden in a subclass, the subclass's implementation of that method will run. If the subclass's method implementation uses instance variables explicitly declared in the subclass, those variables will still have their default initial values. i would like to know the reason behind this ? what is the exact reason ?



Not exactly sure what reason you are looking for -- as that is how it is defined in the JLS. If you try to use an instance variable before it is initialized, it will have the default value.
Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks henry. but when Animal class constructor runs , it calls walk() method , which returns 24 and assigns it to c. since Dog class extends Animal class , it inherits both a whose value is 6, and c whose value (according to me) should be 24. but the output shows that value of c instance variable in Dog object is 79. which means that when the constructor of Animal class was invoked , the walk method defined in Animal class didn't run, but the one overridden by Dog class ran. i would like to know why didn't the animal walk ran. or how did jvm knew which method to call ?
 
Henry Wong
author
Marshal
Pie
Posts: 21378
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:thanks henry. but when Animal class constructor runs , it calls walk() method , which returns 24 and assigns it to c. since Dog class extends Animal class , it inherits both a whose value is 6, and c whose value (according to me) should be 24. but the output shows that value of c instance variable in Dog object is 79. which means that when the constructor of Animal class was invoked , the walk method defined in Animal class didn't run, but the one overridden by Dog class ran. i would like to know why didn't the animal walk ran. or how did jvm knew which method to call ?


The walk() method of the Animal class didn't run when instantiating a Dog object because the method has been overridden by the Dog class. Overriding rules apply -- even before the object has finished initializing. As for how the JVM knows -- it knows that it is instantiating a Dog object, and setup all the needed vtables early in the instantiation process (probably right after memory has been allocated).

Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry . Did not understood what you meant by vtables , but got answer to my problem .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic