The super keyword can refer immediate parent class instance variable or invoke immediate parent class method. The ‘super’ is a reference variable that is used to refer the immediate parent class object. The new class is known as the child class or subclass. The already existing class is known as the parent class or superclass. It allows using the properties and methods of the already existing class to a new class. Inheritance is a major concept of Object Oriented Programming. The keyword ‘super’ is related to inheritance. In the parameterized constructor, there is a statement like this() It will call the current class constructor that is A(). When creating an object of A, the parameterized constructor is called. (“Parameterized Constructor A”) Īccording to the above program, class A has a default constructor and a parameterized constructor. It is also possible to use this keyword to invoke the current class constructor. Using this, the current class method was invoked. In method B, after printing B there is a statement as this.A(). When creating an object of Myclass and invoking the method B will print B, A as the output. The keyword ‘this’ can be used to invoke the current class method. Therefore, giving id and name to the constructor can set the instance variables. When using this, it refers to the current object. There are no values for instance variables. If the programmer writes id = id it won’t initialize the instance variables because the Constructor already has id and name. The id and name are passed to the Employee constructor. Class variables are shared by all objects. Local variables are the variables belongs to methods. According to the above program, class Employee has two instance variables. They are instance variables, local variables and class variables. In Java, there are three types of variables. Refer the given Java program.įigure 01: Java program using this keyword ![]() The keyword ‘this’ is used to refer a current object. ![]() Side by Side Comparison – this vs super in Java in Tabular Form Similarities Between this and super in Javaĥ. The key difference between this and super is ‘ this’ is a reference variable that is used to refer the current object while ‘super’ is a reference variable that is used to refer immediate superclass object. The ‘this’ keyword is used to refer a current object. There are multiple objects in the system. The super keyword can be used to refer an object of the superclass. The classes that already exist are superclasses, and the derived classes are subclasses. The program or software can be modeled using objects. Java supports Object Oriented Programming (OOP). These keywords cannot be used as variables or any other identifier name. The keywords ‘this’ and ‘super’ are used in Java programming.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |