1

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;
}
2
  • 2
    Define an interface with a getX method. Make all your classes implement that interface (possibly by returning the value of their field). Make your GetX method accept a parameter of the interface type and invoke its getX method. Commented Jan 15, 2015 at 17:51
  • 1
    Just as a coding standard, you shouldn't is a capital G for the method name. It should be getX Commented Jan 15, 2015 at 17:52

5 Answers 5

3

You have 2 options.

  1. Create an interface, say, Xholder, with one method, getX() that returns int. Have Car, Apple, and Bee implement Xholder by returning X. Then, your current GetX method is unnecessary; anything that would have called GetX(yourObject) would now call yourObject.getX().

  2. Use reflection in GetX. Get the object's class with getClass() and get the class's Field with getField, passing the variable name "X". From there you can get the value.

The first option is the best, because reflection is slower and more error-prone.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that is how i would normally do it by having a getter method inside the class of each type, but i need a generic method that can return a field from any object. Suppose i had a swing gui with many gui objects, and a method called GetWidth(Object ANYOBJECT), i would either have to define one method per type of object as in GetWidthOfButton(JButton button) etc... or come up with some generic method that works for any object...
2

Two ways off the top of my head:

  • Use an interface common to all objects that defines the getX() method
  • Use reflection

That been said, your method interface is not correct: you do not expect ANY object, but only those who have that X property. That means that the first solution is, from a design perspective, the most correct.

Comments

0

Make all your Classes extends from a base abstract class (Say MyObject which contains X) and make GetX parameter type be MyObject.

Comments

0

Assuming you know that you could do this by using Polymorphism concepts with an interface or abstract method (of an abstract class);

You could resolve this issue by using Java Reflections. You can study more about it here

Comments

-1

Why not just use the java.lang.Object as a Parameter?

1 Comment

Try to give an example when suggesting a solution to make it clearer. Thank you for contributing in Stack Overflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.