2

Problem Statement: We have multiple class files (~200+), which each implement different methods and some use the methods from each other also to implement their own methods.

We wish to call multiple of these class methods (~20-30) in our code and have them deliver functionality like writing to a UI Web page and reading from a web page.

As we wish to make this process simple, we want a single class/interface which contains all these multiple class references (~200+), and we just need to import/include this single class/interface in our code and then using it to reference all the methods in the class files.

Question: Is the above possible? If so, how - by using a class or interface or something else?

I have provided examples and information of what we have tried here: Implement multiple classes in the same interface in Java?

Basically, we have tried

  1. Creating a Class which has all the other class declared in it
  2. Creating an interface, which has implementations across the multiple classes (the ~200+ mentioned above)
  3. Implemented a class and an interface

Nope of the ways seem to work.

As a gist of what we want in code, have provided an example below.

Example (this is not the right code or implementation, just wanted to provide an example of what we want to do in code):

I have 2 classes called "ABC_FamilyGivenName" & "ABC_DOBGender". I have created an interface "Common".

I want to use the methods in both the above classes in my code in class "Application".

With the current implementation, Java wants me to add an @Override to both the ABC_FamilyGivenName & ABC_DOBGender. This again creates an overhead, if we have ~500 such methods, and will thus need to override all ~500 in each of the c~200 class files??

***INTERFACE***:

public interface Common {
    void ABC_GivenNames();
    void ABC_FamilyNames();
    void ABC_Gender();
    void ABC_BirthDay();
}



***IMPLEMENTATION CLASSES***:

**ABC_FamilyGivenName**
public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

    public void ABC_FamilyNames(){
        // Implementation code
    }
}

**ABC_DOBGender**
public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

    public void ABC_BirthDay(){
        // Implementation code
    }
}



**USE IMPLEMENTED CLASS IN CODE**:
public class Application extends Base {

    Common commonClass = new ABC_FamilyGivenName();
    /* *DO I NEED THIS? I THINK I DO, BUT CODE/JAVA SAYS I DO NOT*
     * Common commonClass = new ABC_DOBGender();
     */

    public void ELP_C0050_PassportDetails(){
        commonClass.ABC_GivenNames();
        commonClass.ABC_FamilyNames();
        commonClass.ABC_DOB();
        commonClass.ABC_Gender();
    }
}
gagneet
  • 35,729
  • 29
  • 78
  • 113
  • The one thing that I do not quite grasp with your code, is that you have a "Common" that has gender and familyname implement. An interface is used to implement common methods to similar, yet different classes. So it would make more sense to have either 1 class for both, or two separate interfaces for each. Your interface forces both of the implementing classes to have all of the overridden abstract methods. – SynchroDynamic Nov 28 '17 at 04:38
  • @SynchroDynamic yes as mentioned this is just what we were thinking of doing, but is not a solution to our problem. The code I have provided does not do what we need as a solution to our problem, and was hoping we can get some idea of implementing it in a way that makes it simpler for Users to use and not remember multiple classes to import and then instantiate as new() – gagneet Nov 28 '17 at 04:57
  • Well in your instance, based on what I think you are trying to do, you should consider building component classes. This way you could build interfaces to get a particular object type. I would be happy to put an example up. It may help. It may not. – SynchroDynamic Nov 28 '17 at 05:11

1 Answers1

1

So there is a lot more you can do with this, but this is a very simple example of what I was talking about. Instead of having an interface over them, plug them in.

//Component Classes

public class DOBGender {//Has the specific methods you want

}



public class FamilyGivenName {//Has the specific methods you want

}

//Then just plug them in to this class

public class Person {

    DOBGender dobGender;
    FamilyGivenName fgn;

    public Person(DOBGender dobGender, FamilyGivenName fgn) {//Plug them in
        this.dobGender = dobGender;
        this.fgn = fgn;
    }

    public DOBGender getDobGender() {
        return dobGender;
    }

    public void setDobGender(DOBGender dobGender) {
        this.dobGender = dobGender;
    }

    public FamilyGivenName getFgn() {
        return fgn;
    }

    public void setFgn(FamilyGivenName fgn) {
        this.fgn = fgn;
    }
}

Like I said previously, This can be made in a much more complex way, but would also make coding of so many classes slightly easier for a user. If this is not the route you wanted to go, I can delete this one and take a new approach. If you want me to extend upon this idea, I can do that too.

I hope this helps.

EDIT:

This is what I am referring to if you just want to call Person and start using the component methods.

    public class Person {

    DOBGender dobGender;
    FamilyGivenName fgn;

    public Person() {
        dobGender = new DOBGender();
        fgn = new FamilyGivenName();
    }

    public DOBGender getDobGender() {
        return dobGender;
    }


    public FamilyGivenName getFgn() {
        return fgn;
    }

}
SynchroDynamic
  • 386
  • 2
  • 14
  • Thanks, this helped out a lot. Is there a way to make this simpler for the end user? Basically, possible to remove the constructor and just declare and use the 2 class/methods? – gagneet Nov 28 '17 at 05:57
  • You could always remove the constructor, then use the setters to set the properties. You can also instantiate them inside the Person class if you would like. This would allow you to call The Person Class, which would in turn create the two objects. You would get the result you are looking for then I think. – SynchroDynamic Nov 28 '17 at 06:04
  • 1
    I added an additional class concept to person. This would allow you to start using the components immediately. The only warning here is if you do it like this, in the future if you have to change something, it will be much more rigid. – SynchroDynamic Nov 28 '17 at 06:12