Need a bit of help understanding this OOP code

I have code like this

			if(hallOfFameMemberList.get(i) instanceof HonoraryHOFMember)
					HonoraryHOFMember hof = new HonoraryHOFMember();
					hof.SetAthlete() //The SetAthlete method is specific to the HonoraryHOFMember

hallOfFameMemberList is a ArrayList of type HallOfFameMember that holds different types of HOFmember objects like HonoraryHOFMember. HonoraryHOFMember has all HallOfFameMember attributes/methods along with its own since it extends HallOfFameMember. From the arraylist “hallOfFameMemberList”, how can I access the methods within the HonoraryHOFMember objects that are held in the ArrayList "HallOfFameMemberList, which is “i”? “hallOfFameMemberList.get(i)” will give me access to only HallOfFameMember methods since it’s of type HallOfFameMember.

While you can access the members of a List this way, it’s not The Right Way™.

Use the nifty new extended for:
for( Item item : items ) {
// do stuff with item

or the old iterator for:
for( Iterator<Item> i = items.iterator(); i.hasNext(); ) {
Item item =;
// do stuff with item

That said, once you’ve made your check with instanceof, you may cast a HOFMember to an HonoraryHOFMember. The instanceof check is not required to cast, but given that you have a ‘mixed bag’ of Objects, you will run into ClassCastExceptions if you don’t, in this case.

Also, methods names begin with a lowercase letter, Class Names Begin With Uppercase.

I’m still having trouble understanding this

I did this

ArrayList<HallOfFameMember> hallOfFameMemberList = new ArrayList<HallOfFameMember> ();

for(HallOfFame member: hallOfFameMemberList) {

if(hallOfFameMemberList.get(i) instanceof HonoraryHOFMember)

/*The HonoraryHOFMember extends HallOfFameMember*/

					HonoraryHOFMember hof = new HonoraryHOFMember(); /*hof gives me all HallOfFameMember   and HonoraryHOFMember methods*/
					hof.SetFirstName(member.GetFirstName()); /*HallOfFameMember method*/
					hof.SetLastName(member.GetLastName()); /*HallOfFameMember method*/
					hof.SetDeceased(member.isDeceased()); /*HallOfFameMember method*/
					hof.SetDateOfBirth(member.GetDateOfBirth()); /*HallOfFameMember method*/
					hof.SetAthlete(/*I want to pass isAthlete() from member, but isAthlete is specific to HonoraryHOFMember . I only have access to HallOfFameMember methods*/) 
/*The SetAthlete method is specific to the HonoraryHOFMember*/


I wouldn’t need to upcast a member HOFMember to an HonoraryHOFMember since every HonoraryHOFMember has its methods right? Sorry for being a bit confused here. I want to access the isAthlete() method from member which is of type HallOfFameMember. I can find out if hallOfFameMemberList.get(i) is and instance of HonoraryHOFMember, but it doesn’t get me the method specific to its class, isAthlete(). I need isAthlete() to pass into hof.SetAthlete(), so I can return calculations with another method that calls isAthlete() to return certain results.

I got sidetracked and ended up just glossing over your question…

Okay, so here’s the thing, when you say

MyObject myObject = someObject;

the only thing the Java compiler checks is whether or not someObject can be assigned to a reference of type MyObject.

HonoraryHOFMember is a HOFMember, so it may be assigned to a HOFMember reference.

That covers what you know, here’s what you missed:

It is a HOFMember reference, HOFMembers don’t have a setAthlete method. Read that again, let it sink in.

Even though HonoraryHOFMembers have a setAthlete method, Java is not going assume that when you call setAthlete on a HOFMember reference that this time, the reference is pointing to an HonoraryHOFMember.

Therefore, you must cast to an HonoraryHOFMember and (this is the important bit) assign it to an HonoraryHOFMember reference to be able to call setAthlete.

We think we’re calling the Object’s method, but in fact we’re calling the reference’s method. Reference type doesn’t have that method? “cannot find symbol” says the compiler.

Also, if you keep the “hallOfFameMemberList.get(i)” code, you’re going to be upset.

As far as I can see in your code, i is no longer being updated, so you will always get the same object.

Not to worry, now member, from for(HallOfFame member: hallOfFameMemberList), is the ‘object in hand’, just update your code to reflect that.

Thank you!! This cleared some things up for me