stt
stt

Reputation: 21

how to write a common method for two generated classes in different packages but all same methods in Java

So I have two XSD generated classes. They have all methods common and all methods of both classes have the same parameters as well. They are in different packages. How can I write a common method where I can do it once for both classes. I am trying to present the scenario as below:-

package a.b.c;
class x{
void m(){};
int n(){};
string o(){};
}

Other one

package a.b.d;
class x{
void m(){};
int n(){};
string o(){};
}

Upvotes: 2

Views: 611

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 76424

If the two classes share a method, that sounds very much like they need to have a common ancestor class, having that method and extend that base class by both your classes. You can of course extend classes of different packages:

public class ChildClass extends com.the.other.package.BaseClass {
    public void foo() {
        //...
    }
}

Upvotes: 1

Related Questions