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 ?
Important points regarding super in java
Important uses of super
An example to show how super is working
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-
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.
--> 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);
}
}
Super keyword in java
ReplyDeleteThanks
ReplyDeletenice article for beginners.thank you.
java tutorial
java tutorial