Skip to main content

super keyword in java

What is the use of Super keyword

  • A super keyword used to refer super class property from sub class
  • It is used in non static context only

How to refer Super class Property from sub class ?

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
package keyword;
 
class demo1
 {
   int k=10;
 } 
 
//creating sub class
class Sample extends demo1
  {
    int k=12;
    void disp()
     {
       System.out.println(this.k);//referring current class property
       System.out.println(super.k);//referring super class property from subclass
     }
}
 
public class SuperKeyword
  {
public static void main(String[] args)
  {
    System.out.println("********************");
    Sample obj1=new Sample();
    obj1.disp();
    System.out.println("*********************");
  }
}
OUTPUT:
********************
12
10
*********************