Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language.
The super is a keyword defined in the java programming language.
super keyword is used to call a superclass constructor and to call or access super class members(instance variables or methods).
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.
The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
The super keyword as a standalone statement is used to call the constructor of the superclass in the base class.
'super’ is used for pointing the super class instance.
syntax of super :
super(arg-list):
When a subclass calls super() it is calling the constructor of its immediate superclass.
super() must always be the first statement executed inside a subclass constructor.
super.member:
Here member can be either method or an instance variables.This second form of super is most applicable to situation in which member names of a subclass hide member of superclass due to same name.
Example:
**************************************************
public class SuperKeywordExample {
int i = 10;
static int k = 30; //static variable
public SuperKeywordExample() {
System.out.println("This is superclass constructor");
}
public void getName() {
System.out.println("this is superclass method");
}
}
**************************************************
super.i = 20; //accessing superclass member from subclass
The super is a keyword defined in the java programming language.
super keyword is used to call a superclass constructor and to call or access super class members(instance variables or methods).
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.
The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
The super keyword as a standalone statement is used to call the constructor of the superclass in the base class.
'super’ is used for pointing the super class instance.
syntax of super :
super(arg-list):
When a subclass calls super() it is calling the constructor of its immediate superclass.
super() must always be the first statement executed inside a subclass constructor.
super.member:
Here member can be either method or an instance variables.This second form of super is most applicable to situation in which member names of a subclass hide member of superclass due to same name.
Example:
**************************************************
public class SuperKeywordExample {
int i = 10;
static int k = 30; //static variable
public SuperKeywordExample() {
System.out.println("This is superclass constructor");
}
public void getName() {
System.out.println("this is superclass method");
}
}
public class SuperKeywordDemo extends SuperKeywordExample {
int i = 80;
public SuperKeywordDemo() {
super();// Following lines calls the super class constructor
System.out.println("this is sub class constructor ");
}
public void getName() {
super.getName();// Calls the getName of super class methodsuper.i = 20; //accessing superclass member from subclass
System.out.println("this is subclass method");
System.out.println("value of i in sub class is " + i);
System.out.println("value of i in super class is " + super.i);
/**
* accessing superclass static member from subclass.
* It won't give any compilation error. It will show warning(The static field SuperKeywordExample.k should be accessed in a static way).
* But this is not recommended.
*/
System.out.println("value of k in super class is " + super.k);
/**
* accessing superclass static member from subclass.
* It won't give any compilation error. It will show warning(The static field SuperKeywordExample.k should be accessed in a static way).
* But this is not recommended.
*/
System.out.println("value of k in super class is " + super.k);
}
/**
* @param args
*/
public static void main(String[] args) {
SuperKeywordDemo demo = new SuperKeywordDemo();
demo.getName();
}
}
*****************************************************
No comments:
Post a Comment