How to combine two objects in Java?

Consider:

public class test01{ public void doSomething(){ // do something } } public class test02{ public void printSomething(){ // print something } } // in main test01 t1 = new test01(); test02 t2 = new test02(); // I want do to something like this test01 t3 = t1.merge(t2); // with t3 I should be able to access both t1 and t2 functions t3.doSomething(); t3.printSomething(); 

Please let me know if this is possible in Java? If so, let me know how I can achieve this.

+7
java design-patterns
source share
7 answers

There is no multiple inheritance in java. You can make test02 subclass of test01 , and then create test03 as a subclass of test02 .

OR

you can compose them in the class test03 as follows:

 public class Test03 { private Test01 test01; private Test02 test02; public void doSomething() { test01.doSomething(); } public void printSomething() { test02.printSomething(); } } 

Please note that in java you should not use class names like test01 . They must be meaningful and comply with the naming rules of the java class.

+6
source share

Perhaps your best option:

 public class TestSet { private test01 t1 = new test01(); private test02 t2 = new test02(); public void doSomething() { t1.doSomething(); } public void printSomething() { t2.printSomething(); } } 

Some languages ​​support multiple inheritance, which may be what you are looking for here. But not in Java. You may or may not want to create a pair of interfaces to bind TestSet together with test01 and test02.

+3
source share

Please let me know if this is possible in Java?

It's impossible. You cannot dynamically combine behavior in Java.

The usual way to combine behavior in Java is to use some combination of inheritance, wrapping, or delegation. Even then there will be a problem of subtyping ... if you are not using interfaces ... because Java does not allow a class to have multiple (direct) superclasses.

Consider the @Panzercrisis example. While the test03 class implements methods with the same signatures as the test01 and test02 classes, the test01 instance test03 not compatible with any of them. (You cannot use an instance of test03 as test01 or test02 . Java does not support duck printing!)

To solve the problem, you need to define the face01 and face02 , which are implemented by test01 and test02 . Then you implement test03 as implementing both face01 and face02 .

But these are all static classes and static typing.


In some cases, you can use DynamicProxy or something similar to “synthesize” a class that “combines” the behavior of two existing classes. However, all this is done using static types and code generation behind the scenes. Moreover, this approach would be viable if you had the prudence to define a bunch of interfaces (for example, face01 and face02 ) and write application code for interfaces, not implementation classes.

+2
source share

You can define class Y in another class X as an inner class, and you can use class Y in class X. Other than that, since I know there is no way to do this.

0
source share

The short answer is, isn't it how you describe.

This may be possible if test01 and test02 , where the interfaces and the third class test03 both implement. In C ++, this will be accomplished using multiple inheritance, but it will work in exactly the same way (i.e. you will need to create a third class that extends both), and this option is not available in Java anyway.

Another option is some kind of composition like @Panzercrisis.

Last option (I can think of): have test02 extend test01 , but that modifies test02 .

But actually, no, it's impossible.

0
source share

No, you can’t, really do it the way you describe it, you can do

Public class test01 extend test02 {....

}

so your test1, you can use methods from both classes, but if you can really dance with merging classes together, you can do some nasty things like this:

 public class MyObject { Map<String, Object> objects = new HashMap<String, Object>(); Map<String, Method> methods = new HashMap<String, Method>(); public Object execute(String methodAName) { Object object = objects.get(methodAName); Method method = methods.get(methodAName); if (object==null || method==null) { throw new RuntimeException("method not found"); } try { return method.invoke(object, null); } catch (Exception e) { throw new RuntimeException(e); } } public void merge(Object o) { for (Method m : o.getClass().getMethods()) { objects.put(m.getName(), o); methods.put(m.getName(), m); } } } 

and when you can use it that way

  MyObject o = new MyObject(); o.merge(new test01()); o.merge(new test02()); o.execute("printSomething"); o.execute("doSomething"); 

but as i said is not recommended

0
source share

You can do something similar with Java inheritance , but not the same. you can use extends here. But you must make sure that the redefinition method will not be repeated, otherwise your requirement will not be met.

 Test02 extends Test01 

Then

 Test03 extends Test02 

Now you can achieve something similar.

 Test03 test=new Test03(); test.callMethodInTest1(); test.callMethodInTest2(); 

Example:

 public class Test1 { public String callMethodInTest1() { return "test1"; } } 

.

 public class Test2 extends Test1{ public String callMethodInTest2() { return "test2"; } } 

.

 public class Test03 extends Test2 { public static void main(String[] args){ Test03 sample=new Test03(); sample.callMethodInTest1(); sample.callMethodInTest2(); } } 
0
source share

All Articles