1

I have a requirement to invoke two different methods from two different classes to my own class and those two classes are in two different .jar files provided by the vendors.

The scenario is something like below:

  • abc.jar: public class A{ public void m1(){} }
  • xyz.jar: public class B{ public void m2(){} }

My own class is MyClass{}. If I have to use those two methods in my class I need to extend both the classes, but since java does't support multiple Inheritance, I am not able to achieve it.

Please share if there is any other way to access both m1() & m2() methods in my class. Note: I have to extend the available functionalities and should customize the methods according to my requirement.

Nirmal
  • 43
  • 1
  • 1
  • 9
  • why not use composition pattern? is it necesarry to use herency? why not just create your own methods and call them using for example new A.methodA(), have a look at this: http://www.journaldev.com/1325/what-is-composition-in-java-java-composition-example – Yussef Apr 17 '17 at 15:03
  • @Yussef I am looking for inheritance because I have to add new functionalities to the existing methods. The only way would be overriding I guess. – Nirmal Apr 17 '17 at 15:57

2 Answers2

1

Edit: The question was clarified later, the OP wants to extend the functionalities of the classes.

One idea would be to create two separate sub-classes for the two different base classes. Extend the functionalities in these sub-classes and then use composition in your MyClass to use the functionalities of these sub-classes and their super-classes.

public class SubA extends A {
    @Override
    public void m1() {
        // add extra functionalities
        super.m1(); // invoke existing functionalities
    }
}

public class SubB extends B {
    @Override
    public void m2() {
        // add extra functionalities
        super.m2(); // invoke existing functionalities
    }
}

public class MyClass {
    SubA a = new SubA();
    SubB b = new SubB();
    a.m1();
    b.m2();
}

You can also refer to this question How do Java Interfaces simulate multiple inheritance? for detailed explanation of how to implement multiple-inheritance in Java.

Earlier answer:

Both the methods are public, so you don't need to extend the classes to use those methods. Just create an object of those two classes and call their respective methods.

A a = new A();
a.m1();

B b = new B();
b.m2();
Community
  • 1
  • 1
iavanish
  • 509
  • 3
  • 8
  • I agree with @iavanish since you are not talking about interfaces. But If you want to learn how Java 'simulate' multiple inheritance basically supported by a middle-interface give a look to this post. http://stackoverflow.com/questions/3556652/how-do-java-interfaces-simulate-multiple-inheritance – Sam Apr 17 '17 at 15:12
  • @iavanish I appreciate your answer but my requirement is to customize those given methods(my bad didn't mention that earlier). So according to my understanding, I got to override them using Inheritance. Please suggest... – Nirmal Apr 17 '17 at 16:03
0

Maybe I will do this,

class A{
  public void m1{};
}

class B{
  public void m2{};
}

class OneOverrideA extends A{
    @Override
    public void m1{};
}

class OneOverrideB extends B{
    @Override
    public void m2{};
}

class FatherClass extends OneOverrideA{

  //well you have your class method m1 here but we cannot extend more than two class

  public void m2{new OneOverrideB().m2()};

}

Although it is not awesome in my point of view, anyway why would you want to override both methods? if you're going to do that why not create them from zero? or are you going to use other methods from those class? nvm I would use composition anyway.

Hope it can re fresh your mind and get something from here :) GL

Yussef
  • 610
  • 6
  • 11