• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Inheritance » Super Keyword in Java Example
Next →
← Prev

Super Keyword in Java Example

By Dinesh Thakur

The super() keyword is used to reference objects for the immediate parent class. A subclass inherits the accessible data fields and methods from its superclass, but the constructors of the superclass are not inherited in the subclass. They can only be invoked from constructors of the subclass( es) using the keyword super.

The keyword super refers to superclass of the class in which super appears. The syntax used to call superclass constructor is

super([arguments])

Here, the arguments enclosed in square brackets are optional. The statement super () invokes the constructor with no parameters of its superclass and the statement

super (arguments)

invokes the superclass constructor that matches the arguments. The superclass constructor call must be the first statement in the body of subclass constructor. If one does not specify super, the compiler implicitly inserts super (); at the beginning of the subclass’s default constructor to call the superclass’s default constructor.

When you create an instance of the subclass, it invokes the constructors of all the superclasses along the inheritance chain. A superclass constructor is always called before the superclass constructor.

Now let us consider a program to explain the use of super keyword.

class Point  //superclass 
 {  
       private int x,y,z; 
       Point(int xl,int yl,int zl) 
       { 
           System.out.println("Super class constructor is invoked"); 
           x = xl; 
           y = yl; 
           z = zl; 
       } 
       public int getX() {return x;} 
       public int getY() {return y;} 
       public int getZ() {return z;} 
      
 } 
    class Sphere extends Point //Subclass 
 { 
      private int radius; 
      Sphere(int xl,int yl,int zl,int r) 
      { 
         super(xl,yl,zl); //call point(int xl,int yl,int zl); 
         System.out.println("Derived class constructor is invoked"); 
         radius = r ; 
      } 
      public int getRadius() {return radius; } 
      public double volume() 
      { 
         return (4*Math.PI*Math.pow(radius,3)); 
      } 
 } 
    public class SuperKeyword   
 { 
          public static void main(String[] args) 
       { 
              Sphere s = new Sphere(2,3,4,5); //create Sphere object 
              System.out.println("x coordinate = " +s.getX()); 
              System.out.println("y coordinate = " +s.getY()); 
              System.out.println("z coordinate = " +s.getZ()); 
              System.out.println("Volume of Sphere is = " +s.volume()); 
      } 
 } 

Super Keyword in Java Example

We’ll be covering the following topics in this tutorial:

  • The super() keyword is used:
  • Accessing Parent Class Variables
  • Invoking the Parent Class Constructor
  • Using a Parametrized Super() Call
  • The Super() Keyword and Method Overriding

The super() keyword is used:

• For accessing the parent class data members when the parent and the child contain a member with identical names.
• For explicitly calling the parametrized and no-arg constructors for the parent class.
• For accessing the parent class method when it has been overridden by the child class.

Now let’s see some examples to explain these:

Accessing Parent Class Variables

When you have a child class that contains a variable that is also in the parent class,  when you want access to the parent class variable the super() keyword is needed.   In the next code example, we have declared a data member called num in our parent class. We cannot access the variable for num in our parent class without the  super() keyword:

//Parent class, Superclass or base class

class Superclass {
   int num =150;
}
//Child class, subclass or derived class
class Subclass extends Superclass {
        /* The variable called num which is in the superclass
         * is also present in the subclass */
int num =160;
     void printNumber(){
           System.out.println(num);
     }
     public static void main(String args[]){
Subclass obj=new Subclass();
obj.printNumber();
     }
}

The output of this will be: 160
To access the num variable in the parent class, we can call it in the following way, provided the parent, and the child class contains the same variable:
super.variable_name
Another example: this we have a print statement where, rather than num, we pass super. num instead:
class Superclass {
int num =150;
}
class Subclass extends Superclass {
      int num =160;
void printNumber(){
       /* Rather than using num, we are using
* super.num in our print statement

* referencing the num variable of Superclass */

System.out.println(super.num);
}
   public static void main(String args[]) {
Subclass obj=new Subclass();
obj.printNumber();
}
}
Output: 150

 By using super.num instead of num, we can gain access to the parent class num variable.

