Meaning of Super Keyword

56,589

Solution 1

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

Solution 2

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).

Solution 3

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

Solution 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.

Solution 5

  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)

Share:
56,589
Tushar
Author by

Tushar

Updated on August 22, 2020

Comments

  • Tushar
    Tushar almost 4 years

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

  • Samir Mangroliya
    Samir Mangroliya over 12 years
    Felix why we call super() in Parent class constructor?
  • Ayaz Alifov
    Ayaz Alifov over 9 years
    It needs to be marked as the best simplest answer for beginners.