super:

  • The functionality of super keyword is only to point the immediate super class object of the current object.
  • super keyword is applicable only in the non static methods and super keyword not applicable in the static methods.
  • super keyword used to access the members of the super class object.
  • super.member;
  • It is used to store super class non static members memory reference through current sub class object for separating super class members from subclass members.

Uses:

  • Using super keyword we can explicitly point the immediate super class object and access all the super class members which are present with the same name of the subclass members from the sub class methods.

  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }


  1. package com.instanceofjavaforus;
  2. public Class Subdemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7.  
  8. super.a=10;
  9. super.b=20;
  10.  
  11.   super.show();
  12.  
  13. }
  14.  
  15. public static void main (String args[]) {
  16.  Subdemo obj= new Subdemo();
  17.  obj.a=1;
  18. obj.b=2;
  19. obj.disply();

  20.  
  21. }
  22. }
  23. Output: 
  24. 1
  25. 2
  26. 10
  27. 20

  • Using super keyword we can explicitly call the super class constructor.
  • The statement which explicitly calling super class constructor should be always the first statement of the sub class constructor.
  • The statement which explicitly calls the super class constructor should be present inside a constructor only.


  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10.  SuperDemo(int x, int y){
  11.  a=x;
  12. b=y
  13. }
  14. }


  1. package com.instanceofjavaforus;
  2. public Class Subdemo{ 
  3. int a,b;
  4.  
  5. SubDemo(int x, int y){
  6. super(10,20);
  7.  a=x;
  8. b=y
  9. }
  10.  
  11. void disply(){

  12. System.out.println(a);
  13. System.out.println(b);
  14.   super.show();
  15. }
  16.  
  17. public static void main (String args[]) {
  18.  Subdemo obj= new Subdemo(1,2);
  19.  
  20. obj.disply();

  21.  
  22. }
  23. }
  24. Output: 
  25. 1
  26. 2
  27. 10
  28. 20

 

Key points to remember:

  • It is a keyword used to store super class non static members reference in subclass object.
  • Pre defined instance variable used to hold super class object reference through sub class object.
  • Used to separate super class and sub class members if both have same name.
  • It must be used explicitly if super class and sub class members have the same name.
  • Used to call super class constructor from sub class constructor.


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Super keyword

Select Menu