Like this in java one more most important keyword is there i.e super. super in Java has a wide use and it is mainly used to call the super class constructor explicitly by the user and implicitly used by the JVM to initialize the super class property.
Have you ever wondered that how super class members get instantiated by creating subclass object ?
Let suppose we have two class A and B following inheritance relation where B is extending A that means B is a sub-class and A is a super-class. Here if we create a sub-class object which is going to invoke the constructor of sub-class and then compiler will implicitly place super() to invoke the super-class constructor which is used to instantiate the super-class members.

Important points regarding super in java

1. If there are no constructor then a default constructor is invoke by the JVM to place a super() which will call the super-class constructor.

2. If we have any constructor with some implementation then also compiler will place one super() to call super-class constructor.

3. If we have a parameterized constructor and in which first statement is this( value ) , then it will recursively call the sub-class constructor and hence there will no chance to call super-class constructor. In this case compiler will give compilation error.

4. Always make sure that if a class in inheriting another class then there must  be a super() call through sub-class constructor either explicitly by the user or implicitly by the JVM to initialize the super-class property , otherwise compiler will give compilation error.

Important uses of super

1. It is used to initialize the super-class property by invoking super-class constructor from sub-class constructor.

2. Inherited members are accessible with the help of super.

3. If we have member defined in the sub-class and super-class with the same name then super is used to refer the super-class member where as this is used to refer current class member.

4. It is used to refer the member of current object which is inherited from super-class.

5. In a inheritance hierarchy at last super() is calling to object class constructor which indicates the end of calling super-class constructor.

An example to show how super is working

class A{

int x;   //super-class instance member

}

public class B extends A {

int x;       // sub-class instance member

B(int x){    //local variable x as an argument in constructor

System.out.println(x);     //x indicate local variable

System.out.println(this.x);  //this.x indicate current class 
                                instance member x

System.out.println(super.x); //super.x indicate current object 
                                having inherited member x .

}

public static void main(String sush[]) {

 B b = new B(10);

 }
}

In the above program every variable is having same name so in this case ,
1. If we write simply x then JVM will prefer local variable.
2. If we write this.x then JVM will search for any instance variable in the current class.
3. So to access the super class instance variable x we have to use super.

What if in the above program class A extends another class X then it will also have an instance variable x then how will access that member as super will only refer to its immediate super-class ?
--> To access the instance member x of class X we have to use the method which will return the value of x from class X. Using statement like super.super.x is invalid . for ex-

class X{

int x;           // instance member of class X

int value(){    // method returning x value of class X

this.x=40;     // initializing x to 40. 
return x;
    }
}

class A extends X{

int x;       // instance member of class A

}

public class B extends A {

int x;

B(int x){

System.out.println(x);

System.out.println(this.x);

System.out.println(super.x);

System.out.println(value());

}

public static void main(String[] args) {

 B b = new B(10);

 }
}



Related Posts:

  • Assignment Operator ( = ) in Java It is a kind of operator which has a wide use in any programming language and as it name suggest it is used to assign value to a variable and that value may be a direct value or a variable or we can say that it is used to a… Read More
  • Relational Operator in Java Relational Operator in Java is used where we use any comparison between two value or two variable or a value and a variable, but while comparing variables , it must some numeral value and hence all the operators used for … Read More
  • String Concatenation Operator ( + ) in Java In java " + " symbol is used to concatenate two strings where as in case numerals it will add up the values. It has wide use as an String concatenation. String concatenation means adding/ merging two or more than two st… Read More
  • Increments (++) and Decrements (--) Operators in Java This is most useful and confusing operator in java or any programming language. It is used to increase and decrease the value of a variable by 1. It is a Unary operator but the operand must be a variable containing num… Read More
  • Logical and bit-wise operators in java In java both logical and bitwise operators are used. Logical operator returns the true / false value depends on the condition check and bitwise return the same true / false depends on the condition check but the difference … Read More

2 comments:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

202001

Online Members

Live Traffic