Super keyword in java with example in 2020

Super keyword in java with example in 2020

Posted on

Super keyword in java with example

Super keyword in java Define to the objects of actual parent class. Prior to learning super keyword you must have the wisdom offortune in Java so that you can conclude the examples given in this guide.

Super keyword in java with example in 2020

The uses of super keyword

1) To Get the data members of parent class when one and the other parent and child class have component the very Name
2) To In a clear and detailed manner call the no-arg and parameterized makes something of parent class
3) To Get the method of parent class when child class has overridden that method.

Now lets discuss them in detail with the help of examples:

1) How to use super keyword to entry the variables of parent class
When you have a variable in Kid class which is already availble in the parent class then in order to Authority the variable of parent class, you need to use the super keyword.

Lets take an example to Teach this: In the following program, we have a data member num declared in the child class, the member with the very name is already present in the parent class. Hre is no Chance you can access the num fluctuating of parent class without using super keyword. .

Super keyword in java with example (Parent class)

//Parent class or Superclass or base class

class Superclass
{
int num = 1000;
}
//Child class or subclass or derived class
class Subclass extends Superclass
{
/* The very variable num is announced in the Subclass
* which is present in the Superclass
*/
int num = 1010;
void printNumber(){
System.out.println(num);
}
public static void main(String args[]){
Subclass obj= new Subclass();
obj.printNumber();
}
}
Output:
1010

Use Of the num variable of parent class:

By Checking a variable like this, we can Use the variable of parent class if Two the classes (parent and child) have Equal variable.Its Super keyword in java

super.variable_name
Let’s take the match example that we have seen above, this time in print statement we are passing super.num instead of num.

class Superclass
{
int num = 1000;
}
class Subclass extends Superclass
{
int num = 1010;
void printNumber(){
/* Note that instead of num we are
* writing super.num in the print statement
* the num variable of Superclass
*/
System.out.println(super.num);
}
public static void main(String args[]){
Subclass obj= new Subclass();
obj.printNumber();
}
}
Output:
1000
As you can watch by using super.num we accessed the num to many of parent class.

2) Use of super keyword to invoke creator of parent category
When we tend to produce the thing of sub class, the new keyword invokes the creator of kid categorythat implicitly invokes the creator of parent categorytherefore the order to execution after we produce the thing of kid category is: parent class creator is dead 1st so the kid class constructor is executed. It happens as a result of compiler itself adds super()(this invokes the no-arg creator of parent categorybecause the 1st statement within the constructor of kid class.
Let’s see Associate in Nursing example to grasp what I actually have explained above:

class Parentclass
{
Parentclass(){
System.out.println(“Constructor of parent class”);
}
}
class Subclass extends Parentclass
{
Subclass(){
/* Compile implicitly adds super() here as the
* first statement of this constructor.
*/
System.out.println(“Constructor of child class”);
}
Subclass(int num){
/* Even though it is a parameterized constructor.
* The compiler still added the no-arg super() here
*/
System.out.println(“arg constructor of child class”);
}
void display(){
System.out.println(“Hello!”);
}
public static void main(String args[]){
/* Creating object using default constructor. This
* will invoke child class constructor, which will
* invoke parent class constructor
*/
Subclass obj= new Subclass();
//Calling sub class method
obj.display();
/* Creating second object using arg constructor
* This will invoke arg constructor of child class
* invoke no-arg constructor of parent class automatically
*/
Subclass obj2= new Subclass(10);
obj2.display();
}
}
Output:

Constructor of parent class
Constructor of child class
Hello!
Constructor of parent class
arg constructor of child class
Hello!

Inspire…..

Know About Insurance

One thought on “Super keyword in java with example in 2020

Leave a Reply

Your email address will not be published. Required fields are marked *