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

Java

Super Java

Can somebody explain me how the super keyword works in Java. I've been reading that on forums, but i can't understand it very well. Thanks, Kind regards.

1 Answer

In java, you can "extend" a class to make a child of the class. In this case, the original class is the parent class, also called the super class, and the class you just created this way is the child class/sub class. When you extend a class, you "inherit" all the properties of the parent class, like methods, variables, etc. Now, mostly the reason behind extending a parent class is to redesign some of the methods of the parent class, by adding some specific functionality to it. That saves you the time from having to re-write all the code that's already there in the parent class's method. You just need to add your own functionality to it.

In such a case, if you want to call the parent class's method, before you implement your own features, you use the super keyword. You can access the parent class's version of the method by super.methodName(). Similarly, you can also use super() to call a parent class's constructor, if you need.

Check out the official documentation on super here for good reference: http://docs.oracle.com/javase/tutorial/java/IandI/super.html