Super Keyword in java is used to refer to object of the immediate superclass.Super keyword is used in the context of inheritance.
Super keyword in java can be used at three level:
Super keyword in java can be used at three level:
- Variable level
- Constructor level
- Method level
Usage of super at variable level:
We can refer to variable of super class using:
1
2
3
|
super.variableName
|
Let’s see this with the help of example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package org.arpit.java2blog;
public class Person
{
String name;
public Person()
{
System.out.println("Calling Person constructor");
name="Default";
}
}
class Employee extends Person{
String name;
int age;
public Employee()
{
System.out.println("Calling Employee class constructor");
this.name="Martin";
}
public void workOnAssignment()
{
// Working on assignment
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void printName()
{
System.out.println("Printing default name from person class : "+super.name);
}
public static void main(String args[])
{
Employee e1=new Employee();
e1.printName();
}
}
|
When you run above program, you will get below output:
Calling Person constructorCalling Employee class constructorPrinting default name from person class : Default
As you can see, we have used super.name to print name from parent class in Employee class.
Usage of super at Constructor level:
super keyword can be used to call the constructor of parent class.Please note that super should be first statement in child class constructor.
Let’s see with the help of example:
Usage of super at Constructor level:
super keyword can be used to call the constructor of parent class.Please note that super should be first statement in child class constructor.
Let’s see with the help of example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package org.arpit.java2blog;
public class Person
{
String name;
public Person(String name)
{
this.name=name;
System.out.println("Calling Person Parameterized constructor");
}
}
class Employee extends Person{
int age;
public Employee(String name)
{
super(name);
System.out.println("Calling Employee class constructor");
}
public void workOnAssignment()
{
// Working on assignment
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String args[])
{
Employee e1=new Employee("John");
System.out.println("Employee's name:"+e1.getName());
}
}
|
When you run above program, you will get below output:
Calling Person Parameterized constructorCalling Employee class constructorEmployee’s name:John
As you can see we have used super keyword to call Person contructor from employee class.
Usage of super keyword at method level:
Super keyword can be used to call method of Parent class. It can be used to specifically call method of parent class in case of method overriding.
Let’s see with the help of example:
Usage of super keyword at method level:
Super keyword can be used to call method of Parent class. It can be used to specifically call method of parent class in case of method overriding.
Let’s see with the help of example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package org.arpit.java2blog;
public class Person
{
String name;
public Person()
{
System.out.println("Calling Person constructor");
name="Default";
}
public void printName()
{
System.out.println("Printing default name from person class : "+this.name);
}
}
class Employee extends Person{
String name;
int age;
public Employee()
{
System.out.println("Calling Employee class constructor");
this.name="Martin";
}
public void workOnAssignment()
{
// Working on assignment
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
{
super.printName();
System.out.println("Printing name from Employee class : "+this.name);
}
public static void main(String args[])
{
Employee e1=new Employee();
e1.printName();
}
}
|
When you run above program, you will get below output:
Calling Person constructorCalling Employee class constructorPrinting default name from person class : DefaultPrinting name from Employee class : Martin
As you can see, we have called Person class method from Employee class using super keyword.That’s all about super keyword in java.
0 Comments
If you have any doubts,please let me know