One of the 'super' keywords use is, the 'subclass' can use the 'super' keyword to access the instance variables and methods of the 'superclass', if both 'subclass' and 'superclass' has created the instance variables or methods with the same names.
Using 'super' keyword to access the instance variables of 'superclass':
If the 'subclass' has declared the same instance variable that is already available in 'superclass' then the 'subclass' uses 'super' keyword to access the instance variable declared in the 'superclass'.
In superclass class you have declared -> int a = 5;
In subclass class you have declared -> int a = 10;
So you now have the same variable 'a' declared in both superclass and subclass. Now if you print the value of variable 'a' in 'subclass', then the value of 'a' assigned in the subclass will get printed though 'subclass' inherits the instance variables created in 'superclass' as shown below:
System.out.println("The value of variable 'a' declared and printed in subclass is " + a);
But if you want to print the value of variable 'a' inherited from the 'superclass', then you have to specify the 'super' keyword before the instance variable 'a' as shown below:
System.out.println("The value of variable 'a' declared in 'superclass' and inherited & printed in subclass is "+ super.a);
Lets implement this on Eclipse IDE:
1. Create 'Superclass' superclass as shown below and save:
2. Create 'Subclass' subclass as shown below and save:
3. Create 'SuperVariableDemo' to access the members of 'Subclass' subclass as shown below:
4. Save and Run the 'SuperVariableDemo' class
5. Observe that the output is displayed in the console as shown below:
Download this project:
Click here to download the project containing 'Superclass', 'Subclass' and 'SuperVaraibleDemo' class files used in this post (You can download this project and import into Eclipse IDE on your machine)
Using 'super' keyword to access the instance variables of 'superclass':
If the 'subclass' has declared the same instance variable that is already available in 'superclass' then the 'subclass' uses 'super' keyword to access the instance variable declared in the 'superclass'.
In superclass class you have declared -> int a = 5;
In subclass class you have declared -> int a = 10;
So you now have the same variable 'a' declared in both superclass and subclass. Now if you print the value of variable 'a' in 'subclass', then the value of 'a' assigned in the subclass will get printed though 'subclass' inherits the instance variables created in 'superclass' as shown below:
System.out.println("The value of variable 'a' declared and printed in subclass is " + a);
But if you want to print the value of variable 'a' inherited from the 'superclass', then you have to specify the 'super' keyword before the instance variable 'a' as shown below:
System.out.println("The value of variable 'a' declared in 'superclass' and inherited & printed in subclass is "+ super.a);
Lets implement this on Eclipse IDE:
1. Create 'Superclass' superclass as shown below and save:
3. Create 'SuperVariableDemo' to access the members of 'Subclass' subclass as shown below:
4. Save and Run the 'SuperVariableDemo' class
5. Observe that the output is displayed in the console as shown below:
Click here to download the project containing 'Superclass', 'Subclass' and 'SuperVaraibleDemo' class files used in this post (You can download this project and import into Eclipse IDE on your machine)