Search code examples
javaandroidobjectmethodspass-by-reference

Is there a way to use the methods of a passed class object?


Im trying to reuse section of code that acts on an object of a class, i have multiple classes that for the most part have 90% the same method names, right now i have bunch of if else statements where the first line is unique and the rest are the same, is there a way i can pass the object and use its methods?

ive tried just taking the repeated code and putting into a method in my activity, but the method names are never recognized no matter how i reinitialize the object for example i would make a method called lookupclass(Object lookupObject) and paste the nonspecific code in there and just before that code ive tried many different attempts at creating an object out of the passed object, most will recognize the new object in the code but all of the methods (ex. lookup.loadClass(fileToLookup)) show up as not recognized

if (databaseFolder.equals(CustomerFolder)) {
            CustomerClass lookup = new CustomerClass();
            //region nonspecific code on object
            lookup.loadClass(fileToLookup);
            String[] tempString = new String[lookup.positionIds.length * 2];
            String[] tempExpandString = new String[lookup.positionIds.length];
            String[] classData = lookup.getClassData();
            String[] positionNames = lookup.positionNames;

            for (int i = 0; i < lookup.positionIds.length; i++) {
                tempString[i * 2] = positionNames[i];
                if (lookup.positionExpandableToMultipleBoolean[i] && classData[i] != null && classData[i].contains(",")) {
                    tempString[i * 2 + 1] = "Multiple- Click to Expand";
                    tempExpandString[i] = classData[i];
                } else if (lookup.positionExpandableToMultipleBoolean[i]) {
                    tempString[i * 2 + 1] = lookup.expandablePositionIdToName(i, classData[i]);
                    tempExpandString[i] = classData[i];

                } else if(classData[i] == null){
                    tempString[i * 2 + 1] = "   ";
                } else
                    tempString[i * 2 + 1] = classData[i];
            }
            list = tempString;
            filesToExpand = tempExpandString;
            //endregion

        } else if (databaseFolder.equals(AddressFolder)) {
            AddressClass lookup = new AddressClass();
            //region nonspecific code on object
            lookup.loadClass(fileToLookup);
            String[] tempString = new String[lookup.positionIds.length * 2];
            String[] tempExpandString = new String[lookup.positionIds.length];
            String[] classData = lookup.getClassData();
            String[] positionNames = lookup.positionNames;

            for (int i = 0; i < lookup.positionIds.length; i++) {
                tempString[i * 2] = positionNames[i];
                if (lookup.positionExpandableToMultipleBoolean[i] && classData[i] != null && classData[i].contains(",")) {
                    tempString[i * 2 + 1] = "Multiple- Click to Expand";
                    tempExpandString[i] = classData[i];
                } else if (lookup.positionExpandableToMultipleBoolean[i]) {
                    tempString[i * 2 + 1] = lookup.expandablePositionIdToName(i, classData[i]);
                    tempExpandString[i] = classData[i];

                } else if(classData[i] == null){
                    tempString[i * 2 + 1] = "   ";
                } else
                    tempString[i * 2 + 1] = classData[i];
            }
            list = tempString;
            filesToExpand = tempExpandString;
            //endregion

        }

Solution

  • A good way to do this would be via an Interface. You could define an Interface called Lookup which has the common method signatures defined. Then have your CustomerClass, AddressClass, etc. implement that interface. This would allow you to do something like:

    Lookup lookup = null;
    if (databaseFolder.equals(CustomerFolder)) {
        lookup = new CustomerClass();
    } else {
        lookup = new AddressClass();
    }
    lookup.loadClass(fileToLookup);
    //Rest of common code