Super Keyword Interview Questions Java
Super Keyword Interview Questions Java
Super Keyword:
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.
We can call super class constructor in sub class using super() call.
We can access super class methods and variables in sub class using super.variable_name,
super.method();
Uses of super :
1. By using super keyword we can access super class variables in sub class.
Using super keyword we can access super class variables from sub class.
super.variable_name.
1. package com.superkeywordinjava;
2. public Class SuperDemo{
3.
4. int a,b;
5.
6. }
1. package com.superkeywordinjava;
2. public Class Subdemo extends SuperDemo{
3. int a,b;
4. void disply(){
5.
6. System.out.println(a);
7. System.out.println(b);
8. super.a=10;
9. super.b=20;
10.
11. System.out.println(super.a);
12. System.out.println(super.b);
13.
14. }
15.
16. public static void main (String args[]) {
17. Subdemo obj= new Subdemo();
18.
19. obj.a=1;
20. obj.b=2;
21.
22. obj.disply();
23.
24.
25.
26. }
27. }
Output:
1. 1
2. 2
3. 10
4. 20
2. By using super keyword we can access super class methods in sub class.
Using super keyword we can call super class methods from sub class.
super.method();.
1. package com.instanceofjavaforus;
2. public Class SuperDemo{
3. int a,b;
4.
5. public void show() {
6.
7. System.out.println(a);
8. System.out.println(b);
9.
10. }
11. }
1. package com.instanceofjavaforus;
2. public Class Subdemo extends SuperDemo{
3. int a,b;
4. void disply(){
5.
6. System.out.println(a);
7. System.out.println(b);
8.
9. super.a=10;
10. super.b=20;
11.
12. super.show();
13.
14. }
15.
16. public static void main (String args[]) {
17.
18. Subdemo obj= new Subdemo();
19.
20. obj.a=1;
21. obj.b=2;
22.
23. obj.disply();
24.
25.
26. }
27. }
Output:
1. 1
2. 2
3. 10
4. 20
By using super keyword we can able to call super class constructor from sub class
constructor.
Using super();
For the super(); call must be first statement in sub class constructor.
1. package com.superinterviewprograms;
2. public Class SuperDemo{
3.
4. int a,b;
5.
6. SuperDemo(int x, int y){
7. a=x;
8. b=y
9. System.out.println("Super class constructor called ");
10. }
11.
12.
13. }
1.
package com.superinterviewprograms;
2.
3. public Class Subdemo extends SuperDemo{
4.
5. int a,b;
6.
7. SubDemo(int x, int y){
8. super(10,20);
9. a=x;
10. b=y
11. System.out.println("Sub class constructor called ");
12. }
13.
14. public static void main (String args[]) {
15. Subdemo obj= new Subdemo(1,2);
16.
17.
18. }
19. }
Output:
key points:
4.What will happen if we are calling super() in constructor but our class does not
extending any class?
if our class not extending any class Yes still we can use super(); call in our class
Because in java every class will extend Object class by default this will be added by JVM.
But make sure we are using only super(); default call we can not place parameterized super
call because Object class does not have any parameterized constructor.
1. package com.superinterviewprograms;
2.
3. public Class Sample{
4.
5. Sample(){
6. super();
7. System.out.println("Sample class constructor called ");
8.
9. }
10.
11. public static void main (String args[]) {
12.
13. Sample obj= new Sample();
14.
15. }
16. }
Output:
1. package com.superinterviewprograms;
2. public Class B extends A{
3.
4. B(){
5.
6. System.out.println("B class constructor called ");
7.
8. }
9.
10.
11. }
1. package com.superinterviewprograms;
2. public Class C extends B{
3.
4. C(){
5. System.out.println("C class constructor called ");
6. }
7.
8.
9. public static void main (String args[]) {
10. C obj= new C();
11.
12.
13. }
14.
15. }
Output:
6. Can we call super class methods from static methods of sub class?
No we can not use super in static methods of sub class Because super belongs to object
level so we can not use super in static methods.
If we try to use in sub class static methods compile time error will come.