0% found this document useful (0 votes)
13 views

Super Keyword in Java

The "super" keyword in Java is used to refer to the immediate parent class. It allows access to methods, fields, and constructors of the parent class from the child class. Specifically: 1) super can be used to refer to parent class instance variables and fields. 2) super can be used to invoke or call parent class methods. This is needed when a method is overridden in the child class. 3) super() can be used to invoke or call the parent class constructor from the child class constructor. The super() call must be the first line in the child class constructor.

Uploaded by

bisoyisagar2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Super Keyword in Java

The "super" keyword in Java is used to refer to the immediate parent class. It allows access to methods, fields, and constructors of the parent class from the child class. Specifically: 1) super can be used to refer to parent class instance variables and fields. 2) super can be used to invoke or call parent class methods. This is needed when a method is overridden in the child class. 3) super() can be used to invoke or call the parent class constructor from the child class constructor. The super() call must be the first line in the child class constructor.

Uploaded by

bisoyisagar2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Super Keyword in Java

The “super” keyword in Java is a reference variable which is used to refer immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred
by super reference variable.

Usage of Java super Keyword

1. super can be used to refer immediate parent class instance variable.


2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

1) super is used to refer immediate parent class instance variable.


We can use super keyword to access the data member or field of parent class. It is used if parent class and
child class have same fields.

class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color); //prints color of Dog class
System.out.println(super.color); //prints color of Animal class
}
}
class TestSuper {
public static void main(String args[]) {
Dog d=new Dog();
d.printColor();
}
}

2) super can be used to invoke parent class method


The super keyword can also be used to invoke parent class method. It should be used if subclass contains the
same method as parent class. In other words, it is used if method is overridden.
class Animal {

void eat() {

System.out.println("eating...");

class Dog extends Animal {

void eat() {

System.out.println("eating bread...");

void bark() {

System.out.println("barking...");

void work() {

super.eat();

bark();

class TestSuper {

public static void main(String args[]) {

Dog d=new Dog();

d.work();

• In the above example Animal and Dog both classes have eat() method if we call eat() method from
Dog class, it will call the eat() method of Dog class by default because priority is given to local.
• To call the parent class method, we need to use super keyword.

3) super is used to invoke parent class constructor.


The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:
class Animal {
Animal() {
System.out.println("animal is created");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("dog is created");
}
}
class TestSuper {
public static void main(String args[]) {
Dog d=new Dog();
}
}
Note: super() is added in each class constructor automatically by compiler if there is no super() or this().

As we know well that default constructor is provided by compiler automatically if there is no constructor.
But, it also adds super() as the first statement.
Another example of super keyword where super() is provided by the compiler implicitly.
class Animal{
Animal(){
System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
System.out.println("dog is created");
}
}
class TestSuper{
public static void main(String args[]){
Dog d=new Dog();
}
}

You might also like