aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding object initialization? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt regarding object initialization?" Watch "Doubt regarding object initialization?" New topic
Author

Doubt regarding object initialization?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

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 ?


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18116
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

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
Sheriff

Joined: Sep 28, 2004
Posts: 18116
    
  39

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

Joined: Apr 04, 2012
Posts: 923
    
    1

Thanks Henry . Did not understood what you meant by vtables , but got answer to my problem .
 
Consider Paul's rocket mass heater.
 
subject: Doubt regarding object initialization?
 
Similar Threads
Method declaration question
whats wrong with my code?
Covariant Overload in Java
polymorphism
Casting reference types.