• 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

Object initialization

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[CODE]
class SuperClassA {
protected int superValue;
public SuperClassA() {
System.out.println("Constructor in super classA");
this.doValue();
}

public void doValue() {
this.superValue = 911;
System.out.println("SuperValue: " + this.superValue);
}
}

class SubClassB extends SuperClassA {
private int value = 800;

public SubClassB() {
System.out.println("Constructor in SubClass B");
this.doValue();
System.out.println("SuperValue: " + this.superValue);
}

public void doValue() {
System.out.println("Value: " + this.value); // doubt ???
}
}

public class ObjectInitialization {
public static void main(String[] args) {
new SubClassB();
}
}

{/CODE]

I have a doubt where I place "doubt ???". This line prints 800. As I understand the instance variable gets initialized after the base constructor completes the execution. In that case how is it printing 800 before the constructor completes?

Thanks
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are observing behaviour as a result of violating a fundamental rule (oooh! did I say that?). The fundamental rule is this: "Never ever call an overridable method from a constructor on a reference to 'this'". This fundamental rule is only slightly related to another fundamental rule: "Declare all classes final, always (except where concession is due to a broken dependancy e.g. Java exceptions)".

Check this out:
http://qa.jtiger.org/GetQAndA.action?qids=10
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chithra,

the doValue() in Super will never be called as you are overriding it in the SubClassB. The call to doValue() both in the super class and the subclass constructors leads to the SubClassB.doValue() at run time. If you make print statements more specific, this will be clear to you. This means, the superValue is never assigned your intended value, 911. So you observe that superValue is always the default 0.

hope this helps,
Soumya.
reply
    Bookmark Topic Watch Topic
  • New Topic