wood burning stoves 2.0*
The moose likes Java in General and the fly likes Overriding a private method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding a private method" Watch "Overriding a private method" New topic
Author

Overriding a private method

Jerri Combs
Greenhorn

Joined: Feb 25, 2012
Posts: 2
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.)


Don't give up the day job.
(It's a valuable opportunity to catch up on sleep.)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18763
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
wood burning stoves
 
subject: Overriding a private method