• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Using the super() method with an instance variable

 
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 21
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41656
883
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of the input, it was helpful!
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic