2023-03-23

Create a function with a generic input in Java (different potential inputs with same function names)

I am quite new to Java and read about the generic inputs and classes. I was wondering if we can have a function for example named as getNewWeights(genericInput) where the genericInput can be different class types but they have similar functions, as follows:

public static Double getNewWeight(Class<T> genericInput) {
    double weight = 0.0;
    //logic
    weight = genericInput.getCalcuatedWeight(INPUTS); 
    //logic
    return weight
}

while genericInputs of any type has same getCalcuatedWeight function.

for example: if genericInput class type is Car or Train both of these classes have a public function as getCalcuatedWeight that returns a double value.



No comments:

Post a Comment