super keyword in java

Posted on

Tremendous key phrase :

The tremendous key phrase in java is utilized in 3 ways :- 

  • tremendous can be utilized to refer quick mother or father class occasion variable.
  • tremendous can be utilized to invoke quick mother or father class technique.
  • tremendous() can be utilized to invoke quick mother or father class constructor.
  • tremendous can be utilized to refer quick mother or father class occasion variable.

E.g.

class SuperClass

{

          int num=10;

}

class SubClass extends SuperClass

{   

          int num=20;

          void show()

         {

                System.out.println(num);

               System.out.println(tremendous.num);

         }

public static void most important (String [] args)

     {

     SubClass s= new SubClass();

     s.show();

      }

}

—————————————————————————————————————————————-

Output : 20

                  10

————————————————————————————————————————————— 

Within the above instance, SuperClass and SubClass each lessons have a typical variable num. If we print variable with out tremendous key phrase, it would print the variable num of present class by default. To entry the mother or father num variable, we have to use tremendous key phrase.

 

tremendous can be utilized to invoke quick mother or father class technique

 E.g.

class SuperClass

{

            void run()

            {

            System.out.println(“mother or father run technique”);

            }

}

class SubClass extends SuperClass

{

   void run()

            {

               System.out.println(“little one run technique”);

             }

   void show()

           {

               run();

               tremendous.run();

           }

public static void most important (String [] args)

    {

     SubClass s = new SubClass();

     s.show();

    }

}

————————————————————————————————————————————— 

Output : little one run technique

                  mother or father run technique

—————————————————————————————————————————————-

Within the above instance, SuperClass and SubClass each lessons have a way with the identical technique signature(identify) run(). If we print invoke technique with out tremendous key phrase, it would invoke the run() technique of present class(SubClass) by default. To entry the mother or father class i.e. SuperClass run() technique, we have to use tremendous key phrase.

 

tremendous() can be utilized to invoke quick mother or father class constructor

E.g :

 

class SuperClass

{

            SuperClass()

            {

            System.out.println(“mother or father class contructor”);

            }

}

class SubClass extends SuperClass

{

   SubClass()

           {

               Tremendous();

               System.out.println(“little one little one class constructor”);  

             }

  public static void most important (String [] args)

     {

            SubClass s = new SubClass();

    }

}

—————————————————————————————————————————————-

Output : mother or father class constructor

                 little one class constructor

 —————————————————————————————————————————————

 Within the above instance the ParentcClass constructor is invoked within the SubClass constructor utilizing tremendous().If we don’t write a constructor, a default constructor is offered by the JVM and that default constructor additionally accommodates tremendous() as its first assertion.