• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is the overriden method called instead of the original one?  RSS feed

 
Radulescu Iulia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!
I have the following section of code:



Why is the result 3 when I call new Bar(); instead of 2? (the method doSth() from Bar gets called instead of doSth() from Foo)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you calling it? What is the ACTUAL object you are creating? My guess is you are making a new Bar object, so that's the method that gets called - but that is only a guess, since you didn't bother to show us that part.
 
Radulescu Iulia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:How are you calling it? What is the ACTUAL object you are creating? My guess is you are making a new Bar object, so that's the method that gets called - but that is only a guess, since you didn't bother to show us that part.

As I mentioned at the end of my post, I'm creating a Bar object (new Bar()). Can you please explain in more detail?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radulescu Iulia wrote:the method doSth() from Bar gets called instead of doSth() from Foo


Since the Bar class overrides the method of the Foo class, shouldn't the overridden method be called? After all, isn't that what overriding supposed to do?

Henry
 
Radulescu Iulia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Radulescu Iulia wrote:the method doSth() from Bar gets called instead of doSth() from Foo


Since the Bar class overrides the method of the Foo class, shouldn't the overridden method be called? After all, isn't that what overriding supposed to do?

Henry


Yes, but the part that seems weird to me is: I create a new Bar() object. The Foo() constructor is called and inside the Foo() constructor doSth() is called. Shouldn't it be the method within the Foo() class?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radulescu Iulia wrote:
Yes, but the part that seems weird to me is: I create a new Bar() object. The Foo() constructor is called and inside the Foo() constructor doSth() is called. Shouldn't it be the method within the Foo() class?


Weird or not, that is simply how it works. If you override a method, and you call the method, the overridden version will be called. The only way to call the previous version, is via the use of the "super" keyword from the overriding class -- and that is not what you have in your example.

Henry
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Radulescu,

Here is a tutorial on the super keyword :Using 'super keyword'
Also you can use the debugger if you have any confusion on what variable is being used.

What I would do is use the super keyword and see what the variable is used in the debugger, then I would take out the super keyword and check the debugger again.
I use this learning style with my students while teaching math to reinforce learning, I call it discovery learning (there might be another tech term for it). But it really works.

Take care!


Bill
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is the reason that you should never call an overridable method in a constructor. For example, suppose Bar has an instance variable that is used in doSth():

Now when you call new Bar(), you end up with NullPointerException, because doSth() is called (from Foo's constructor) before y is created.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radulescu,

In each class in Java you have an implicit "this." for everything inside it's scope. So if we rewrite your code, we can easily see what is going to happen:

As you can see that in Bar you do not have the constructor overridden, so that puts you back into the scope of Foo and not in Bar, so you now have actions taking place Foo and not in Bar. Also notice that you have a declared x as static also. This makes x a Class Variable and as such is not distinct in any of the implemented objects, but shared by all. So if you have 100 Foo and 50 bar, all 150 objects reference the same container called x, so when you do any action in any of the objects, x will reflect those changes in all objects. There is only 1 x shared by all. So if someplace in your code, say in the 30th Bar you have this line of code execute some place:

every object will see x as 6 at that point, no matter the section of code they are executing. And that is why you should only use "static" when you need to, and understand what effects that will produce.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!