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:

  • 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
  • Type Casting in Java Type casting in java allow the users to convert one form of data type into another but it should be between same " type " that means we cannot type primitive type to String or any other reference type and vic… 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
  • Control statement in Java Control statement in any programming language is used to control the flow of the statement in the application. That is why it is termed as " control ". There are three types of control statement i.e 1> Conditional control… Read More
  • " new " and " instanceof " Operator in Java new operator refers to the object creation where as instanceof used with the reference variable. Details of which we are going to discuss late in this topic.  What is " new " Operator and its uses in Java ? … Read More

2 comments:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201979

Online Members

Live Traffic