bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!
×
UPTO
50%
OFF!

super keyword in java

 

super keyword 

It is a reference variable which always refers to the immediate parent class object. It performs following operations:

 

1. Used to call the immediate parent class constructor.

e.g.

class Fruit{

Fruit(){

System.out.println(“Fruit class created”);

}

}

class Apple extends Fruit{

Apple () {

super();//It will call parent class constructor

System.out.println(“Apple class created”);

}

public static void main(String args[]){

Apple a=new Apple ();

}

}

Output

Fruit class created

Apple class created

 

2. Refer immediate parent class instance variable

e.g. 

class Fruit{

int quantity=20;

}

public class Apple extends Fruit{

int quantity=10;

void show(){

System.out.println(super.quantity);/*It will print Fruit’s quantity that is Refer immediate parent class instance variable */

System.out.println(quantity);

}

public static void main(String args[]){

Apple ap=new Apple ();

ap.show();

}

}

Output

20

10

 

3. Can call immediate parent class method

e.g.

class Fruit{

void print(){

System.out.println("Fruit class created");}

}

public class Apple extends Fruit{

void print(){

System.out.println("Apple class created");

}

void show(){

super.print();// It will call parent class method

print(); // It will call apple class method

}

public static void main(String args[]){

Apple ap = new Apple();

ap.show();

}

}

Output

Fruit class created

Apple class created

Learn more about Cross-Platform Mobile Technology for Android and iOS using Java in this insightful blog now!

"0 Responses on super keyword in java"

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

  • To avail this offer, enroll before 18th November 2017.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer

Sign Up or Login to view the Free super keyword in java.