| Author |
This is a good one about inheritance
|
Jose Campana
Ranch Hand
Joined: May 28, 2007
Posts: 339
|
|
Hello there true believers! 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 method is 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 ]
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 8104
|
|
|
the object being created is a Derived.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Bill Shirley
Ranch Hand
Joined: Nov 08, 2007
Posts: 457
|
|
during the magic of new, the java runtime calls the constructors of the superclass and the subclass. You have overriden addValue() in the subclass. All instances of the subclass have this method replaced. The only (non-reflective) way to get to it is to invoke super.addValue() from the subclass. [ January 09, 2008: Message edited by: Bill Shirley ]
|
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
|
 |
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
|
|
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.
|
bitguru blog
|
 |
Andre Brito
Ranch Hand
Joined: Dec 13, 2007
Posts: 95
|
|
I did not understand why 40. I'd answer 20 if that was a question from the exam. Can someone explain me again?
|
 |
Yelamuri Chandu
Greenhorn
Joined: Jan 08, 2008
Posts: 15
|
|
Its simple i will explain in normal way. 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.
|
 |
Jose Campana
Ranch Hand
Joined: May 28, 2007
Posts: 339
|
|
Hi there ! 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
|
 |
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
|
|
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.
|
 |
Jose Campana
Ranch Hand
Joined: May 28, 2007
Posts: 339
|
|
Thanks Mr. Cole... Thanks, sincerely. 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. I wish you good luck! Have a nice day, Jose
|
 |
Andre Brito
Ranch Hand
Joined: Dec 13, 2007
Posts: 95
|
|
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. Is this or not? Sorry, but I didn't get this explanation :/
|
 |
Gregg Bolinger
Sheriff
Joined: Jul 11, 2001
Posts: 15021
|
|
during the magic of new... 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.
|
My Blog | DZone Articles
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 23247
|
|
Originally posted by Andre Brito: Is this or not?
Yes, that's it.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Jose Campana
Ranch Hand
Joined: May 28, 2007
Posts: 339
|
|
Hello Andre, How are you sir? 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. Hope that has helped, As it helped me... Jose
|
 |
Andre Brito
Ranch Hand
Joined: Dec 13, 2007
Posts: 95
|
|
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 Thanks folks!
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3287
|
|
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!
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Milton Ochoa
Ranch Hand
Joined: Oct 23, 2007
Posts: 335
|
|
Jose Thank you for the question, that made me learn more about inheritance.
|
 |
Jose Campana
Ranch Hand
Joined: May 28, 2007
Posts: 339
|
|
Any time, Mr. Milton, Glad to know my questions are useful to other ranchers out there! Talk to you later dude !
|
 |
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
|
|
So it doesn't bother anyone that Test4 throws a NullPointerException before it can emit any output? I guess it's good that everyone understands.
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3287
|
|
That's great Brain 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
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3287
|
|
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. The above code produces the following output.. Hope this helps!
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
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.
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
|
 |
 |
|
|
subject: This is a good one about inheritance
|
|
|