0% found this document useful (0 votes)
456 views

Super Keyword Interview Questions Java

The super keyword in Java is used to refer to the immediate parent class. It allows subclasses to access methods and fields of the parent class. There are three main uses of the super keyword: 1. To access the parent class constructor from the child constructor using super(). This must be the first line in the child constructor. 2. To access overridden methods of the parent class from the child using super.method(). 3. To access fields and methods of the parent class that are hidden in the child class using super.field or super.method(). The super keyword can only be used in non-static contexts since it operates on the parent class instance that the current object extends. It cannot be

Uploaded by

samarnarendra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
456 views

Super Keyword Interview Questions Java

The super keyword in Java is used to refer to the immediate parent class. It allows subclasses to access methods and fields of the parent class. There are three main uses of the super keyword: 1. To access the parent class constructor from the child constructor using super(). This must be the first line in the child constructor. 2. To access overridden methods of the parent class from the child using super.method(). 3. To access fields and methods of the parent class that are hidden in the child class using super.field or super.method(). The super keyword can only be used in non-static contexts since it operates on the parent class instance that the current object extends. It cannot be

Uploaded by

samarnarendra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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

3. We can call super class constructor from class constructor:

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:

1. Super class constructor called


2. Sub class constructor called

key points:

Super(); call must be first statement inside constructor.


By default in every class constructor JVM adds super(); call in side the constructor as first
statement which calls default constructor of super class, if we are not not calling it.
If we want to call explicitly we can call at that time default call wont be there.

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. Sample class constructor called

5.What if there is a chain of extended classes and 'super' keyword is used


1. package com.superinterviewprograms;
2. public Class A{
3.
4. A(){
5. System.out.println("A class constructor called ");
6. }
7.
8.
9. }

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:

1. A class constructor called


2. B class constructor called
3. C class constructor called

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.

You might also like