Java Tip: The compiler cares about what methods are callable on the reference variable, not the actual object

This means that you can call methods on an object only if the methods are in the class/interface used as the reference variable type, regardless of the actual object type. So, if you have an ArrayList<Object> and you load it up with, say, Guitar objects that have a play() method, you cannot pull one out and call play() on it because the reference variable is of type Object. To do so, in this case, you’d have to cast the object to type Guitar first. A basic but oft-forgotten Java principle.

Advertisements

About buffalobillion

Web Developer, JavaScript Balrog, Java dude, Ruby/Rails enthusiast. Guitar Playa.
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s