The document explains the use of 'this' and 'super' keywords in Java. 'This' is used to refer to the current object and resolve ambiguity in variable names, while 'super' is used to access properties of the parent class. Examples illustrate how these keywords function in class constructors and methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views9 pages
This and Super Keyword
The document explains the use of 'this' and 'super' keywords in Java. 'This' is used to refer to the current object and resolve ambiguity in variable names, while 'super' is used to access properties of the parent class. Examples illustrate how these keywords function in class constructors and methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9
SUPER AND THIS
KEYWORDS IN JAVA 03/02/2025 2
In java, this keyword is used to THIS
refer the current Object inside a KEYWORDS method or a constructor. IN JAVA USING THIS FOR AMBIGUITY VARIABLE NAMES In java, it is not allowed to declare two variables having the same name inside a scope Example: class test USE { OF int a; class(int a) THIS { KEYWORD a=a; } } In the above program, the instance variable and a parameter have the same name: a .Here, the java compiler is confused due to name ambiguity.In such a situation, we use this keyword Using this keyword Example: Class text class text { { int age; int age; test (int age) test(int age) { { this.age= age; age=age; } } public static void main( String[] args) public static void main( String[] args) { { test obj= new test(8); test obj= new test(8); System.out.println("Age:"+obj.age); System.out.println("Age:"+obj.age); } } } } OUTPUT: OUTPUT: Age: 8 Age: 0 SUPER KEYWORD The superkeyword is used to SUPER access the parent class or super KEYWORD class, of a subclass. Used to refer to the superclass properties from subclass We can refer only to the immediate superclass USAGE OF It is optional to use super keyword when the SUPER subclass non static member names and super KEYWORD class non static member names are different. It is mandatory to user super keyword when the subclass non static member names and super class non static member names one same System.out.println(this.y); Class Demo System.out.println(super.x); { System.out.println(super.y); } int x=10; } int y=20; class Demo2 extends Demo { } int x=111; class Demo2 extends Demo int y=222; Public void Print() { { int x=1; int x=100; EXAMPLE int y=200; int y=2; System.out.println(x); System.out.println(y); Public void Display() System.out.println(this.x); { System.out.println(this.y); System.out.println(super.x); int x=11; System.out.println(super.y);}} int y=12; Class Main class { System.out.println(x); Demo3 d3= new Demo3(); System.out.println(y); d3.Print(); d3.Display(); System.out.println(this.x); }