• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting super class of 2 subclasses  RSS feed

 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a super class that has methods used by both subclasses so i don't have to repeat the same method in both of the latter. i want to be able to call that method only once, but in the class im working on i have no reference to the superclass. i could call the method onece from each subclass, yes, but i wouls like to know how to get a referenc to the super class from either one of those 2 subclasses to as to call the method in the super calss only once for both subclasses.

Thanks.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Isaac Hewitt,

We don't need a super class instance to call its methods (of course given that those are accessible from sub class).

If I understand correctly, all you need is that a method should be called only once - no matter from which sub-class it is being called. Is that right? In that case, a simple static boolean variable might save the trouble for you.

Initialize it to true, and while calling the method for the first time, set the variable to false. Whenever you are making call to super-class method, always check that if the variable is true.

I hope this helps.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!