473,473 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

inheritance confusion

Can someone please help explain the following behaviour, or point me
to a post that explains it?

While i was debugging one of my company's programs i noticed some
objects behaving strangely. I was having a very difficult time
finding the source of the problem. Finally, when i realized what was
going on i wrote the following three classes to try to understand what
happens in the JVM.

Given:
/**
* @author Ashkon Sabet
* www.ashkon.net
*/

public class Parent
{
protected boolean parentBoolean = false;

public Parent()
{
System.out.println("Entering Parent() Constructor");

init();

System.out.println("Exiting Parent() Constructor");
}

protected void init()
{
System.out.println("Entering Parent.init()");

parentBoolean = true;

System.out.println("Value of parentBool "+parentBoolean);

System.out.println("Exiting Parent.init()");
}
}

/**
* @author Ashkon Sabet
* www.ashkon.net
*/
public class Child extends Parent
{
protected boolean childBoolean = false;

public Child()
{
super();
}

protected void init()
{
System.out.println("Entering Child.init()");

super.init();

setChildBoolean();

System.out.println("Child.childBoolean = "+childBoolean);

System.out.println("Exiting Child.init()");
}

public void setChildBoolean()
{
if (super.parentBoolean == true)
this.childBoolean = true;
}
}

/**
*
* @author Ashkon Sabet
* www.ashkon.net
*/

public class FamilyTester {

/** Creates a new instance of FamilyTester */
public FamilyTester() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
Child myFirstChild = new Child();
System.out.println("My First Child Boolean ==
"+myFirstChild.childBoolean);

// Child mySecondChild = new Child();
// mySecondChild.setChildBoolean();
// System.out.println("My Second Child Boolean ==
"+mySecondChild.childBoolean);

}
}

When you run FamilyTester you get the following output:

Entering Parent() Constructor
Entering Child.init()
Entering Parent.init()
Value of parentBool true
Exiting Parent.init()
Child.childBoolean = true
Exiting Child.init()
Exiting Parent() Constructor
My First Child Boolean == false

*******************************
I would have expected the last line to read:
My First Child Boolean == true

Thank you for you help.
-Ash
Jul 17 '05 #1
4 2307
Yes, I will try to explain this. Before the Child instance is
initialized, the Parent instance within that Child instance is
constructed. So, the Parent constructor is called, then the
initializers for the Child class are run, then the Child constructor is
executed. For the variable childBoolean, this is what happens:

1) Parent constructor is run. Parent constructor calls init(), which
results in Child.init() being executed. So childBoolean is set to true.

2) Child initializers are executed. childBoolean is then set to false.

3) Child constructor is executed, no change for childBoolean.

To see this in action, try the following modification and note when the
new message is displayed:

class Parent
{
protected boolean parentBoolean = false;

public Parent()
{
System.out.println("Entering Parent() Constructor");

init();

System.out.println("Exiting Parent() Constructor");
}

protected void init()
{
System.out.println("Entering Parent.init()");

parentBoolean = true;

System.out.println("Value of parentBool "+parentBoolean);

System.out.println("Exiting Parent.init()");
}
}

class Child extends Parent
{
protected boolean childBoolean = false;
private String msg = getMsg();

public Child()
{
super();
}

protected void init()
{
System.out.println("Entering Child.init()");

super.init();

setChildBoolean();

System.out.println("Child.childBoolean = "+childBoolean);

System.out.println("Exiting Child.init()");
}

public void setChildBoolean()
{
if (super.parentBoolean == true)
this.childBoolean = true;
}

private String getMsg()
{
String result = "childBoolean was just initialized to false";
System.out.println(result);
return result;
}
}

public class FamilyTester {

/** Creates a new instance of FamilyTester */
public FamilyTester() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
Child myFirstChild = new Child();
System.out.println("My First Child Boolean == "
+myFirstChild.childBoolean);

}
}

Ray

Jul 17 '05 #2
On 28 Aug 2003 23:56:26 -0700, as****@hotmail.com (Ashkon) two-finger
typed:
Ray,

Thank you for your insight. I understand what is happening now.

However, the flow of the source code makes it seem more intuitive to
have Child's global data members created and initialized before
Parent's constructor is run and not in between the running of Parent's
constructor and Child's constructor. From your post though, i
understand that Parent's constructor is executed before Child's global
data memebers are defined.

Thank you. You are genius! :)

So, what now? What if i need to override Parent's init() method but
still call it and still set global data memeber values? What's a good
solution?


Why not try this:

public class Parent {

protected boolean parentBoolean = false;

public Parent() {
Parent.init(this);
}

static void init(Parent p) {
p.parentBoolean = true ;
}
}

public class Child {

protected boolean childBoolean = false;

public Child() {
super();
Child.init(this);
}

static void init(Child p) {
p.childBoolean = true ;
}
}

This way the parent only ever initializes parent variables, and the child
does the parent first, and then only its own variables.

Cheers.
Jul 17 '05 #3
Ashkon wrote:
Ray,

Thank you for your insight. I understand what is happening now.

