Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Reflection My Only Option?

 
Dave Schick
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following scenario (please bear with me on the setup, here)...

Class named "House" has a public static variable named "address". House is a subclass of Building. Building has no variables named "address".

I want to be able to access House.address from Building. Is Reflection my only option? E.g., Building bldg = new House(); I can't say bldg.address to get the House.address variable. Could use reflection, but is there another way that I'm missing?

Goal is to keep Building generic so I can retrieve the "address" of ANY subclass that extends Building.

Any help is appreciated.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is my thought on this
If you need Generic approach then "address" attribute should belong to Class Building.
Because every type of building would certainly have an address.

Regards,
Amit
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it may be better create a method call getAddress() and have the building implement a generic one (or have the method as abstract) -- then the subclasses can override the method.

And BTW, having a *static* variable for address may not be what you want -- as it is likely that different instances of house have a different addresses.

Henry
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Schick:
I want to be able to access House.address from Building.
Here's your main problem: why does the Building class know anything about the House class? In general a class shouldn't include any knowledge about details of its subclasses.

Anyway, if the Building class has a reference to a House object it doesn't need to do reflection to get the address variable of that object. This code works anywhere:
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one way to handle it

Now this is not a good example. In fact "instanceof" is an example of things we like to avoid. The other suggestions above are better.

*Move address up to Building so all Buildings have addresses

*Give Building a getAddress() method that returns nothing, override in House

*Give Building an instance of House - aggregation instead of inheritance

Scroll down to the OO, UML, etc forum for a group who loves to talk the theory and practice of OO design. These suggestions don't just show up in text books, they work better in real apps, too.
 
Dave Schick
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify -- the Building and House isn't a real production example, that was just for illustration purposes.

In my production scenario, I will NOT know what types of subclasses are possible, so I can't use instanceof, etc.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If, as you said, the address variable is public and static, you don't need an instance at all - you can just access it from anywhere using

House.address

I have a hard time imagining any situation where this design would make sense. But that's the simplest answer to the question that was asked.
[ October 31, 2007: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic