File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help

 
ramesh kumar
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Yeming Hu
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramesh kumar
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you are write ??


:roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic