wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help" Watch "Help" New topic
Author

Help

ramesh kumar
Greenhorn

Joined: Aug 15, 2006
Posts: 25
class Parent {
String s = "Parent";

public Parent(){
method();
}

public void method(){
System.out.println("in Parent "+s);
}
}

class Child extends Parent {
String s = "Child";

public Child(){
}

public void method(){
System.out.println("in child "+s);
}
}

public class ObjectTest {

public static void main(String[] args) {
Child child = new Child();
}
}

When i run the above code it is printing
in child null
And when i changed the Child class method name to name1() it is printing

in Parent Parent

Can any body explain me the reason
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Topics you should review to understand this type of questions:

1- Order of assignment of instance variables in the class hierarchy.
2- Polymorphic call of methods.


When you create child object in the main method, as child class constructor
automatically calls the parent class constructor and in this child class
"method()" is called (because object is created of Child class) and by this
time, the instance variable "s" of the child has not been initialized hence
you got the default value of the String as "null".





Note: In future, please place your code inside the [code] tag. It makes the
code readable.


Thanks,
[ May 28, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Yeming Hu
Ranch Hand

Joined: May 14, 2007
Posts: 37
In the first situation, the child overrides the method(), so in the Parent constructor, the overridden method() is called and the s from child is printed. In addition, the constructor of the Parent hasn't finished, therefore s from child is null.

In the second situation, the child doesn't overrides the method(), so in the Parent constructor, the method() defined in the Parent(inherited by Child) is called and s from Parent (inherited by Child) is printed. Because Parent's superclass is Object, whose constructor has finished at this time, s from Parent is initialized to Parent.


Best Wishes,<br /> Yeming
ramesh kumar
Greenhorn

Joined: Aug 15, 2006
Posts: 25
Thank You for your reply.With your reply i understand that once super class constructor finishes then only instance variables of subclass will be initialized am I currect?
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi
----------------------------------------------------------------------------
Thank You for your reply.With your reply i understand that once super class constructor finishes then only instance variables of subclass will be initialized am I currect?
----------------------------------------------------------------------------

Yes you are write


Thanks

Anil Kumar
ramesh kumar
Greenhorn

Joined: Aug 15, 2006
Posts: 25
Thanks all
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


you are write ??


:roll:


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help