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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:10:04+00:00 2026-06-15T14:10:04+00:00

I have some class public class myClass implements A, B where A and B

  • 0

I have some class public class myClass implements A, B where A and B both contain a method public int doSomething();, but A.doSomething is specified by the interface to do something different than B.doSomething.

I have read Two interfaces with same method signature implemented in Java class, but that doesn’t really address my problem, because the methods are overriden to do the same thing, but as I said above, my question is about when they are specified in the interfaces to do different things.

For example, suppose A.doSomething() is supposed to return 0, while B.doSomething() is supposed to throw an exception, and violating either one wold cause problems for methods that are supposed to take them as parameters.

Is there any way to do this in java? If so, how would one actually do it?

  • 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-15T14:10:05+00:00Added an answer on June 15, 2026 at 2:10 pm

    You might be able to use a Proxy instance to do this. See this question for info on Proxy (particularly, the second part of the answer.)

    The InvocationHandler that you write would check to see which interface is being used to call the method and delegate to the appropriate method inside your object. Here’s what your implementation looks like:

    public class MyClass {
        // Note that we aren't implementing the interfaces anymore
    
        public int doSomethingForA() {
            return 0;
        }
    
        public int doSomethingForB() {
            throw new IllegalArgumentException();
        }
    }
    

    Then your InvocationHandler:

    public class MyClassInvocationHandler implements InvocationHandler {
    
        private MyClass target;
    
        public MyClassInvocationHandler(MyClass target) {
            this.target = target;
        }
    
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            try {
                if (method.getDeclaringClass().equals(InterfaceA.class))
                    return MyClass.getMethod("doSomethingForA").invoke(target, args);
                else if (method.getDeclaringClass().equals(InterfaceB.class))
                    return MyClass.getMethod("doSomethingForB").invoke(target, args);
                else
                    throw new UnsupportedOperationException("Unsupported interface: " + method.getDeclaringClass());
            } catch (NoSuchMethodException ex) {
                throw new UnsupportedOperationException("Method not found", ex);
            } catch (IllegalAccessException ex) {
                throw new UnsupportedOperationException("Method was not public", ex);
            } catch (InvocationTargetException ex) {
                // May throw a NullPointerException if there is no target exception
                throw ex.getTargetException();
            }
        }
    }
    

    Then to create the proxy, you’ll pass in both interfaces:

    Proxy.newProxyInstance(null, new Class<?>[] { InterfaceA.class, InterfaceB.class }, new MyClassInvocationHandler(mc));
    

    I think this will work. When you call it using one interface or the other:

    MyClass mc = new MyClass();
    Object proxy = Proxy.newProxyInstance(null, new Class<?>[] { InterfaceA.class, InterfaceB.class }, new MyClassInvocationHandler(mc));
    InterfaceA a = (InterfaceA) proxy;
    a.doSomething();
    InterfaceB b = (InterfaceB) proxy;
    b.doSomething();
    

    Then it should pass in Method objects with different declaring classes. I’m not sure if this is how it works though, so this will need to be tested.

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

Sidebar

Related Questions

So I have a class is something like this public class Order { //some
In Java if you were to have the statement: public class MyClass implements LargerClass
If say I have some generic class, for example: public class Attribute<T> { }
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
I have a std::vector of some class of the form class A{ public: A():i(someNumber){}
Let's say I have some model objects that resemble this: public class FooModel {
I have some questions with relationship of ef code first. My code: public class
Suppose I have a class that processes some data: class SomeClass { public: void
I have a simple Java class that has some methods: public class Utils {
Assume some domain and view objects (that have no common base class) public 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.