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 8387743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:17:52+00:00 2026-06-09T18:17:52+00:00

A service from external API which I am not allowed to modify returns me

  • 0

A service from external API which I am not allowed to modify returns me

  MyClass instance = ServiceUtil.getThing();

I would like to extend this returned class and Add/Override a method but leave intacts the others, say 150 methods.

  private class MyWrapperClass extends MyClass(){
      public MyWrapperClass(){super();}
      @Override public String toString(){ return "Blocked toString"; }
  }

Is there any way to force this “casting” from the returned MyClass instance to my newly particular subtype??

NOTE: Please, not suggest the approach of making a constructor, passing the original object and having to copy and implement the 150 methods to call the wrapped object

  • 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-09T18:17:54+00:00Added an answer on June 9, 2026 at 6:17 pm

    If MyClass is an interface look at java.lang.reflect.Proxy and java.lang.reflect.InvocationHandler.

    You can implement a dynamic proxy that always does the same. It is, always pass control to your original implementation… except when method X is invoked.

    Something like:

    class MyHandler implements InvocationHandler {
      Object invoke(Object proxy, Method method, Object[] args) {
        if (method is the one you want to change) {
           do whatever
        else
           return method.invoke(originalObject, args);
      }
    }
    

    Note: you must create this proxy implementation anyway:

    MyClass original = ServiceUtil.getThing();
    MyClass proxy = (MyClass) Proxy.newProxyInstance(
                                              MyClass.class.getClassLoader(), // classloader to use
                                              new Class[] { MyClass.class }, // interfaces to implement
                                              new MyHandler()); // who does the dirty work when methods are invoked
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to directly capture JSON from an external API in a service layer,
Bellow is my code from asp.net service which is trying to run some external
I Have an internal SOAP Web service that is being called from an external
I have a PHP script that grabs data from an external service and saves
I have a problem with deserializing JSON data from an external REST service. Depending
I'm writing a service (say, service.com) that provides a REST API to external apps
From within PHP, how can I call an external JSON web service and then
The scenario is around calling an external SSL SOAP web service from within Mirth.
Having problem to open form in new window, script is pulled from external service.
I am calling an external REST service (Vimeo REST API). The response of the

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.