super keyword in Java

 super keyword in Java is used to refer to parent class objects. It is basically introduced with the concept of inheritance. It can be used as:

  • accessing methods and variable of parent class
  • accessing and initializing parent class constructor
1. Accessing variable of parent class:
        In case of ambiguity that is when the parent class and child class objects have instance variables with same name, in that case JVM will provide the value of the variable of current class.
But what if we need to use parent class instance variable, in that case use of super keyword comes in picture.
Let's understand it with an example:
    
class Application{
public String name = "MyApplication";
public int sizeInMB = 373;
public void run() {
System.out.println("Application starts running...");
}
}

public class Program extends Application{
public String name = "MyProgram";
public String getApplicationName() {
return super.name; //accessing parent class varible using super keyword
}
public static void main(String[] args) {
Program p = new Program();
System.out.println("Program instance variable name value: " + p.name);
System.out.println("Parent class instace variable name value: " +         p.getApplicationName());

}

}

Output:    Program instance variable name value: MyProgram
                Parent class instace variable name value: MyApplication

2. Accessing methods of Parent class:
    You can use super keyword to access methods of the parent class as:
package com.infy.practice;

class Application{
public String name = "MyApplication";
public int sizeInMB = 373;
public void run() {
System.out.println("Application starts running...");
}
}

public class Program extends Application{
public String name = "MyProgram";

public void run() {
super.run(); //calling parent class run() method
System.out.println("Program starts running...");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Program p = new Program();
p.run();
}
}

Output:     Application starts running...
                 Program starts running...

3. Initializing parent class constructor:
        Using super keyword we can call both parameterized or parameter less constructors depending upon the need.
Note: super() call must be the first statement in child class constructor, otherwise program will throw a compile time error.

class Application{
public String name = "MyApplication";
public int sizeInMB = 373;
public Application() {
System.out.println("Constructor initialized for Application class...");
}
public void run() {
System.out.println("Application starts running...");
}
}

public class Program extends Application{
public String name = "MyProgram";
public Program() {
super(); //for parameterized constructor call as super(param1, param2, ...)
System.out.println("Program class constructor...");
//super(): calling super after some statements will throw an error
}
public String getApplicationName() {
return super.name;
}
public void run() {
super.run();
System.out.println("Program starts running...");
}
public static void main(String[] args) {
Program p = new Program();
}
}

Output:    Constructor initialized for Application class...
                Program class constructor...

Continue Learning: static keyword 





Comments

Popular Posts