• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor Confusion?  RSS feed

 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






I wrote the preceding code while testing out my understanding of constructors (among other things not shown here that would have no impact on the issue at hand).
My biggest issue is that it prints the following:

Constructor A: Default
Method being called in ConstructorB *******This is the confusing part*******
Method #2 being called in ConstrutorA
Constructor B: Default
Method being called in ConstructorB

I cannot figure out why it is printing out "Method being called in ConstructorB" in the default ConstructorA constructor? Even when I hover over it in Eclipse, it says that the this.methodCallInConstr() is pointing at the method in ConstructorA (talking about the method inside the default constructor of ConstructorA). I'm assuming that somehow, B is polymorphically overriding that method b/c the names of the methods are the same? I know that it is probably horrible programmership, but it shows me a bad assumption I was making in my studies.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
methodCallInConst() is overridden in ConstructorB, so the call to it in ConstructorA goes there.
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:methodCallInConst() is overridden in ConstructorB, so the call to it in ConstructorA goes there.


Why does it get overridden though? When ContstructorA() executes, why doesn't it use it's own class' methods? I understand in polymorphism, when you have a reference variable of another type, but I don't see where this would have that reference variable of type ConstructorB?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding (and I'm confident someone will correct me if I'm wrong) is that the super() in ConstructorB is executed in ContructorB's environment. It is as if you had executed this:

 
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
Richard Newman wrote:
Why does it get overridden though? When ContstructorA() executes, why doesn't it use it's own class' methods? I understand in polymorphism, when you have a reference variable of another type, but I don't see where this would have that reference variable of type ConstructorB?


With polymorphism, the method chosen is based on the type of the object. It doesn't matter what the reference variable type is. It is a ConstructorB instance that got created -- via the new operator.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!