1

How can I access variable outside a class that extends an other class that is used in an array list? I get an error that says that the variable does not exist in the extended class. Take a look, I want to access the variable members without having to declare it in my Object class:

public abstract class Object {
    public int x, y;
}

public class House extends Object {
    public int members = 10;
}

// Somewhere else
ArrayList<Object> list = new ArrayList<Object>();
list.add( new House() );
for (Object o : list ) {
    o.members;
}

The problem is that in the RTS I'm writing, my Object class has over 40 variables, just because it doesn't work to declare them only in the sub class and access them from outside. Hope you understand. How can I do this?

lawls
  • 1,498
  • 3
  • 19
  • 34
  • 3
    Don't name your class `Object`. What other classes extend `Object`? Do these classes have `members`, too? –  May 16 '13 at 14:07
  • @LutzHorn It's not really named Object in my program. Yes there are other classes that extend Object, and I also want those to be able to have their own methods and variables. – lawls May 16 '13 at 14:09
  • You can see my this answer if it can help http://stackoverflow.com/questions/15999934/get-variable-in-other-classes/16000005#16000005 This one also http://stackoverflow.com/questions/16153968/proper-way-of-accessing-variable-java-android/16154388#16154388 – Nikhil Agrawal May 16 '13 at 14:09
  • @Er.NikhilAgrawal Your posts don't solve the problem which is about members, inheritance, and visibility. –  May 16 '13 at 14:12

5 Answers5

8

You can use instanceof and a cast:

for (Object o : list ) {
    if (o instanceof House) {
        h = (House) o;
        h.members;
    }
}

However, this is often considered bad design; you should consider defining an appropriate method in Object (which should really have another name, as others have pointed out) and override it in House. Then, you may call that method on an Object without knowing what kind of object it refers to, and if it is a House, the correct method will be called. (Learning how to do this properly, and when to do it, takes a bit of practice - google polymorphy and overriding.)

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • Thank you for this! So I should rather keep all my 40 variables in Object than doing it the way you proposed? – lawls May 16 '13 at 14:24
  • @lawls: No; keep the variables in the classes where they belong. However, one often finds that the information one needs can be acquired in a class-independent manner. Let's say that another `Object` subclass is `Car`, with a variable `int passengers`. You can define an `abstract` method in `Object` called `getOccupants()`, which you override in `House` to return `members` and in `Car` to return `passengers`. Now, you can ask for the number of occupants inside your object without knowing whether it is a `House` or a `Car`. – Aasmund Eldhuset May 16 '13 at 14:44
  • @lawls: (Sometimes, though, it is not possible to define such a "common" method, so judicious use of `instanceof` is allowed. For more complex scenarios, you can consider the [Visitor pattern](http://en.wikipedia.org/wiki/Visitor_pattern); however, don't bother with it yet if you're a beginning programmer.) – Aasmund Eldhuset May 16 '13 at 14:47
3

First do not name your class Object (see the comments). You cannot access member of an Object in your code, because Object has no field member, House has. But an Object does not have to be a House, so it is not guaranteed that it has member.

If you're sure that in this case youre Object is always a House, cast it:

    ((House) anObject).member;

This way the compiler assumes that you know more than he does about the actual class of the Object and handles it as if it was a House. You can use instanceof to check if the cast is valid.

LionC
  • 3,106
  • 1
  • 22
  • 31
0

Hope you understand that you are using the name for your class as 'Object', which is the parent class for all the classes in Java. Now in your environment there will be two Object classes one which java provides from java.lang.Object and another one you have created. So when you are trying to access your class object and trying to get the attributes of that, it is actually not your class object rather it is an instance of java.lang.Object and hence you are running into an issue.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

You have to cast o to a House. E.g. ((House) o).members

Peter Berg
  • 6,006
  • 8
  • 37
  • 51
0

just cast the member of arraylist like this

((House)o).members;
stinepike
  • 54,068
  • 14
  • 92
  • 112