• 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
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

this or super (call from sub class)

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been thinking of what's the most appropiate way to call a method in an abstract class from an instance of one of its sub-classes. I'll show you some brief code to get it clear what I mean:

Abstract class

And an example of a sub class...

So if you check the last part of the code, both calls work. I think the most appropiate way of calling the method in the super class is by doing it with help of this but I might be wrong? Please let me know if you have a good explanation.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If SubClass does not override the method from AbstractClass, they are equivalent and point to the same method on the same instance, and you don't really need to use either "super" or "this".

Using "super" means that you need to change the method call if you ever override it in your subclass. Using "this" (or none) ensures that you call the overriding method if such exists (or inherited if it doesn't).

So, generally, don't use super, unless you want to ensure you don't call an overriding method.
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic