• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


you are write ??



:roll:
reply
    Bookmark Topic Watch Topic
  • New Topic