473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inheritance and member variables

I wrote this piece of code:
class A {
public A() {
someMeth();
}
public void someMeth() {
out.println(entries[0]);
}
public final String[] entries = { "a" };
}

class B extends A {
public void someMeth() {
out.println(entries[0]); *** NullPTR
super.someMeth();
}
private final String[] entries = { "b" };
}

the point is to start, during instantiation, some methods from the
bottom of the inheritance tree instead of from the top.

but i had some unexpected behavior: at the marked point, I receive a
NullPTR.
Why does this happen?

Seem like that at the moment of the call (***) member vars are not
instantiated, since the call comes from the constructor of the
superclass, and the subclass is not still instantiated, but why then the
subclass method works?

Bye!
it
Jun 21 '07 #1
1 3268
Lew
ITMozart wrote:
I wrote this piece of code:
class A {
public A() {
someMeth();
}
public void someMeth() {
out.println(entries[0]);
}
public final String[] entries = { "a" };
}

class B extends A {
public void someMeth() {
out.println(entries[0]); *** NullPTR
super.someMeth();
}
private final String[] entries = { "b" };
}

the point is to start, during instantiation, some methods from the
bottom of the inheritance tree instead of from the top.

but i had some unexpected behavior: at the marked point, I receive a
NullPTR.
Why does this happen?

Seem like that at the moment of the call (***) member vars are not
instantiated, since the call comes from the constructor of the
superclass, and the subclass is not still instantiated, but why then the
subclass method works?
The subclass method exists, but the subclass variables don't get initialized
until the superclass constructor finishes. During the call to the polymorphic
method someMeth() in the superclass constructor, you access the subclass
method which access the subclass 'entries' array, which still has its default
value of null because you have not entered the child class constructor yet.

Joshua Bloch covered this in /Effective Java/ when he advised that you not
call overridable methods in a constructor.

--
Lew
Jun 21 '07 #2

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

Similar topics

10
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
1
by: Joseph Turian | last post by:
I'm having a little difficultly with inheritance. I want to have a class "SubT" inherit from class "T". SubT contains all the information in T, plus a little more based upon the member variables...
17
by: lm401 | last post by:
I'm trying to work with the following idea: class animal: def __init__(self, weight, colour): self.weight = weight self.colour = colour class bird(animal): def __init__(self, wingspan):
8
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
4
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.