• Post Reply Bookmark Topic Watch Topic
  • New Topic

This is a good one about inheritance  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the object being created is a Derived.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andre Brito:

Is this or not?


Yes, that's it.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose Thank you for the question, that made me learn more about inheritance.

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!