Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I 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 yes, then let me know how can I achieve this?

like image 453
Muthu Avatar asked Nov 27 '13 14:11

Muthu


3 Answers

There is no multiple inheritance in java. What you can do is making test02 a subclass of test01 then create test03 as a subclass of test02.

OR

you can compose them into a Test03 class like this:

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 shouldn't use class names like test01. They should be meaningful and comform to the java class naming guidelines.

like image 91
Adam Arold Avatar answered Sep 21 '22 07:09

Adam Arold


Your best option is probably this:

public class TestSet {
    private test01 t1 = new test01();
    private test02 t2 = new test02();

    public void doSomething() {
        t1.doSomething();
    }

    public void printSomething() {
        t2.printSomething();
    }
}

In some languages, multiple inheritance is supported, which may be what you're looking for here. But not in Java. You may or may not want to make a couple of interfaces here to tie TestSet more closely together with test01 and test02.

like image 42
Panzercrisis Avatar answered Sep 22 '22 07:09

Panzercrisis


Please let me know if this is possible in Java?

It is not possible. You cannot combine behaviour dynamically like that in Java.

The normal way to combine behaviour in Java is to use some combination of inheritance and wrappering or delegation. Even then, there will be an issue of subtyping ... unless you use interfaces ... because Java does not allow a class to have multiple (direct) superclasses.

Consider for @Panzercrisis's example. While his test03 class implements methods with the same signatures as the test01 and test02 classes, an instance of test03 is not type compatible with either of them. (You can't use a test03 instance as a test01 or a test02. Java doesn't support duck typing!)

To address that you would need to define interfaces face01 and face02 that are implemented by test01 and test02 respectively. Then you would implement test03 as implementing both face01 and face02.

But this is all static classes and static typing.


Under some circumstances, you could use DynamicProxy or something similar to "synthesize" a class that "merges" the behaviour of two existing classes. However, that is all done with static types and code generation behind the scenes. Moreover, this approach would only viable if you'd had the foresight to define a bunch of interfaces (e.g. face01 and face02) and write your application code against the interfaces rather than the implementation classes.

like image 28
Stephen C Avatar answered Sep 22 '22 07:09

Stephen C