super keyword and super() method
super keyword in Java:
super keyword is used to get the immediate parent properties(variable values , method ) in parent child inheritance.
For example, if parent class has int i = 10 and
child class has int i = 20; Then calling i with super keyword will return 10.
super can be used to call immediate parent class method. If you have
a method in parent class and have a
method with same name in child class then , using super keyword parent class
method will be called.
When child class and parent class have similar properties, then always
child class properties take precedence. To shift the presidency to parent
class, we use super keyword.
For example, let's take a parent class and child class both having
method "methodA" and both
having variable int i as below :
public class ParentClass {
int i = 10;
public void methodA() {
System.out.println("This is parent class method");
}
}
public class ChildClass extends ParentClass {
int i = 20;
public void methodA(){
System.out.println("This is child class method");
}
}
Let's call the methods as below without using the super keyword :
public class superKeyword {
public static void main(String[] Args) {
ChildClass obj = new ChildClass();
obj.methodA();
System.out.println("The value of i is = " +obj.i);
}
}
Output:
As you can see below, the output is showing the result from child class
only without super keyword.
This is
child class method
The value of i is = 20
Now, Let's use
"super" keyword to get output from parent class as below :
public class ParentClass {
int i = 2;
public void methodA() {
System.out.println("This is parent class method");
}
}
public class ChildClass extends ParentClass {
int i = 20;
public void methodA(){
System.out.println("This is child class method");
}
//Since
super keyword is not supported by main method, below code is created in child class and is called in the main method.
int z = super.i;
public void methodB() {
super.methodA();
}
}
Now, to run , let's call the method as below in the main class:
public class superKeyword {
public static void main(String[] Args) {
ChildClass obj = new ChildClass();
obj.methodB();
System.out.println("The value of i is = " +obj.z);
}
}
Output:
This is
parent class method
The value of i is = 2
Hence we can use "super" keyword to get
immediate parent class values.
Suppose there is multilevel
inheritance, then to get properties of grand parent class having same
properties like in parent and child classes , we can use two super keywords
like below :
public class GrandParentClass {
int i =5;
public void methodA() {
System.out.println("This is grand parent class method.");
}
}
public class ParentClass extends GrandParentClass {
int i = 10;
public void methodA(){
System.out.println("This is parent class method");
}
//Since
super keyword is not supported by main method, below code is created in parent class and is called later.
int n = super.i;
public void methodB() {
super.methodA();
}
}
public class ChildClass extends ParentClass {
int i = 20;
public void methodA(){
System.out.println("This is child class method");
}
//Since
super keyword is not supported by main method, below code is created in child class and is called in the main method.
int z = super.n;
public void methodC() {
super.methodB();
}
}
public class superKeyword {
public static void main(String[] Args) {
ChildClass obj = new ChildClass();
obj.methodC();
System.out.println("The value of i is = " +obj.z);
}
}
Output:
This is
grand parent class method.
The value of i is = 5
"this" keyword an example how "super" is difference than "this"
public class ParentClass {
int i =10;
public void methodA(int i){
System.out.println("The value of i = "+ i);
}
public static void main(String[] Args){
ParentClass jj = new ParentClass();
jj.methodA(5);
}
}
Output:
The value of i = 5
public class ParentClass {
int i =10;
public void methodA(int i){
System.out.println("The value of i = "+this.i);
}
public static void main(String[] Args){
ParentClass jj = new ParentClass();
jj.methodA(5);
}
}
Output:
The value of i = 10
super() method
Using "super()" , you
can call the immediate parent constructor.
Let's have below three classes and will see how super() will call each
immediate parent constructor.
public class GrandParentClass {
GrandParentClass() {
System.out.println("This is grand parent class Constructor.");
}
}
public class ParentClass extends GrandParentClass {
ParentClass() {
super();//writing
super() is optional because even if you do not write super(), JVM automatically
calls the super().
System.out.println("This is Parent Class Constructor.");
//There is no need to write super() as JVM automatically
provides this if you do not write. However, if you write , you have to write
super() as a first statement in the consturctor . If you write super() here ,
ie, at the position other than first statement, it shows compilation error.
}
}
public class ChildClass extends ParentClass {
ChildClass() {
super();//writing super() is optional because even if you do not write
super(), JVM automatically calls the super(). So you can just write
ChildClass() {
} and you will get same result.
}
}
Let's run with main method below:
public class superKeyword {
public static void main(String[] Args) {
ChildClass
obj = new ChildClass();
}
}
Output:
This is
grand parent class Constructor.
This is Parent Class Constructor.
Let's call
parameterized or default constructors using super()
Each classes are having parameterized and default constructors as
shown below:
public class GrandParentClass {
GrandParentClass() {
System.out.println("This is grand parent default class Constructor.");
}
GrandParentClass(int i) {
System.out.println("This is grand parent class parameterized Constructor with
i = "+i);
}
}
public class ParentClass extends GrandParentClass {
ParentClass() {
super(5);
System.out.println("This is Parent Class default Constructor.");
}
ParentClass(int i) {
super();
System.out.println("This is Parent Class parameterized Constructor with i =
"+i);
}
}
public class ChildClass extends ParentClass {
ChildClass() {
super(2);
}
ChildClass(int n) {
super();
}
}
Let's call
combination of parameterized and default constructors:
public class superKeyword {
public static void main(String[] Args) {
ChildClass obj = new ChildClass();
}
}
Output(Child default - Parent
Parameterized - GrandParent default):
This is
grand parent default class Constructor.
This is Parent parameterized Class Constructor with i = 2
Let's call in a
different way:
public class superKeyword {
public static void main(String[] Args) {
ChildClass obj = new ChildClass(1);
}
}
Output(Child Parameterized -
Parent default - GrandParent Parameterized):
This is
grand parent parameterized class Constructor with i = 5
This is
Parent default Class Constructor.
No comments:
Post a Comment