Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:32:53+00:00 2026-06-17T06:32:53+00:00

I have build a subclass from a class in Java that has private methods

  • 0

I have build a subclass from a class in Java that has private methods which I want to access in the subclass, I cannot change or edit the superclass. The problem is of course they are private. Suppose I have written the superclass by myself and there were certain reasons why these methods have to be private. I could copy the code in the subclass. But is there a better way (without producing so much lines of code) to get able to work with them when writing a subclass?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:32:55+00:00Added an answer on June 17, 2026 at 6:32 am

    Ignoring that your reasons for wanting to do this are potentially very bad (there’s no contract for your usage of private variables, so there are no guarantees that they won’t change, or disappear completely!) you could probably do what you want using reflection:

    Using:
    http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getDeclaredField(java.lang.String)
    http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/AccessibleObject.html#setAccessible(boolean)
    http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Field.html#get(java.lang.Object)

    Class c = object.getClass();
    Field field = c.getDeclaredField("somePrivateInstanceVariable");
    field.setAccessible(true);
    Object someValue = field.get(object);
    

    I just want to emphasise that you should consider the reasons for doing this and decide against it! If you own the code that you are extending, consider if you should instead make the field protected instead of private. Remember, hooking into code you’re not supposed to have access to breaks OOP principles (you’re circumventing encapsulation) and there are no guarantees the code your application depends on won’t disappear in an update to the library (so you’re also locking yourself down to a fixed version of the lib).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ParentBean class which has the @ManagedBean annotation. If I subclass this
I have a base class that has class methods defined for returning various pieces
I have a cube that I have build that has data across multiple servers.
I am working on a project where I have a class which has UIView
I am trying to build a simple abstract Cache class that can enforce methods
I'd like to have a BaseViewController class (that inherits from UIViewController ) for my
I have a very simple java applet that I took from here: http://docs.oracle.com/javase/tutorial/deployment/applet/subclass.html import
I have a handful of view controllers that inherit from a subclass of UIViewController,
I have build C# program that work with RAPI (communication to PPC or WinCE)
I have build a c# class library verification.dll using OpenCVSharp. This references OpenCvSharp.dll in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.