40

What's the meaning and usage of the super keyword in Java?

exebook
  • 32,014
  • 33
  • 141
  • 226
Tushar
  • 5,907
  • 15
  • 49
  • 81
  • 1
    Android apps are (usually) written using the Java language. Search for the meaning of `super` in Java and you should have everything you need. Hint: `super` has multiple uses. – Joachim Sauer Feb 28 '11 at 11:28
  • possible duplicate of [super() in Java](http://stackoverflow.com/questions/3767365/super-in-java) – Fenikso Jan 17 '14 at 13:07

5 Answers5

64

super is a keyword in Java. It refers to the immediate parents property.

super()            //refers parent's constructor
super.getMusic();  //refers to the parent's method

- Read More on super

CaptJak
  • 3,592
  • 1
  • 29
  • 50
jmj
  • 237,923
  • 42
  • 401
  • 438
40

The super keyword refers to the instance of the parent class (Object, implicitly) of the current object. This is useful when you override a method in a subclass but still wants to call the method defined in the parent class. For example:

class ClassOne { 
     public say() { 
      System.out.println("Here goes:");
  }
}

class ClassTwo extends ClassOne { 
          public say() {
            super.say();
            System.out.println("Hello"); 
   } 
 }

Now, new ClassTwo().say() will output:

Here goes:
Hello

As others have mentioned, super() will call the parent's constructor, and it can only be called from the subclass' constructor (someone correct me if I'm wrong).

Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
Felix
  • 88,392
  • 43
  • 149
  • 167
4

The super keyword is not specific to Android. It's a concept belonging to OOP, and represents the parent class of the class in which you use it. In Android, it's mostly usefull when you create your own Activity or component, and lets you call a default behavior before implementing yours.

For instance, the super methods must be called before anything when you override the Activity onPause, on Resume, onStop etc... methods.

For more information, I suggest you take a look at Object Oriented Programming books, as well as Java Programmation books, which should cover the subject more deeply.

XGouchet
  • 10,002
  • 10
  • 48
  • 83
4

When you inherit from another class, you might want to override a method from the class you inherit from.

When doing this, you sometimes need the overrided method to be called first, and then do something after. In that case, you write super();

Here is a few links on inheritance, it is very important in programming: Wiki's Inheritance

A short explaination from Oracle

mlz7
  • 2,067
  • 3
  • 27
  • 51
Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
2
  1. Simply to say as name implies "super" points to the super class i.e. immediate parent class.

  2. It is meant to obtain immediate instance of super class either it may be method or variables.

  3. As i pointed super can be used to retrieve instance of immediate parent class in case of multilevel inheritance it will not work if you are trying to get instance of extreme parent class.

  4. you need super only when parent and child classes have methods of same name and type

  5. You may aware that constructor will be created automatically created by compiler once class is declared in same way super() will be created by compiler inside constructor as first statement......

Hope it helped you... have good day (S)