super keyword
super keyword is used to call parent class methods, variables in child class for ease of use, also can be used to invoke parent class constructor.
Only public and protected methods / variables can be accessed by using super keyword, can’t access private methods/variables of parent class.
NOTE : Using super keyword should be the first line in a method of child class, else will get compile error.
Watch here for more details
Code sample
Parent class – Bank.java
package superKeyword; public class Bank { Double Balance; String AccHolder; Bank(Double Bal, String AccName){ Balance = Bal; AccHolder = AccName; } void Disp() { System.out.println("Balance - " + Balance); System.out.println("Name - " + AccHolder); } }
Child class – HDFCBank.java
package superKeyword; public class HDFCBank extends Bank{ Double minBalance ; HDFCBank(Double Bal, String Name, Double minBal){ super(Bal, Name);//parent constructor minBalance = minBal; } void Disp() { /*super.AccHolder = "ABC"; super.Balance = 120.0;*/ super.Disp(); System.out.println("Min Balance - " + minBalance); } public static void main(String[] args) { HDFCBank hdfcBank = new HDFCBank(120.0, "ABC", 100.0); hdfcBank.Disp(); } }
Hope that helps!