Super keyword in Java




Super Keyword is used in java to access variables, method or constructor of super class(Parent class) from a child class.

In case of variables and methods we need to used super keyword only when variable of Child Class is hiding the variable of Parent Class i.e. Child Class is having variable with exactly same name and type.
Or

Child Class is overriding a method of Parent Class.

If above is not the case then variables/methods of Parent Class can be access from Child class without using super keyword as they are being inherited by child class.

So super keyword is used:
  • To access Parent Class constructor
  • To access Parent Class variables
  • To access Parent Class Methods





Access Parent Class constructor:
  • To access Parent Class constructor we use super keyword
  • Call to Parent Class constructor with super keyword must be the first line inside Child Class constructor
  • To call Parent Class constructor we don't need to use Parent Class Constructer name. This is because of the fact that Every Child Class always knows the name of Parent Class and every Class's Constructor name is same as Class name
  • To class default constructor of Parent class we use 'super()'
  • To class parameterized constructor of Parent class we use 'super(list of parameters)'
  • If you don't call any constructor of Parent Class then compiler will automatically insert 'super()' inside the Child Class constructor to call the default constructer of Parent Class

package com.meritmix.blog.java;

public class SuperKeywordParent {
 protected int a =10;
 protected int b=40;
public SuperKeywordParent() {
System.out.println("Default Parent Class constructor");
}

public SuperKeywordParent(int i) {
 a=i;
 System.out.println("Parameterized Parent Class constructor");
}

}

class ChildClass extends SuperKeywordParent{
 protected int a =10;
 protected int b=30;
 public ChildClass() {
  super(); //optional as if not written, compiler will automatically inserts it
  System.out.println("Default Child Class constructor");
  //super(); //calling Parent class constructor from here not allowed
              //as its not first line inside constructor
 }
 public ChildClass(int i) {
  super(i);
  a=i;
  System.out.println("Parameterized Child Class constructor");
 }
 
 }

class TestSuper{
 public static void main(String[] args) {
  ChildClass childObj = new ChildClass();
  childObj.print();
 }
}



Access Parent Class variables:
  • To access Parent Class variables we use super keyword
  • Call to Parent Class variables with super keyword not necessarily be the first line inside Child Class code
  • To access variables of Parent class we use 'super.variableName'
  • If Child class is not having same variable then there is no need to use 'super' keyword to access variables of Parent class
package com.meritmix.blog.java;

public class SuperKeywordParent {
 protected int a =10;
 protected int b=40;
public SuperKeywordParent() {
System.out.println("Default Parent Class constructor");
}

public SuperKeywordParent(int i) {
 a=i;
 System.out.println("Parameterized Parent Class constructor");
}

public void print() {
 System.out.println("a :"+a+", b :"+b);
}
}

class ChildClass extends SuperKeywordParent{
 protected int a =10;
 protected int b=30;
 public ChildClass() {
  super(); //optional as if not written, compiler will automatically inserts it
  System.out.println("Default Child Class constructor");
  //super(); //calling Parent class constructor from here not allowed
              //as its not first line inside constructor
 }
 public ChildClass(int i) {
  super(i);
  a=i;
  System.out.println("Parameterized Child Class constructor");
 }
 
 public void print() {
  System.out.println("a :"+a+", b :"+b); // prints Child class variables
  System.out.println("Parent a :"+super.a+", Parent b :"+super.b); // prints Parent class variables
  
 }
}

class TestSuper{
 public static void main(String[] args) {
  ChildClass childObj = new ChildClass();
  childObj.print();
 }
}



Access Parent Class Methods:
  • To access Parent Class Methods we use super keyword
  • Call to Parent Class Methods with super keyword not necessarily be the first line inside Child Class code
  • To class variables of Parent class we use 'super.MethodName()'
  • If Child class is not overriding the method of Parent Class then there is no need to use 'super' keyword to access method of Parent class
package com.meritmix.blog.java;

public class SuperKeywordParent {
 protected int a =10;
 protected int b=40;
public SuperKeywordParent() {
System.out.println("Default Parent Class constructor");
}

public SuperKeywordParent(int i) {
 a=i;
 System.out.println("Parameterized Parent Class constructor");
}

public void print() {
 System.out.println("a :"+a+", b :"+b);
}
}

class ChildClass extends SuperKeywordParent{
 protected int a =10;
 protected int b=30;
 public ChildClass() {
  super(); //optional as if not written, compiler will automatically inserts it
  System.out.println("Default Child Class constructor");
  //super(); //calling Parent class constructor from here not allowed
              //as its not first line inside constructor
 }
 public ChildClass(int i) {
  super(i);
  a=i;
  System.out.println("Parameterized Child Class constructor");
 }
 
 public void print() {
  System.out.println("a :"+a+", b :"+b); // prints Child class variables
  System.out.println("Parent a :"+super.a+", Parent b :"+super.b); // prints Parent class variables
  super.print();// Call Parent class print method
 }
}

class TestSuper{
 public static void main(String[] args) {
  ChildClass childObj = new ChildClass();
  childObj.print();
 }
}



Output:

Default Parent Class constructor
Default Child Class constructor
a :10, b :30
Parent a :10, Parent b :40
a :10, b :40

No comments:

Post a Comment