Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

Super Keyword In Java

Super keyword in java is a reference variable that is used to refer parent class object. we can access variable, method and constructor of super class using super keyword..

super keyword can be used in following cases:-

To Access Super Class instance variable
To Access Super Class method
To Access Super Class constructor

final class Use of super with variables :

This scenario occurs when a subclass class and super class has same data members. In that case there is a possibility of ambiguity for instance variable between subclass class and super class.We can understand it more clearly using below code snippet:-
super-class-variable

In the above example, both base class and subclass have a member legs and color. We could access legs and color of base class in sublcass using super keyword.



final class Use of super with methods :

This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. This code snippet helps to understand the same.
super-class-method

Output :

In the above example, we have seen that if we only call method fly() then, the current class fly() is invoked but with the use of super keyword, fly() of superclass could also be invoked.



final class Use of super with constructors :

super keyword can also be used to access the parent class constructor. "super" keyword can call both parameterized as well as non parameterized constructors depending upon the business requirement. Following is the code snippet to explain the above.
super-class-constructor

Output :

legs = 6
this.legs = 6
super.legs = 2
color = yellow
this.color = yellow
super.color = red

In the above example we have called the superclass constructor using keyword "super" via subclass constructor.

jvm-architecture Some importants Points about super keyword :-
          Call to super() must be first statement inside subclass constructor.
      If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
      If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that a whole chain of constructors called, all the way back to the constructor of Object. This, in fact, is the case. It is called constructor chaining.