super keyword

  • The super is java keyword. As the name suggest super is used to access the members of the super class.It is used for two purposes in java.

  • The first use of keyword super is to access the hidden data variables of the super class hidden by the sub class.

  • e.g. Suppose class A is the super class that has two instance variables as int a and float b. class B is the subclass that also contains its own data members named a and b. then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command super.member;

  • Here member can either be an instance variable or a method. This form of super most useful to handle situations where the local members of a subclass hides the members of a super class having the same name. The following example clarify all the confusions.

Example Supervar.java
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
class A {
 
    int a;
    float b;
 
    void Show() {
        System.out.println("b in super class: " + b);
    }
}
 
public class Supervar extends A {
 
    int a;
    float b;
 
    Supervar(int p, float q) {
        a = p;
        super.b = q;
    }
 
    void Show() {
        super.Show();
        System.out.println("b in super class: " + super.b);
        System.out.println("a in sub class: " + a);
    }
 
    public static void main(String[] args) {
        Supervar subobj = new Supervar(1, 5);
        subobj.Show();
    }
}

 

output:

b in super class: 5.0

b in super class: 5.0

a in sub class: 1

Use of super to call super class constructor:

  • The second use of the keyword super in java is to call super class constructor in the subclass. This functionality can be achieved just by using the following command super(param-list);

  • Here parameter list is the list of the parameter requires by the constructor in the super class.

  • super must be the first statement executed inside a super class constructor.

  • If we want to call the default constructor then we pass the empty parameter list.

  • The following program illustrates the use of the super keyword to call a super class constructor.

Example Supercons.java
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
class A {
 
    int a;
    int b;
    int c;
 
    A(int p, int q, int r) {
        a = p;
        b = q;
        c = r;
    }
}
 
public class Supercons extends A {
 
    int d;
 
    Supercons(int l, int m, int n, int o) {
        super(l, m, n);
        d = o;
    }
 
    void Show() {
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);
    }
 
    public static void main(String args[]) {
        Supercons b = new Supercons(4, 3, 8, 7);
        b.Show();
    }
}

 

output:

a = 4

b = 3

c = 8

d = 7


Last modified: Monday, 26 December 2011, 04:39 AM

Skip Navigation

Navigation

Skip OpenGurukulSkip Settings

Settings