If the 'subclass' has declared the same method that is already available in 'superclass' then the subclass uses 'super' keyword to access the method declared in the 'superclass'.
Example - super.methodname( );
Lets implement this on Eclipse IDE:
1. Create 'Superclass' superclass as shown below:
2. Create 'Subclass' subclass as shown below:
3. Create 'SuperMethodDemo' class to call the methods available and methods inherited by the 'Subclass' as shown below:
4. Save and Run the 'SuperMethodDemo' class
5. Observe that the output is displayed in the console as shown below:
Download this project:
Click here to download this project containing 'Superclass', 'Subclass' and 'SuperMethodDemo' class files used in this post (You can download this project and import into Eclipse IDE on your machine)
So now we have come to know that 'super' keyword can be used to access the instance variables and methods of the 'superclass' that have hidden by the instance variables and methods of the 'subclass'. We can also use 'super' keyword to call the Superclass constructors.
Example - super.methodname( );
Lets implement this on Eclipse IDE:
1. Create 'Superclass' superclass as shown below:
5. Observe that the output is displayed in the console as shown below:
Click here to download this project containing 'Superclass', 'Subclass' and 'SuperMethodDemo' class files used in this post (You can download this project and import into Eclipse IDE on your machine)
So now we have come to know that 'super' keyword can be used to access the instance variables and methods of the 'superclass' that have hidden by the instance variables and methods of the 'subclass'. We can also use 'super' keyword to call the Superclass constructors.