Java provides the super keyword that enables a subclass to refer to its superclass. The super keyword is used to access:
- superclass constructors
- superclass methods and variables
Consider a Java application, which consists of multiple classes. The subclass in the application needs to reuse the constructor of the superclass. In this application, super keyword is used as a method and invokes the constructor of its immediate superclass. The syntax to invoke the constructor of a superclass using the super() method is:
super (<parameter1>, <parameter 2>,..,<parameterN>);
In the above syntax, <parameter1>, <parameter 2>,..,<parameterN> refer to the list of parameters that you need to pass to the constructor of the superclass.
If no parameters are passed to the super() method, it invokes the default constructor of the superclass. If the superclass contains the overloaded constructor, you can pass parameters to the super() method to invoke a particular constructor. When you use the super() method in the constructor of the subclass, it should be the first executable statement in the constructor.
Consider a Java application, in which both, superclass and subclass, have member variables with the same name. In this application, the super keyword is used to access the member variables of the superclass. The syntax to access the member variable of a superclass is:
super.<variable>;
The subclass can also access the member methods of the superclass using the super keyword. The following code snippet shows the Literature subclass accessing the member variable of the Book superclass:
class Book
{
int categoryCode;
}
class Literature extends Book
{
int categoryCode;
public void input(int c1, int c2)
{
super.categoryCode=c1;
categoryCode=c2;
}
}
In the above code snippet, the Book class contains the categoryCode variable to store the category of a book, such as literature, science, fiction, suspense, and history. The Literature class also contains the categoryCode variable to store the subcategory of a book. For example for literature category, the subcategories are English, French, Hindi, and Russian. The input() method accepts two values for c1 and c2 variables. The value that you pass for c1 variable is assigned to the superclass variable whereas the value of the c2 variable is assigned to the subclass variable. The this keyword is used to refer to the current object. You can use the this keyword when a method defined in a Java class needs to refer to the object used to invoke that method, as shown in the following code snippet:
Book(int bcode, double bprice)
{
this.bookCode=bcode;
this.bookPrice=bprice;
}
In the above code snippet, the this keyword refers to the object that invokes the Book() constructor.
Another situation where you can use the this keyword is when the local and instance variables have the same name. For example, the Book class has two member variables, bookCode and bookPrice. The constructor of the Book class is used to initialize its member. If the formal parameters passed to the constructor have the same name as instance variables, you need to use the this keyword to refer to the
instance variables. The following code snippet shows how to use the this keyword when instance and formal parameters have the same name:
Book(int bcode, double bprice)
{
this.bcode=bcode;
this.bprice=bprice;
}
In the above code snippet, this keyword refers to the instance variables, bcode and bprice. The values of the formal parameters, bcode and bprice of the Book() constructor are assigned to the instance variables.
In addition, you can use the this keyword in a constructor of a class to invoke another constructor of the class. Unlike the super keyword, the this keyword can invoke the constructor of the same class. The following code snippet shows how to invoke a constructor using the this keyword:
class Book
{
public Book(String bname)
{
this(bname, 1001);
}
public Book(String bname, int bcode)
{
bookName=bname;
bookCode=bcode;
}
}









