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

  • SEARCH
  • Home
  • 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 8968475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:27:33+00:00 2026-06-15T17:27:33+00:00

In Java , is it possible to override methods in a class that you

  • 0

In Java, is it possible to override methods in a class that you create using reflection? For example, say I have the following class:

public class MyObject
{
    public String foo, bar;

    public MyObject(String foo)
    {
        this.foo = foo;
        this.bar = foo + "bar";
    }

    public void setBar(String bar)
    {
        this.bar = bar;
    }
}

And in one class I want to create it directly and override its setBar method as follows:

MyObject obj = new MyObject("something")
{
    @Override
    public void setBar(String bar)
    {
        this.bar = this.foo;
    }
};

Is there a way to override a method in this same manner using reflection? Maybe something like this? :

Class<?> _class = Class.forName("com.example.MyObject");
Constructor<?> _constructor = _class.getConstructor(new Class<?>[]{String.class});
Method m = _class.getMethod("setBar", new Class<?>[]{String.class});
Object obj = _constructor.newInstance("Foo String")
{
    m = new Method(new Class<?>[]{String.class})
    {
        System.out.println("Foobar");
    }
};

If not, are there other ways of doing this, or an external library that could help? I am looking for way to add listeners to a setter method in order to change binded values.

  • 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-15T17:27:34+00:00Added an answer on June 15, 2026 at 5:27 pm

    No, it’s not possible in the way of your example.

    In your example, the Java compiler will create two separate classes:

    MyObject.class
    MyObject$1.class
    

    The latter being the one with the overridden method. In this case, it’s an anonymous inner class (See Java tutorial documentation)

    But there is more complex solution involving bytecode weaving libraries. Libraries such as cglib, asm, javassist etc. give you a facility to dynamically create new classes at runtime and load them.

    Javassist has a tutorial on how to add methods to classes at runtime. It should be possible to adapt it to add/override the method, something like this:

    CtClass origClazz = ClassPool.getDefault().get("org.example.MyObject");
    CtClass subClass = ClassPool.getDefault().makeClass(cls.getName() + "New", origClazz);
    CtMethod m = CtNewMethod.make(
                 "public void setBar(String bar) { this.bar = bar; }",
                 subClass );
    subClass .addMethod(m);
    Class clazz = cc.toClass();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is possible in plain Java to override a method of a class programmatically
Is it possible to override system property (timezone etc.) in Java using predefined properties
Possible Duplicate: Java: Always override equals? should I override equals function for any class
I have the following code, partly in Java, partly in Scala: Java: public interface
Is it possible to create a web service operation using primitive or basic Java
If I have a class that implements java.lang.Runnable , there is technically nothing stopping
I've got problem in my code in Java. I have four(important) Classes: public class
Overriding Methods in java have the following features : 1> The Overrinding method should
I have a class that needs to be deserialized from JSON using Jackson. The
Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class

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.