Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call override method inside parent constructor

 
Stal Alexandr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I try to call override method inside parent constructor and I'm confused:




The output of Sub-object creation is "Sub". It is clear, OK.
But when I've changed acces modificator at Base class method to private I got "Base" output.
Why is it so??

Thank you.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The output of Sub-object creation is "Sub". It is clear, OK.


Show the full code (including the main method) where the output is "Sub".
 
Stal Alexandr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the method is private no overriding happens because the subclass doesn't see any method to override.
 
Stal Alexandr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:When the method is private no overriding happens because the subclass doesn't see any method to override.


Thank you! Now it is clear for me.
But one more little question about above code.

We create Sub object (Sub s = new Sub();) the keyword "this" at Base constructor reference to Sub object.


But my "method()" is private at Base class. So on, we call private method of parent class for child object. Why do Java allow it?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The actual method that is called is determined at runtime not at compile time. At compile time the class Base will compile because this.method refers to the method in the same Base class which is allowed by the rules.
 
Stal Alexandr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote: At compile time the class Base will compile because this.method refers to the method in the same Base class which is allowed by the rules.


Hmm...
I've just changed Base constructor to



If "this.method refers to the method in the same Base " then this.toString() should refers to the method in the Base too. But I've got result
test.Sub@a90653
Why??
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the default toString implementation (in class Object) gets the actual (runtime) type of the object (and your instance is a Sub).
 
Stal Alexandr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Roel. Now I understand it.
 
Winston Liek
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry but I would like to bring up this topic.

Why when the method "method" on the superclass becomes private, the this.method on the Base contructor executes its own method instead of subclass method upon creating the subclass from main.

Such as:



Output:

Base
javaexerciseproject.Sub@1372a1a
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:Why when the method "method" on the superclass becomes private, the this.method on the Base contructor executes its own method instead of subclass method upon creating the subclass from main.

Easy (if you know it )! You can only override something (and have polymorphism) if the (instance) method is inherited. Because a private method is not inherited by the subclass, no overriding (and polymorphism) occurs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic