- super() is used to refer immediate parent class variable
- super() is used to invoke immediate parent class method
- super() is used to invoke immediate parent class constructor
super() is used to refer immediate parent class variable: <br>By calling a variable like this, we can access the variable of parent class if both the classes (parent and child) have same variable.<br><br>
When you have a variable in child class which is already present in the parent class then in order to access the variable of parent class, you need to use the super keyword.<br><br>
package com.cerotid.fundamentals;
class Parent1 {
int i = 10;
}
class Child1 extends Parent1 {
int i = 20;
void m1() {
System.out.println("value of i in child class: " + i);
System.out.println("value of i in parent class: " + super.i);
}
}
public class SuperKeyword {
public static void main(String[] args) {
Child1 c = new Child1();
c.m1();
}
}
Output:
value of i in child class: 20
value of i in parent class: 10
super() is used to invoke immediate parent class method: Representation of parent class method
package com.cerotid.fundamentals;
class ParentM {
void m1() {
System.out.println("Parent m1 method");
}
}
class ChildM extends ParentM {
void m2() {
super.m1();
System.out.println("Child m2 method");
}
}
public class SuperKeyWordMethodRepresentation {
public static void main(String[] args) {
ChildM c = new ChildM();
c.m2();
}
}
Output:
Parent m1 method
Child m2 method
What if the child class is not overriding any method: No need of super
When child class doesn’t override the parent class method then we don’t need to use the super keyword to call the parent class method.
package com.cerotid.fundamentals;
class ParentM2 {
void display() {
System.out.println("Parent method");
}
}
class ChildM2 extends ParentM2 {
void printMsg() {
super.display(); // parent method
System.out.println("Child method"); // child method
display(); // parent method
}
}
public class SuperKeyWordMethodRepresentation2 {
public static void main(String[] args) {
ChildM2 c2 = new ChildM2();
c2.printMsg();
}
}
Output:
Parent method
Child method
Parent method
How to use super keyword in case of method overriding
package com.cerotid.fundamentals;
class ParentM2 {
void display() {
System.out.println("Parent method");
}
}
class ChildM2 extends ParentM2 {
void display() {
super.display(); // parent method
System.out.println("Child method"); // child method
}
}
public class SuperKeyWordMethodRepresentation2 {
public static void main(String[] args) {
ChildM2 c2 = new ChildM2();
c2.display();
}
}
Output:
Parent method
Child method
super() is used to invoke immediate parent class constructor and this() keyword is used to invoke the current class constructor:
When we create the object of Child class, the new keyword invokes the constructor of child class, which implicitly invokes the constructor of parent class. So the order to execution when we create the object of child class is: parent class constructor is executed first and then the child class constructor is executed. It happens because compiler itself adds super()(this invokes the no-arg constructor of parent class) as the first statement in the constructor of child class.
package com.cerotid.fundamentals;
class Parent {
Parent() {
System.out.println("Parent class 0-arg constructor");
}
}
class Child extends Parent {
Child(){
/* while executing child class 0-arg constructor, I would like to call
* 1-arg current class constructor. for this, use this(10)
*/
this(10);
/* Compiler implicitly adds super() here as the
* first statement of this constructor.
*/
System.out.println("Child class 0-arg constructor");
}
Child(int a){
//super();
/* Even though it is a parameterized constructor.
* The compiler still adds the no-arg super() here
*/
System.out.println("child class 1-arg constructor");
}
}
public class SuperKeyWordConstructor {
public static void main(String[] args) {
/* Creating object using default constructor. This
* will invoke child class constructor, which will
* invoke parent class constructor
*/
Child c1 = new Child();
}
}
Output:
Parent class 0-arg constructor
child class 1-arg constructor
Child class 0-arg constructor
When we have a constructor in parent class that takes arguments then, to invoke parameterized constructor of parent class from the constructor of child class super( parameter) is used.
package com.cerotid.fundamentals;
class Parent {
Parent() {
System.out.println("Parent class 0-arg constructor");
}
Parent(String str){
System.out.println("Parent class-1 arg constructor");
}
}
class Child extends Parent {
Child(){
/* while executing child class 0-arg constructor, I would like to call
* 1-arg current class constructor. for this, use this(10)
*/
this(10);
/* Compiler implicitly adds super() here as the
* first statement of this constructor.
*/
System.out.println("Child class 0-arg constructor");
}
Child(int a){
super("Ram");
/* Even though it is a parameterized constructor.
* The compiler still adds the no-arg super() here
*/
System.out.println("child class 1-arg constructor");
}
}
public class SuperKeyWordConstructor {
public static void main(String[] args) {
/* Creating object using default constructor. This
* will invoke child class constructor, which will
* invoke parent class constructor
*/
Child c1 = new Child();
}
}
Output:
Parent class-1 arg constructor
child class 1-arg constructor
Child class 0-arg constructor
package com.cerotid.fundamentals;
class Parent {
{
System.out.println("Parent class instance block");
}
Parent() {
System.out.println("Parent class 0-arg constructor");
}
static {
System.out.println("Parent class static block");
}
}
class Child extends Parent {
{
System.out.println("Child class instance block");
}
Child(){
System.out.println("Child class 0-arg constructor");
}
static {
System.out.println("child class static block");
}
}
public class SuperKeyWordConstructor {
public static void main(String[] args) {
/* Creating object using default constructor. This
* will invoke child class constructor, which will
* invoke parent class constructor
*/
Child c1 = new Child();
Child c2 = new Child();
}
}
Output:
Parent class static block
child class static block
Parent class instance block
Parent class 0-arg constructor
Child class instance block
Child class 0-arg constructor
Parent class instance block
Parent class 0-arg constructor
Child class instance block
Child class 0-arg constructor
This keyword:
Using the keyword 'this', we can refer the members of a class such as constructors, variables and methods. This keyword is used only within the instance methods or constructors.
- In variables: Differentiate the instance variable from the local variable if they have the same names, within a constructor or a method
- this() can be used to invoke the current class constructor
- to invoke the current class method
- can be used to pass argument in method and constructor
- can be used to return the current class instance
Important Notes:
- To call the super class constructor, use super(); and to call current class constructor, use this();
- Inside the constructor keyword must be the fist statement
- Inside the constructor, it is not possible to use super and this keyword at a time
- In the child class, when we are calling super class 0-arg constructor and there is no 0-arg constructor in parent class, default constructor will be generated
- In any constructor whether it is 0-arg or it is 1-arg constructor, if we are not declaring this or super keyword, then compiler generates super keyword at the fist line of constructor
- When there is no constructor in the child class, default constructor will be generated and super keyword also will be generated. If there is parent class 0-arg constructor then it will be executed
- Instance blocks are executed during object creation and execution of instance block depends on object creation for example, 10 times object creation means 10 times instance block will be executed. First, parent class static block, then child class static block, then parent class instance block, then parent class constructor then child class instance block and finally child class constructor will be executed.
- Static blocks are executed during dot class loading and hence static blocks are executed only once.
No comments:
Post a Comment