this and super in Java





this Keyword :

  • this keyword can be used to refer current class instance variable.

  • this( ) can be used to invoke current class constructor.

  • this always represents current object.

  • If we have local and instance variable with the same name in a given context then we use this keyword to remove this ambiguous situation.

    class Demo
    {
      int status = 10;
      void show()
       {
         int status = 20;
         System.out.print(status);
       }
    }
    predict the output :
     It will give 20
    
     In a given code we have two variable with name “status”, first one is instance type that is declared in a Demo class, and second one is local type that is declared in show method.
    
     If we access status inside show method it will always refer local variable.
    
     this keyword solves that problem.
    class Demo
    {
      int status = 10;
      void show()
       {
         int status = 20;
         //instance variable
         System.out.print(this.status);
         //local variable
         System.out.print(status);
       }
    }
  • this() can be used to invoked current class constructor.

  • Call to this() must be the first statement in constructor.

  • Call to this( ) allowed only once.

  • If we have multiple constructors in a class, then Call to this( ) in different constructors must be organised in such a way that it should not form a recursive invocation.

  • e.g.
    class Demo
    {
     Demo()
      {
      }
     Demo ( int a )
      {
        this ();
      }
    public static void main(String [] ar)
      {
        Demo ob1 = new Demo();
        Demo ob2 = new Demo(10);
      }
    }
    
     The object constructed for reference ob1, initialized  by the default constructor of Demo class.
    
     The object constructed for reference ob2, initialized by both default & parameterized(single int)  constructor of Demo class.

super Keyword :

  • The super keyword in java is a reference variable which is used to refer immediate parent class object.

  • super can be use :

  • to access parent class instance variable.

  • to invoke parent class method.

  • to invoke parent class constructor using super().

Example :

//Parent.java
public class Parent
{
  int x =10;
  public void show()
   {
    System.out.println("Parent show");
   }
}
---------------
//Child.java
public class Child extends parent
{
 int x = 20;
//overriding method
 public void show()
  {
   System.out.println("child show");
  }
 public void display()
  {
   int x = 30;
//local varaible
   System.out.println("x = "+x);

//current class variable
  System.out.println("x = "+this.x);

//super class variable
  System.out.println("x = "+super.x);

//current class show method
   show();

//calling parent show method
   super.show();
  }
}            

invoke parent class constructor.

//Parent.java
public class Parent
{
//default constructor
 Parent()
  {
   //definition
  }
//Parameterized constructor
 Parent(String name)
  {
    //definition
  }
}
---------------
//Child.java
public class Child extends Parent
{
  Child()
  {
//invoke parent parameterized constructor
    super("Java Prowess");
   //definition
  }
 Child()
  {
/* if you do not specify parent class constructor here, by-default compiler added default constructor of parent */
  }
}            
  • Note :
  • super() is added in each class constructor automatically by compiler if there is no super() or this().

  • if you don't have default constructor in parent class, then its mandatory that you have to specify explicit call of parent class parameterized constructor in every child's constructor.

  • this() and super(), must be very first statement in constructor, and can't use together.

  • this and super can be use only within non-static methods.

Next topic is final keyword




 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved