Since the code I'm about to post is longer than in previous times, I'll be brief. Analyze this case and please tell me Why the method with the comment//This methodis never reached.
The preceding code prints out 40. And of course, that means that the method addValue() in class Base() never executes. When inside Base() constructor, it calls the addValue() method, and it uses the one defined in class Derived. but the Question is:
WHY ?
Hope you guys can help me out., this one is beyond the scope of my current knowledge.
Best Regards, Jose
PD. and by the way this code Excerpt was taken from a Mock Exam by Sahir Shah at JavaBlackBelt, However, I modified it a little bit. [ January 09, 2008: Message edited by: Jose Campana ]
Originally posted by Jose Campana: When inside Base() constructor, it calls the addValue() method, and it uses the one defined in class Derived. but the Question is:
WHY ?
Because that's how overridden methods work in Java. If you believe that Derived.addValue() overrides Base.addValue(), and you believe that the lone object created at runtime is an instance of Derived, then there's no way that Base.addValue() can be called. (Well, I guess Derived could call super.addValue() but it doesn't.)
That the variable 'b' has a compile-time type of Base, and that addValue() is called from Base's constuctor, are irrelevant.
Not all languages work this way, but Java does.
----
Allow me to confuse the issue by adding two lines (marked /* added */) to your example. I'll also rename everything so it can be discussed without confusion with the original example.
You are creating a object of Drevied and assingn it to a variable with ref Base.
When you are creating any Object wiht new Keyword what the JVM will do is first it creates all the varaibles and methods belong to its super and then it own. At any point of time if you call any varible or method first it will try to pick from its own area if not available it goes to super area.
here also the addvalue is available with Derived one, so it executes the devried class method.
In this case if you want to supress and call super class methods then you need to use super keyword which invoke super class method directly.
Fortunately (or unfortunately) the explanations you guys have given me seem to confirm what I thought happens behind the scenes. I said unfortunately because it's an strange behavior, at least in my opinion and that implies that is yet another Rule that must be understood, and complicates things because for the same Scenario but with static methods instead, the result is different and the rules that apply of course are different as well.
Anyways, The explanations have been satisfying nonetheless. Thank you very much, Sincerely, Jose
Originally posted by Jose Campana: I said unfortunately because it's an strange behavior, at least in my opinion and that implies that is yet another Rule that must be understood, and complicates things because for the same Scenario but with static methods instead, the result is different and the rules that apply of course are different as well.
The key issue is does Derived.addValue() override Base.addValue() or not?
In your example, it does.
In the same Scenario but with static methods instead then it wouldn't. (Static methods can't be overridden.)
So it's not so much that different rules apply so much as one must also be aware of the rules about method overriding.
You've taken special care about my question, and I'm grateful for that. And, no worries now, I know how to think now when I happen to see something like this.
Hi! I'm sorry to say this but this question is not very good for me. Like... I think that the answer is 40 because, first you called the constructor of Base, so the value of value (strange) is 20, because of the overriding method, right?. But then, it called the constructor of Derived, adding 20 to value, turning it into 40.
Bill, It would be cool if you could stop referring to events in programming as magic. There really isn't anything magical about it. It may confuse beginners into thinking there is something going on that they can't possibly understand. We're all muggles here. Well, most of us. Besides, magic isn't allowed outside of Hogwarts anyway.
Let me help you, It's better if you Understand it without reasoning much. as you have read before, and you probably noticed, Fred's explanation, He said,
"The object created is of type derived"
Actually that's all there is.
What you must understand is that the only methods that will be executed at Runtime are methods Defined in the Object that was Instantiated, it doesn't even matter if its reference variable is of a different type through polymorphism.
Yes... a little bit... Actually, I studied Inheritance and Polimorphism at college but I just forgot some details... so I'm reading chapter 7 from Kathy and Bert's Heads First Java... but I can tell you that I'm having some difficulties to learn that, but it's ok. With calm I can learn something
Originally posted by Andre Brito: ...but I can tell you that I'm having some difficulties to learn that, but it's ok. With calm I can learn something
That's great.
Also practice as much as you can with pretty simple, small examples as well. Keep enough System.out.println() statements for your easy understandings. It will really help!
Its a good example i think in the middle your example was somehow missed to gather the attention.
By the time the parent constructor execution is in progress, the 'foo' member inside the addValue() of DerivedFoo is NULL (otherway, the initialization of DerivedFoo is yet to happen). That's why we get a NullPointerException
Just for better understanding, i have modified the program in such a way that instead of calling toUpperCase on foo reference variable, it just prints outs the value. Also added some SOPs.
I said unfortunately because it's an strange behavior, at least in my opinion and that implies that is yet another Rule that must be understood, and complicates things because for the same Scenario but with static methods instead, the result is different and the rules that apply of course are different as well.
That's because static methods aren't polymorphic - and that's really the only rule you need to learn, everything else follows automatically. (The same is true for private methods, by the way.)
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus