Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

This and Super Keyword in Java

In the previous video of our Core Java course we have seen all about what are Keywords in Java programming language with example to understand it better. Now, in this video we are going to learn about what are This and Super keyword in Java.

This keyword in Java is a reference variable that refers to the current object. It can be used inside the method or Constructor of class.
Example :
class boy1
{
int id;
String name;
boy1(int id,String name)
{
this.id = id;
this.name = name;
}
void display()
{
(System.out.println (id+" "+name);
}
public static void main (String args [])
{
boy1 s1 = new boy1(11,"karan");
boy1 s2 = new boy1 (12, "kriti");
s1.display ();
s2.display ();
}
}
Output :
11 karan
12 kriti

The Super keyword in Java is used to refer immediate parent class object. 'super' is used to refer immediate parent class instance variable. super() is used to invoke immediate parent class constructor. Super is used to invoke immediate parent class method.

To differentiate between base class features and derived class features, it must be preceded by super keyword in Java.
Syntax :
super.base class member name

In order to differentiate between the data member of base class and derived class, in the context of derived class the base class data members, must be preceded by super keyword in Java.
Example :
class Employee
{
float salary=10000;
}
class HR extends Employee
{
float salary=20000;
void display ()
{
System.out.println ("Salary: "+super.salary);
}
}
class val
{
public static void main (String[] args)
(
HR obj=new HR();
obj.display();
}
}
Output :
Salary : 10000.0

Hence, Super keyword in Java is always used to refer to the base class.

Super keyword in Java is used when base class method name and derived class method name have same name.
Example :
class Person
{
void message()
{
System.out.println ("welcome");
}
}
class Student9 extends Person
{
void message()
{
System.out.println("welcome to java");
}
void display()
{
message();
super message();
}
public static void main (String args [])
{
Student9 s=new Student9();
s.display();
}
}
Output :
welcome to java
welcome

So, this are This and Super keywords in Java programming language. In our next video we will learn about Super Keyword at Constructor Level in Java with accurate example.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere