Skip to content
November 24, 2009 / Ben Chun

super.super

Last week I introduced the super keyword in my AP Computer Science class. Actually, it came up in the book a few weeks back, but it suddenly became relevant because it appeared on a quiz. And naturally in class someone asked if you could use super.super to get to the grandparent. Instead of answering, I left the question open as part of an extra credit assignment. (Since, after all, it is a computer science class and they have all the tools necessary in front of them to do the experiment.)

Best answer so far:

I did this at Starbucks (which explains the frappuccino thing). After some probing I learned super only goes up one level (for methods anyways). Apparently it is nearly impossible to go back to the top from a third level (ECExtension2 in this case), so I made a method in ECExtension called drinkEC() that would go back to its top drink() (the highest drink()). It’s usefulness lies in letting me say “delicious” two different ways AND still being able to say I am drinking a frappuccino. All in one inheritance tree.

My favorite way to explain why super.super isn’t legal is to ask students to consider a class with no explicit superclass. By default, it’s superclass is Object. And so if you call super.super.something(); from an instance of this class, what is the parent of Object we should consult for the something() method? Oh, right.

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

%d bloggers like this: