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

Using the super() method with an instance variable

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing small pieces of code just to practice for the exam and came up with the following question. I understand you can call the super() version of an overridden method from a method in the subclass. Is there a way to do it directly from an instance variable of the subclass? The following does not work:


I haven't seen anything like this so I assume I would have to write a new method in SubClass to call the SuperClass method. Thanks!
 
Douglas Cyporyn
Greenhorn
Posts: 21
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kendall.

Calling super() invokes either the default or no-arg constructor for the super class. A constructor's purpose in life is to initialize it's object. So it really only makes sense to call super() when the object is being instantiated. Therefore, the only place you can explicitly call super() is from within a constructor in the subclass. And then you had better be sure that either A) no other constructor has been declared on the super class thereby letting the compiler add the default constructor or B) the no-arg constructor has been explicitly declared on the super class.

So, the constructor is not really a method. Constructors have lots of rules specific to them. You can think of it as a special kind of method with its own rules if you want. But it's definitely not a method.

Hope it helps!

Doug
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could write super.method(), but it has to be in the subclass itself. Not in code that calls the subclass.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:Is there a way to do it directly from an instance variable of the subclass?

No, you can't!

As already mentioned by Douglas, you can use super() only to invoke constructor from the superclass and this() to invoke an overloaded constructor as shown in the following code snippet:


Kendall Ponder wrote:I haven't seen anything like this so I assume I would have to write a new method in SubClass to call the SuperClass method.

That's indeed a possibility. But you have to be careful and invoke this method using the super keyword, like Jeanne has suggested. Otherwise you still would invoke the method() method in SubClass. Illustrated with this code snippet:

Another possibility would be to refactor the SuperClass class a little bit: you define an additional final method (subclasses can't override this method) which has the println-statement; the current method() method simply invokes this final method. As shown in this code snippet:


Hope it helps!
Kind regards,
Roel
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of the input, it was helpful!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic