Forums Register Login

Overriding a private method

+Pie Number of slices to send: Send
Is it possible to override a private method using reflection?
I know how to invoke a private method, and I know how to get or set the value of a private variable, but I want to override the method.

I want the main method in SubClass to print out 2, but it prints out 1.
I don't think this can be done through reflection.
If not, does anyone know of another way of doing it?
(Other than making SuperClass.getInt() protected, or copying and pasting the printInt() method into SubClass.)
1
+Pie Number of slices to send: Send
 

slow grammer wrote:Is it possible to override a private method using reflection?
I know how to invoke a private method, and I know how to get or set the value of a private variable, but I want to override the method.

I want the main method in SubClass to print out 2, but it prints out 1.
I don't think this can be done through reflection.
If not, does anyone know of another way of doing it?
(Other than making SuperClass.getInt() protected, or copying and pasting the printInt() method into SubClass.)



Unfortunately, one of the requirements of overriding is that the superclass method must be accessible from the subclass -- so, besides the options that you already named, I don't think so.

Henry
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1792 times.
Similar Threads
Inheritance and private variables
jtips 1, q26
call subclass method in superclass but not the other class
Call subclass method in superclass but not the other classes
method overloading issues
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:17:38.