Suppose i have a project with many objects of different kinds, but they all have an instance variable X, how do i make a method that accepts an object of any type, and returns X?
Dummy code example:
class Car{
int X =19;
}
class Apple{
int X =21;
}
class Bee{
int X =32;
}
public int GetX(Object ANY_OBJECT_HERE){
return ANY_OBJECT_HERE.X;
}
getXmethod. Make all your classes implement that interface (possibly by returning the value of their field). Make yourGetXmethod accept a parameter of the interface type and invoke itsgetXmethod.getX