• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling Overridden Methods without super  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I call the overridden version of an object's foo() method
outside that object's class, without using super? EG:

class MainProgram {
public static void main(String[] args) {
Beta b = new Beta();

// I want to invoke Alpha.foo() but on the Beta instance.
// I want to cancel the dynamic binding that makes Beta.foo()
// get called instead
// b.super.foo() <---- obviously doesn't compile
}
}

class Alpha {
public void foo() { System.out.println("inside Alpha.foo()"); }
}

class Beta extends Alpha {
public void foo() { System.out.println("inside Beta.foo()"); }
}

I know, it is possible through JNI , but is it possible through reflection?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm suprised (and disgusted) that it can be done through JNI.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems as though you are not understanding the purpose of method overriding. If you have a class B that overrides a method foo() from its superclass A then the implementation found in the sub class is what is supposed to be invoked. Overriding that behaviour fights the main purpose and principles of abstraction. In general the calling code should not know or need to know the details of the implementation it calls into. It's like you needing to know or wanting to control the torque of your engine when you step on the pedal in your car. Back to the point. If the behaviour in the super class is the desired behaviour then the assembler (the person responsible for linking the implementation into the calling code) should control that.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Clifton Craig:
It seems as though you are not understanding the purpose of method overriding...


Certainly, one would agree that an overriden method should itself, in many cases, call the overriden super before or perhaps after the overriden functionatiy is performed. Let's consider the latter case. I want to call the super after I do my overriding task:



Now consider that domore() needs to be threaded and the super.doit cannot be called until the domore() finishes:



The only way that I know how to do it (refecting on the Class.getSuperclass() does not work) is this ugly bit:
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg, try

MyDoitClass.super.doit();
 
Greg Moose
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:

MyDoitClass.super.doit();


Brilliant Ilja! (At least I hope it is a sign of brilliance if someone tells me something that I didn't already know )

This protects the API for leaking out such sillyness. I could have made superdoit() private, but total unnecessary now... My only fear is that I leaked out a way for Singhal or other to call the super on the api..
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!