Invoking the Parent Class Constructor

The object for the subclass is created, we use the new keyword for invoking the child class constructor. In turn, this implicitly invokes the parent class constructor.  The execution order when the child class object is created is a constructor for the parent class gets executed first, followed by the constructor for the child class. The reason for this order is that the compile inserts super() to invoke the parent class no-arg constructor as the initial statement in the child class constructor.
Let’s see this by way of an example:

class Parentclass {
Parentclass(){
        System.out.println(“Constructor of parent class”);
}
}
class Subclass extends Parentclass {
Subclass(){
        /* Compile will implicitly add super() at this point as the
* initial statement for this constructor. */

System.out.println(“Constructor of child class”);
    }
Subclass(int num)  {
      /* Although it is a parameterized constructor.
* The compiler will still insert the no-arg super() at this point */
       System.out.println(“arg constructor of child class”);
}
    void display()  {
        System.out.println(“Hello!”);
}
   public static void main(String args[])  {
/* The default constructor is used for creating the object and this
* invokes the child class constructor, which, in turn

* invokes the constructor for the parent class */

Subclass obj = new Subclass();
        //We are now calling the subclass method
obj.display();
         /* We use the arg constructor to create another object
* which invokes the child class constructor which, in turn

* will invoke the parent class no-arg constructor automatically */

Subclass obj2 = new Subclass(10);
obj2.display();
    }
}
The output for this will be:
Constructor of parent class
Constructor of child class
Hello!
Constructor of parent class
arg constructor of child class
Hello!

Using a Parametrized Super() Call

For the child class constructor, we can explicitly call super(), but it makes no sense to do so because it would only be redundant. However, for a parent class constructor that takes parameters, we would use a parametrized super() call instead, as a way of invoking the parent class’s parametrized constructor from the child class’s constructor.  

An example will make this clearer:

class Parentclass {
    //no-arg constructor
Parentclass()  {
System.out.println(“no-arg constructor of parent class”);
    }
    //arg or parameterized constructor
Parentclass(String str)  {
      System.out.println(“parameterized constructor of parent class”);
}
}
   class Subclass extends Parentclass {
Subclass()  {
/* super() must be added to the initial statement of the constructor
* otherwise you get a compilation error. Another important

* point is that when super is explicitly used in the constructor,

* the compiler will not invoke the parent constructor automatically. */

          super(“Hahaha”);
          System.out.println(“Constructor of child class”);
       }
void display(){
System.out.println(“Hello”);
}
      public static void main(String args[])  {
Subclass obj=new Subclass();
obj.display();
}
}
The output of this will be:
parameterized constructor of parent class
Constructor of child class
Hello

This particular example throws up two critical points:
You must have either super() or a parametrized super() as the initial constructor statement otherwise the compilation error we talked of earlier will be thrown.  When super() was placed explicitly in the constructor, note that the no-arg constructor for the parent class was not called by the compiler.

The Super() Keyword and Method Overriding

When a method that is already in the parent class is declared by the child class, it is called overriding. We will talk more about this later; for now, keep in mind that when child classes override parent class methods when a method is called form the child class object, it will be the child version of the method. However, when you  use super() in this way – super.method_name, the parent class method, the one that has been overridden, may be called. Have a look at an example:

class Parentclass {

        //The method that has been overridden

     void display(){

        System.out.println(“Parent class method”);

     }

}

class Subclass extends Parentclass {

       //The method doing the overriding

       void display(){

           System.out.println(“Child class method”);

       }

void printMsg()  {

         //This calls the Overriding method

     display();

        //This calls the Overridden method

     super.display();

}

   public static void main(String args[])  {

       Subclass obj=new Subclass();

       obj.printMsg();

   }

}

The output of this would be:

Child class method

Parent class method

You’ll also like:

  1. Example of Super Method in Java
  2. Java this keyword with example
  3. Final Keyword in java
  4. Static keyword in Java (With Examples)
  5. What is Java keyword (reserved words)? – Definition
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW