Thursday, June 22, 2017

Java-this,super keywords,this(),super()

this
super
this()
super()



this
----
this is keyword used to refer to current class instance varaibles and methods.

this cannot used in static context.


syntax
------
How to access variable
----------------------
this.varname

ex:
this.name

How to access methods
---------------------
this.methodname();

ex:
this.show();


super keyword
-------------
A super keyword is used to refer to instance variables and instance methods of super class from a sub class.

super cannot be used in static context.


How to access variable
----------------------
super.varname

ex:
super.a

How access methods
------------------
super.methodname();

ex:super.display();





Program to demonstrate super keyword
------------------------------------
class A
{
int a=10;
public void display()
{
System.out.println(a);
}
}
class B extends A
{
int a=20;
public void display()
{
  System.out.println(a);
  System.out.println(super.a);
  super.display();
 
}
public static void main(String args[])
{
   B b=new B();
   b.display();

}
}


Note:
It is recommended to use super keyword if both the super class memebers and sub class members are same.


what is the output of the follwoing program?
Program
-------
class A
{
 int a=10;
}
class B extends A
{
 int a=20;
}
class C extends B
{
 int a=30;
 public void show()
 {
   System.out.println(a);
   System.out.println(super.a);
   System.out.println(((A)this).a);//line1
 }
 public static void main(String args[])
 {
    C c=new C();
    c.show();
 }
}
output
------
30
20
10


In the above code we are trying acces the instance variable of "A" class from "C" class,super keyword is applicable upto one level of inheritance.so to invoke the variable from A class to C class we had done casting as shown at line1.


this()
------
It is used to invoke one constructor from another constructor of same class.

Chaining of constructor within the same class is possible using this().


this() is of 2 types
default-->this();
parameterized-->this(value1,value2,...);
                   or
                this(var1,var2,...);


this() must be the first statement in a constructor.

//case1:Chaining parameterized from default
class Test
{
   Test()
   {
     System.out.println("1");
   }
   Test(int x,int y)
   {
     this();    
     System.out.println("2");
   }
}
class ThisTest
{
  public static void main(String args[])
  {
     Test t1=new Test(10,20);
 
  }
}

//case2:Chaining  parameterized from another //constructor
class Test
{
   Test()
   {
     this(30,40);
     System.out.println("1");
   }
   Test(int x,int y)
   {
         
     System.out.println("2");
   }
}
class ThisTest
{
  public static void main(String args[])
  {
     Test t1=new Test();
 
  }
}


what is the output of the following code?
class A
{
   A()
   {
      this(10);
      this(10,20);//line1
   }
   A(int x)
   {
   
   }
   A(int x,int y)
   {
   }
   public static void main(String args[])
   {

   }
}
//i)compiles
//ii)executes
//iii)cE[tick]
//iv)RE

In the above code this() method is declared as a second statement @line1.As per rule this() must be first statement in a constructor.


class A
{
   A()
   {
      this();
      System.out.println("A");
   }
   public static void main(String args[])
   {
       A a=new A();
     
   }
}
options
-------
i)CE
ii)RE
iii)Exceutes sucessfully and prints A
iv)None of the above.

Ans:
CE

Analysis
--------
The above program leads to CE saying "recursive constructor invocation" because we are calling same constructor repeatitively by declaring default this() inside default constrcutor.



Construtor calling in inheritance
---------------------------------
In java contructor is not inherited from super class to subclass because a construtor purpose is to initialize instance variables of a class in whihc it is declared.

In java,A sub class contructor will automatically make a call to its super class constructor but this is applicable only to default constructors.


While calling a super class construtor from a sub class constructor we use super() method.

super()
-------
A super() is used to make to call the constructor of a super class from a sub class constructor.

A super() must declared as a first statement in a sub class constructor.

A super() is of 2 types
default--->super()
       --->It is used to make a call to default constructor of a super class from a sub class constructor.

parameterized--->super(value1,value2,...);
                  or
             --->super(var1,var2,...);
--->It is used to make a call to parameterized constructor of a super class from a sub class constructor.

--->we can pass parameters from a sub class constructor to a super class constructor by using parameterized super() method.

Program to  demonstrate default super()
---------------------------------------
class A
{
   A()
   {
   
     System.out.println("A");
   }
 
}

class B extends A
{
   B()
   {
     super();
     System.out.println("B");
   }
}
class C extends B
{
   C()
   {
    super();
    System.out.println("C");
   }
   public static void main(String args[])
   {
       C c=new C();

   }

}

output
------
A
B
C
Important points
----------------
In the above code we are making a call to super class constructor B() from C() by using super(),similary from B() to A().


In case of default constuctors,if we don't declare a super() in a sub class constructor then the compiler will implicitly(automatically) add one super() in a sub class constructor.

program to demonstrate Parameterized super()
--------------------------------------------
class A
{
  A(int x,int y)
  {
    System.out.println("A "+x+y);
  }
}
class B extends A
{
  B(int x,int y,int z)
  {
    super(x,y);
    System.out.println("B "+x+y+z);
  }
}
class C extends B
{
  C(int x)
  {
    super(x,10,20);
    System.out.println("C "+x);
  }
  public static void main(String args[])
  {
    C c=new C(10);
   
  }
}



if we declare a constructor of a super class as private then it is not possible to inherit that super class.

Program
-------
class A
{
 
   private A()
   {
   }

}

class B extends A
{
   B()
   {
   }

}

The above code leads to CE saying  A() has private access.

No comments: