AdSense

AdSense3

Saturday, 27 June 2015

super and this- similarities and differences

this(current class)super(super class)
1.It is a keyword used to store current object reference.1.It is a keyword used to store super class object in sub class.
2.Pre define instance variable used to hold current object reference.2.Pre define instance variable used to hold super class object reference through sub class object.
3.Used to separate state of multiple objects of same class and also used to separate local variables and class level variables in a non-static method if both have same name.3.Used to seperate super class and subclass members if both have same name.
4.It must be used explicitly if non-static variables and local variables or parameter name is same.4.It must be used explicitly if super class and sub class members have same names.
5.Can't be referred from static context. It can be printed, means can be called from System.out.println. For example System.out.println(this.x);5.Can't be referred from static context.It can't be printed, means cannot be called from System.out.println. For example System.out.println(super.x); it leads to compile time error.

No comments:

Post a Comment