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:

  • How and Why to use main() method in java? In this post i will tell you something interesting about main() method in java. Like What is the use of main() method? How we are going to use main() method? Why we are using main() method in our java program? You will find… Read More
  • Compilation and Interpretation in java In this post i will tell you something about compilation which is a task of compiler and on other side interpretation which is the task of interpreter.  There are few queries related to this topic which i am going … Read More
  • Importance of " import " statement in java Import statement in java is used in java to import all the predefined classes defined in a package and within that classes many methods are defined. Package is just like the directory in the file system.  Whenever we a… Read More
  • Literals in java Literals in java define the actual value we are using for variables, constants or to perform any operation. There are seven literals in java in which one literals is introduced from java 7 i.e binary literals. They are a… Read More
  • Program to print a simple text in java After installing java and setting the path for it now its turn to have some fun with codes. Now i am going to tell you how to print your text in java. Its a very simple program and basically for beginners. There are some im… Read More

2 comments:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic