473,439 Members | 1,951 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,439 software developers and data experts.

Derived class mystery

Okay so here is a tricky example from my lecture notes:
Expand|Select|Wrap|Line Numbers
  1. class Base {
  2.     private int val;
  3.     Base () {
  4.     val = lookup();
  5.     }
  6.     public int lookup() { 
  7.     return 5; 
  8.     }
  9.     public int value() {
  10.     return val;
  11.     }
  12. }
  13. class Derived extends Base {
  14.     private int num=10;
  15.     public int lookup() {
  16.     return num;
  17.     }
  18. }
  19. class Test {
  20.     public static void main( String[] args ) {
  21.     Derived d = new Derived();
  22.     System.out.println(d.value());
  23.     }
  24. }
  25.  
I would expect the program to print out 10, but it actually prints out 0... Can anyone explain that?:D
Expand|Select|Wrap|Line Numbers
  1. java -version
  2. java version "1.6.0_18"
  3. OpenJDK Runtime Environment (IcedTea6 1.8) (fedora-41.b18.fc13-x86_64)
  4. OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)
  5.  
Thanks!
Jul 28 '10 #1
3 1674
Oralloy
988 Expert 512MB
The constructor for Base was executed after the memory was allocated for the object, but before the constructor for Derived was executed .... right?

So why do you expect that num was set to anything other than a random (or perhaps the default value) when the Base constructor runs?

Think about it...
Jul 28 '10 #2
Mh right, makes sense... Thanks!
Jul 28 '10 #3
Oralloy
988 Expert 512MB
@sandromani
You're welcome.

BTW, I'm not sure what the JVM specification says about such things. That's the ultimate answer, and it probably states that the behaviour is unspecified.
Jul 28 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
10
by: Julia | last post by:
Hi Please can someone explain this behaviour: I have a MustInherit Base class and a Derived class that Inherits Base and Shadows a method in the base class. If I Dim a variable of type...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.