Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Rasmus Ostenfeld Firla-Holme
1,510 PointsWhat does "super" do
So I've gone though the first few stages of the Android track and it's been very educational. But I don't get what super.on[Create/Resume/Pause] does. I've tried finding the answer elsewhere but I just don't get it.
If somebody would explain this to me as if I were 6 years old, I'd be very grateful.
3 Answers
Stephen Whitfield
16,771 PointsThe super call basically says "call my parent's version of this method instead of mine." If you inherited from another class, you'll be able to make callbacks to the original methods from the class you inherited from with the super call.
Here's an example in Ruby from my Codecademy lesson:
class Creature
def initialize(name)
@name = name
end
def get_name
@name
end
def fight
return "Punch to the chops!"
end
end
# Add your code below!
class Dragon < Creature
def fight
puts "Instead of breathing fire..."
super
end
end
spitting=Dragon.new("Charles")
spitting.fight
#~> Instead of breathing fire...
#~> Punch to the chops!
Ben Jakuben
Treehouse TeacherHopefully Stephen's answer helps explain this. In terms of our Android Activities, it stems from the fact that our MainActivity is a child of a regular Activity class. When onCreate() in our child class is called, our method overrides the method from the base (or parent) class, which means our code gets executed instead. In these instances, there might be code in the base class that we want to execute too, which we do by calling the "super" version of the method. "super" means "base" or "parent"--it's just the specific keyword used to indicate that we're doing something with the base class.
this -> the current class
super -> the parent of the current class
All these different terms that mean the same thing can definitely be confusing when they're new. :)
Rasmus Ostenfeld Firla-Holme
1,510 PointsHmm... I'm still unsure if I get it yet.
So when I write:
public void onPause(){
super.onPause();
mSensorManager.unregisterListener(mShakeDetector);
I first create a method called OnPause. But instead of making my own onPause code or copy/pasting code from the Activity class, I simply refer to it by using the 'super' call? Or am I completely off track?
Ben Jakuben
Treehouse TeacherLet's imagine you don't have any onPause() code in your Activity. The system will still call that method for all sorts of reasons (let's say you get a phone call while using the app). The call will be handled by the base class you are extending: Activity. Because our MainActivity is a child of Activity, we do everything an Activity does plus anything extra we add.
So then we override the method and add our own onPause() method. Now if the system calls this method for a phone call, our code in our custom Activity will handle it. If we don't call super.onPause(); then only our code will run. Adding that call makes sure any code in the base class runs, too. The reason we normally want to do this is there may be code that we don't know about that is supposed to do something for us.
If you call it first like your example, then Activity's onPause() method will run, and then your code (mSensorManager...) will run. If you instead had called it later then your code would have run first. We typically call the super methods first thing.
Hope this helps more, but keep asking questions if we're leaving holes in your understanding!
Rasmus Ostenfeld Firla-Holme
1,510 PointsYes, that makes perfect sense. This question has been on my mind all day and now I get it! Thank you, Ben, you're a great teacher.
Ben Jakuben
Treehouse TeacherGlad to help! Thanks for being a student!
Andrew Chalkley
Treehouse Guest TeacherAndrew Chalkley
Treehouse Guest TeacherActually the above code would just return the string: "Instead of breathing fire..."
You cannot call anything after you
returnsomething. You'd callsuperbefore hand and even then it would just return "Punch to the chops!" in the above example.Stephen Whitfield
16,771 PointsStephen Whitfield
16,771 PointsWhoops! That's supposed to be a "puts". Thanks for pointing that out.