Uses of Super keyword in java

Super keyword

Working with super keyword:-

Super keyword in java which can be used to solve 3 different problems. Super always refers to parent class object. superkeyword in Java is a reference variable that is used to refer parent class objects

Super keyword only can be placed within a non-static method or nonstatic block or constructor. Whenever you create the instance of the subclass, an instance of the parent class is created implicitly which is referred by super reference variable.

Uses of the super keyword in java

Use-1

When the local variable and instance variable name is the same then “this” keyword is used to the difference between them.

  • If the child class and superclass variable are the same then “super” is used to create a difference between them.

Example

class Test

{

    int x=10;

}

class Demo extends Test

{

    int x=20;

    void show()

    {

         int x=30;

         System.out.println(x);

         System.out.println(this.x);

         System.out.println(super.x);

    }

   public static void main(String[] args)

   {

          new Demo().show();

   }

}

Output:-

30

20

10

Use-2

In the case of method overriding super is used to create a difference between the child class and the parent class method.

Example:-

class Demo

{

  void display()

  {

     System.out.println("display in Demo.class");

  }

}

class Test extends Demo

{

   void display()

   {

       System.out.println("display in Test.class");

   }

   void show()

   {

        display();

        super.display();

        this.display();

   }

   public static void main(String[] args)

   {

       new Test().show();

  }

}

Output

display in Test.class

display in Demo.class

display in Test.class

Use-3

Super is used to call parent class constructor from the constructor of the subclass. The super statement should be a very first statement within the constructor.

Example:-

class Demo

{

    Demo()

    {
System.out.println(" i am in demo ctor");

            }

}

class Test extends Demo

{

            Test()

            {

                        this("hello");

                        System.out.println(" i am in Test default ctor");

            }

            Test(int x)

            {

                       

                        System.out.println(" i am in Test intiger ctor");

            }

            Test(String x)

            {

                        this(182);

                        System.out.println(" i am in Test String ctor");

            }

            public static void main(String[] args)

            {

                        new Test();

            }

}

Output

i am in demo ctor

 i am in Test intiger ctor

 i am in Test String ctor

 i am in Test default ctor

This Post Has 4 Comments

  1. furtdsolinopv

    Some genuinely nice stuff on this internet site, I like it.

    1. basicittopic

      thanks for giving the feedback

  2. survivalist

    I have been examinating out many of your articles and it’s nice stuff. I will make sure to bookmark your site.

    1. basicittopic

      Thanks for giving the feedback

Leave a Reply