However, the flow of the source code makes it seem more intuitive to
have Child's global data members created and initialized before
Parent's constructor is run and not in between the running of Parent's
constructor and Child's constructor. From your post though, i
understand that Parent's constructor is executed before Child's global
data memebers are defined.

Thank you. You are genius! :)

So, what now? What if i need to override Parent's init() method but
still call it and still set global data memeber values? What's a good
solution?

Glad I could help.

There are many ways to do what you want. I suspect that the example is
too simplified to really understand what you need. Perhaps if you try
to explain the context a little more.

Ray

Jul 17 '05 #4
Good example, and I'm only taking an educated guess at what's going on here.

I compiled your example and saw your results.

I found that if you change this line:

protected boolean parentBoolean = false;

to

protected boolean parentBoolean;

you WILL get the result you expect ("My First Child Boolean = true").

This tells me that fields are initialized to their DEFAULT values
immediately upon entering the constructor but then to their declared
initializations immediately AFTER a call to super(), if one exists (and
which of course has to be the first line of the constructor if it were to
exist at all).

I can't explain the rationalization for why the JVM chooses (if it was even
intended at all!) this behaivor, but it's clearly true.

Btw, your example would be cleaner for explaining the point at hand if you
changed Child to simply be:

class Child extends Parent
{
protected boolean childBoolean = false;

public Child() {
super(); // will call Child.init()
}

protected void init() {
childBoolean= true;
}
}

You would see the same result: false returned as is; but true returned if
the initialization (which appears to be harmless duplication) were removed.
"Ashkon" <as****@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
Can someone please help explain the following behaviour, or point me
to a post that explains it?

While i was debugging one of my company's programs i noticed some
objects behaving strangely. I was having a very difficult time
finding the source of the problem. Finally, when i realized what was
going on i wrote the following three classes to try to understand what
happens in the JVM.

Given:
/**
* @author Ashkon Sabet
* www.ashkon.net
*/

public class Parent
{
protected boolean parentBoolean = false;

public Parent()
{
System.out.println("Entering Parent() Constructor");

init();

System.out.println("Exiting Parent() Constructor");
}

protected void init()
{
System.out.println("Entering Parent.init()");

parentBoolean = true;

System.out.println("Value of parentBool "+parentBoolean);

System.out.println("Exiting Parent.init()");
}
}

/**
* @author Ashkon Sabet
* www.ashkon.net
*/
public class Child extends Parent
{
protected boolean childBoolean = false;

public Child()
{
super();
}

protected void init()
{
System.out.println("Entering Child.init()");

super.init();

setChildBoolean();

System.out.println("Child.childBoolean = "+childBoolean);

System.out.println("Exiting Child.init()");
}

public void setChildBoolean()
{
if (super.parentBoolean == true)
this.childBoolean = true;
}
}

/**
*
* @author Ashkon Sabet
* www.ashkon.net
*/

public class FamilyTester {

/** Creates a new instance of FamilyTester */
public FamilyTester() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
Child myFirstChild = new Child();
System.out.println("My First Child Boolean ==
"+myFirstChild.childBoolean);

// Child mySecondChild = new Child();
// mySecondChild.setChildBoolean();
// System.out.println("My Second Child Boolean ==
"+mySecondChild.childBoolean);

}
}

When you run FamilyTester you get the following output:

Entering Parent() Constructor
Entering Child.init()
Entering Parent.init()
Value of parentBool true
Exiting Parent.init()
Child.childBoolean = true
Exiting Child.init()
Exiting Parent() Constructor
My First Child Boolean == false

*******************************
I would have expected the last line to read:
My First Child Boolean == true

Thank you for you help.
-Ash

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matthew Bell | last post by:
I've got a conceptual problem to do with inheritance. I'd be grateful if someone could help to clear up my confusion. An example. Say I need a class that's basically a list, with all the...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
5
by: ma740988 | last post by:
Prefer composition to inheritance (can't recall which text I stole that line from) is one of the fundamental tenets thats engrained in my mind. Having said that inheritance requires careful...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
2
by: Flavian Musyoka Mwasi | last post by:
I'm a novice programmer just beginning to learn the new C# language. I'm a bit confused about the way Inheritance and Interfaces are constructed in C#. The following examples may help clarify...
1
by: Flavian Mwasi | last post by:
I'm a novice programmer just beginning to learn the new C# language. I'm a bit confused about the way Inheritance and Interfaces are constructed in C#. The following examples may help clarify...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
3
by: Hazz | last post by:
I am just beginning to design a Treeview display (winforms) for wine regions. Problem. Some wine growing regions belong to two counties. Eg. Carneros is in both Napa and Sonoma Counties. Although...
5
by: Invalidlastname | last post by:
Hi, I just read the pattern "Design and Implementation Guidelines for Web Clients" from MSDN. Here is my question. In chapter 3,...
3
by: tshad | last post by:
I am playing with Inheritance and want to make sure I understand it. I have the following Classes: ******************************************* Public Class AuthHeader:Inherits SoapHeader Public...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.