Reference : Reference is an abstract identifier for a block of memory in the heap.
Special Reference: There are three special references in Java source code.
- null
- this
- super
null reference : The null reference is an invalid reference. It has no type , and may be assigned to a variable of any reference type. When a reference variable is declared but not constructed , it is initially equal to null.
this reference : The this reference always refers to the current object, method .
Example: int j = this.x;
super reference : The super reference refers to the methods and fields of the immediate super class. You need to use super prefix only if a field or method in the subclass has same name as a field or method in super class.
If you use super() as the first statement in a constructor, it calls the matching constructor in the immediate super class based on parameters passed to super() .
If you do not include an explicit call to super() as the first statement in your constructor, then the compiler will insert such a call into the byte code. The compiler always chooses to no arguments super() constructor if you don't explicitly choose a different one.
Example:
public class SuperClass{
public SuperClass(int i){
}
}
class SubClass extends SuperClass{
public SubClass(){
}
}
Note: If you try to compile this program you get the error message "no constructor matching SuperClass() found in class SuperClass. Because constructor chaining is mandatory in super-sub hierarchy.If you provide a parameterised constructor in super class then always put a noargs constructor in super class.
Example:
public class SuperClass{
public SuperClass(int i){
}
}
class SubClass extends SuperClass{
public SubClass(){
}
}
Note: If you try to compile this program you get the error message "no constructor matching SuperClass() found in class SuperClass. Because constructor chaining is mandatory in super-sub hierarchy.If you provide a parameterised constructor in super class then always put a noargs constructor in super class.
0 comments:
Post a Comment