Pages

Sunday, February 5, 2017

super keyword in Java


  • super is a keyword which is used to refer the members of immediate super class.
  • super cannot be accessed from static context.
  • super keyword can be used in 3 ways:
    1. To access the immediate super class variables
      Syntax:
                  super.<variableName>
    2. To access the immediate super class methods
      Syntax:
                  super.();
    3. To access the immediate super class constructors
      Syntax:
                  super(params);

Example 1

class Lab2
{
    public static void main(String[] args) 
    {
        Hello h = new Hello();
        h.show();
    }
}

class Hai
{
    int a = 10;
}

class Hello extends Hai
{
    int a = 20;

    void show(){
        int a = 30;
        System.out.println(a); 
        System.out.println(this.a);
        System.out.println(super.a);
    }
}

Example 2

class Lab4
{
    public static void main(String[] args) 
    {
        Hello h = new Hello();
        h.show();
    }
}

class Hai{
    static int a=10;
}

class Hello extends Hai
{
    static int a = 20;

    void show(){
        int a = 30;
        System.out.println(a); 
        System.out.println(this.a); //Hello.a by the compiler
        System.out.println(super.a);//Hai.a by the compiler
    }
}

/*
Because it is static, compiler replaces by the class name

output
30
20
10
*/

Example 3

this and super keywords cannot be accessed from static context
class Lab7
{
    public static void main(String[] args) 
    {
        Hello hello = new Hello();
        System.out.println(hello.a);
    }
}

class Hai
{
    int a;
}

class Hello extends Hai
{
    String a;
}

/*
output
null
*/

Example 4

class Lab8{
    public static void main(String args[]){
            new C().show();
    }
}

class A
{
    int a = 10;
}

class B extends A
{
    String a = "SRJ";
}

class C extends B
{
    boolean a = true;
    void show(){
        float a = 99.99F;
        System.out.println(a);
        System.out.println(this.a);
        System.out.println(super.a);
        System.out.println(super.super.a);
    }
}

/*
Compile Time Error

Lab8.java:22: error: <identifier> expected
                System.out.println(super.super.a);
                                         ^
Lab8.java:22: error: not a statement
                System.out.println(super.super.a);
                                              ^
Lab8.java:22: error: ';' expected
                System.out.println(super.super.a);
                                                ^
3 errors
*/

Example 5

class Lab9{
    public static void main(String args[]){
            new C().show();
    }
}

class A
{
    int a = 10;
    int getAData(){
        return this.a;
    }
}

class B extends A
{
    String a = "SRJ";
}

class C extends B
{
    boolean a = true;
    void show(){
        float a = 99.99F;
        System.out.println(a);
        System.out.println(this.a);
        System.out.println(super.a);
        System.out.println(getAData());
    }
}

/*
output:
99.99
true
SRJ
10
*/

Example 6

super keyword can be used to ONLY to reference parent class variable, method or invoke parent constructor
class Lab10 
{
    public static void main(String[] args) 
    {
        new Hello.show();
    }
}

class Hai{}

class Hello extends Hai
{
    void show(){
        System.out.println(this);
        System.out.println(super);
    }
}

/*
Lab10.java:15: error: '.' expected
                System.out.println(super);
                                        ^
Lab10.java:15: error: ')' expected
                System.out.println(super);
                                         ^
Lab10.java:15: error: ';' expected
                System.out.println(super);
                                          ^
Lab10.java:17: error: reached end of file while parsing
}
 ^
4 errors
*/

Example 7

super cannot be assigned to any reference variable. It can be used only along with parent class variable,method and constructors
class Lab11
{
    public static void main(String[] args) 
    {
        new Hello.show();
    }
}

class Hai{}

class Hello extends Hai
{
    void show(){
        Hai h1 = this;
        Hello h2 = super;
    }
}

/*
Lab11.java:15: error: '.' expected
                Hello h2 = super;
                                ^
Lab11.java:15: error: ';' expected
                Hello h2 = super;
                                 ^
Lab11.java:17: error: reached end of file while parsing
}
 ^
3 errors
*/

Example 8

Default constructor is not created in the parent class. The super() inserted by the compiler in the sub-class constructor tries calling parent class constructor
class Lab15{
    public static void main(String args[]){
        new B();
    }
}

class A{
    A(int a){
        System.out.println("A->(int) cons");
    }
}

class B extends A{
    B(){
        super();
        System.out.println("B->D.C");
    }
}

/*
Lab15.java:15: error: constructor A in class A cannot be applied to given types;
                super();
                ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error
*/

Example 9

super() has to be the first statement inside a constructor. If programmer doesn't provide super() or this() inside constructor
then compiler will automatically add super() in the first line of a constructor
class Lab18{
    public static void main(String args[]){
        new B();
    }
}

class A{
    A(int a){
        System.out.println("A->(int) cons");
    }
}

class B extends A{
    B(){
        System.out.println("B->D.C");
        super(10);
    }
}

/*
Lab18.java:14: error: constructor A in class A cannot be applied to given types;
        B(){
           ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length
Lab18.java:16: error: call to super must be first statement in constructor
                super(10);
                     ^
2 errors
*/

Example 10

class Lab23{
    public static void main(String  args[]){
        new B();
    }
}

class A{
    A(Object obj){
        System.out.println("A(Object) Cons");
    }
}

class B{
    B(){
        super(this);
        System.out.println("B() D.C");
    }
}

/*
Lab23.java:15: error: cannot reference this before supertype constructor has been called
                super(this);
                      ^
Lab23.java:15: error: constructor Object in class Object cannot be applied to given types;
                super(this);
                ^
  required: no arguments
  found: B
  reason: actual and formal argument lists differ in length
2 errors
*/

Example 11

class Lab22{
    public static void main(String  args[]){
        B obj = new B("SRJ", 10);
        obj.show();
    }
}

class A
{
    int a;

    A(int a){
        this.a = a;
        System.out.println("A(int)");
    }
}

class B extends A
{
    String a;

    B(String a1, int a2){
        super(a2);
        this.a = a1;
        System.out.println("B(String, int)");
    }

    void show(){
        System.out.println(this.a);
        System.out.println(super.a);
    }
}

/*
output:
A(int)
B(String, int)
SRJ
10
*/

No comments:

Post a Comment