Thursday , September 21 2017
Home / JAVA / Java program to use super keyword

Java program to use super keyword

Super is a keyword which refers to reference of base class. Super is used to call any constructor or any method of base class but it must be 1st line of the derived class constructor.

We use the super keyword in java for two purposes:

1-      Super keyword for calling base class constructor.
2-      Super keyword for calling base class function.

Calling Constructor

In this we have two class parent class and child class and define the constructor for each class. Now if we want to call the constructor of base class than we use super keyword for calling the base class constructor. Also super keyword must be use inside another child class constructor and must be the first line in the child class constructor.

Example #1: 

class superclass
{
superclass()   // constructor of parent class
{
System.out.println("This is a superclass constructor");
}
}  // end of parent class

class subclass extends superclass
{
subclass()  // subclass constructor 
{
super();  // calling base class constructor from child class using this super keyword
System.out.println("This is a subclass constructor");
}
public static void main(String args[])
{
subclass obj=new subclass();  // creation of object of child class
} 
}

Example #2: 

class Class1  // parent class
{
int mul,sum;
Class1(int s)  // constructor with single argument
{
mul=s*s;  
System.out.println("Multiplication is "+mul);
}

Class1(int a,int b) // constructor with 2 arguments
{
sum=a+b;
System.out.println("Sum is "+sum);
}

}

class Class2 extends Class1     // child class inherit the properties of parent class
{ 
int add;
Class2()  // constructor with no arguments
{
super(5);  // calling single argument base constructor
System.out.println("This is child class constructor");
}

Class2(int c,int d)   // constructor with 2 arguments
{
super(5,4);  // calling base class constructor with 2 arguments
add=c+d;
System.out.println("Addition is "+add);
}
public static void main(String args[])
{
Class2 obj=new Class2();  // calling no argument constructor of child class
Class2 obj1=new Class2(3,2); // calling 2 arguments constructor of child class
}
} // end of class


Method Overriding

The method of calling base class constructor and base class method are same as by using the super keyword which must be the 1st line of the method in the child class. Its technique of using super keyword is same as in the constructor. Here we show the program of calling base class method with same name but different functionality. But if we have to call the base class method as it is than we call it through the super keyword.

class Func_super // base class
{
void Function1()  // method of base class
{
System.out.println("This is base class function");
}
}

class Child_super extends Func_super
{
void Function1()  //overriding  method of child class
{
System.out.println("This is Derived class function");
}
void Function2() // another method of child class
{
super.Function1();  // calling base class method
}
public static void main(String args[])
{
Child_super obj=new Child_super();  // object of child class
obj.Function2(); // calling child class method which in turn call base class method
obj.Function1(); // calling child class method
}
}

 

 

Check Also

Program to check Upper Triangular matrix

What is Upper Triangular Matrix?  An upper triangular matrix is a square matrix in which ...

Leave a Reply

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