SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
Programming in Java
Lecture 9: This, Super & Final Keywords
By
Ravi Kant Sahu
Asst. Professor, LPU
Contents
• Object class
• super keyword
• final keyword
• final class
• static keyword
• this keyword
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Object Class
 There is one special class, called Object, defined by Java.
 All other classes are subclasses of Object.
 A reference variable of type Object can refer to an object of any other
class.
 Arrays are implemented as classes, so a variable of type Object can also
refer to any array.
 In Object class, getClass( ), notify( ), notifyAll( ), and wait( ) are
declared as final.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods in Object class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
‘final’ Keyword
• ‘final’ keyword is used to:
– declare variables that can be assigned value only once.
final type identifier = expression;
– prevent overriding of a method.
final return_type methodName (arguments if any)
{
body;
}
– prevent inheritance from a class.
final class Class_Name
{
class body;
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
‘super’ Keyword
• ‘super’ keyword is used to:
– invoke the super-class constructor from the constructor
of a sub-class.
super (arguments if any);
– invoke the method of super-class on current object
from sub-class.
super.methodName (arguments if any);
– refer the super-class data member in case of name-
conflict between super and sub-class data members.
super.memberName;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
‘static’ Keyword
• used to represent class members
• Variables can be declared with the “static” keyword.
static int y = 0;
• When a variable is declared with the keyword “static”, its called
a “class variable”.
• All instances share the same copy of the variable.
• A class variable can be accessed directly with the class, without
the need to create a instance.
• Note: There's no such thing as static classs. “static” in front of
class creates compilation error.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
static methods
• Methods can also be declared with the keyword “static”.
• When a method is declared static, it can be used without creating an
object.
class T2 {
static int triple (int n)
{return 3*n;}
}
class T1 {
public static void main(String[] arg)
{
System.out.println( T2.triple(4) );
T2 x1 = new T2();
System.out.println( x1.triple(5) );
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• Methods declared with “static” keyword are called “class
methods”.
• Otherwise they are “instance methods”.
• Static Methods Cannot Access Non-Static Variables.
• The following gives a compilation error, unless x is also static.
class T2 {
int x = 3;
static int returnIt () { return x;}
}
class T1 {
public static void main(String[] arg) {
System.out.println( T2.returnIt() ); }
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
‘this’ Keyword
• 'this' is used for pointing the current class instance.
• Within an instance method or a constructor, this is a reference to
the current object — the object whose method or constructor is
being called.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
class ThisDemo1{
int a = 0;
int b = 0;
ThisDemo1(int x, int y)
{
this.a = x;
this.b = y;
}
public static void main(String [] args)
{
ThisDemo1 td = new ThisDemo1(10,12);
ThisDemo1 td1 = new ThisDemo1(100,23);
System.out.println(td.a);
System.out.println(td.B);
System.out.println(td1.a);
System.out.println(td1.B);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Chaining of constructors using this keyword
• Chaining of constructor means calling one constructor
from other constructor.
• We can invoke the constructor of same class using
‘this()’ keyword.
• We can invoke the super class constructor from
subclass constructor using ‘super ()’
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
class ThisDemo{
public ThisDemo() {
this(10); System.out.println("First Constructor");
}
public ThisDemo(int a) // overloaded constructor
{
this(10,20); System.out.println("Second Constructor");
}
public ThisDemo( int a, int B) // another overloaded constructor
{
this(“Ravi Kant"); System.out.println("Third Constructor");
}
public ThisDemo(String s) // and still another
{
System.out.println("Fourth Constructor");
}
public static void main(String args[]) {
ThisDemo first = new ThisDemo();
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java keywords

Contenu connexe

Tendances

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 

Tendances (20)

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java package
Java packageJava package
Java package
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Class and object
Class and objectClass and object
Class and object
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Java IO
Java IOJava IO
Java IO
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
JAVA PROGRAMMING
JAVA PROGRAMMING JAVA PROGRAMMING
JAVA PROGRAMMING
 
OOP java
OOP javaOOP java
OOP java
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 

En vedette

Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
myrajendra
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
myrajendra
 
Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 

En vedette (20)

Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Final keyword
Final keywordFinal keyword
Final keyword
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Selenium ide material (2)
Selenium ide material (2)Selenium ide material (2)
Selenium ide material (2)
 
Overriding methods
Overriding methodsOverriding methods
Overriding methods
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
Java
Java Java
Java
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Fundamentals
FundamentalsFundamentals
Fundamentals
 
Exception handling
Exception handlingException handling
Exception handling
 
Performance management system slide
Performance management system slidePerformance management system slide
Performance management system slide
 
William Bronchick Coaching
William Bronchick CoachingWilliam Bronchick Coaching
William Bronchick Coaching
 

Similaire à Java keywords

Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
Ravi_Kant_Sahu
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 

Similaire à Java keywords (20)

Inheritance
InheritanceInheritance
Inheritance
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Generics
GenericsGenerics
Generics
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Java beans
Java beansJava beans
Java beans
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
 
Hemajava
HemajavaHemajava
Hemajava
 
This keyword and final keyword
This keyword and final  keywordThis keyword and final  keyword
This keyword and final keyword
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 

Plus de Ravi_Kant_Sahu (16)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
Event handling
Event handlingEvent handling
Event handling
 
Basic IO
Basic IOBasic IO
Basic IO
 
List classes
List classesList classes
List classes
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Packages
PackagesPackages
Packages
 
Array
ArrayArray
Array
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Jdbc
JdbcJdbc
Jdbc
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Swing api
Swing apiSwing api
Swing api
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Java keywords

  • 1. Programming in Java Lecture 9: This, Super & Final Keywords By Ravi Kant Sahu Asst. Professor, LPU
  • 2. Contents • Object class • super keyword • final keyword • final class • static keyword • this keyword Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Object Class  There is one special class, called Object, defined by Java.  All other classes are subclasses of Object.  A reference variable of type Object can refer to an object of any other class.  Arrays are implemented as classes, so a variable of type Object can also refer to any array.  In Object class, getClass( ), notify( ), notifyAll( ), and wait( ) are declared as final. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. Methods in Object class Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. ‘final’ Keyword • ‘final’ keyword is used to: – declare variables that can be assigned value only once. final type identifier = expression; – prevent overriding of a method. final return_type methodName (arguments if any) { body; } – prevent inheritance from a class. final class Class_Name { class body; } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. ‘super’ Keyword • ‘super’ keyword is used to: – invoke the super-class constructor from the constructor of a sub-class. super (arguments if any); – invoke the method of super-class on current object from sub-class. super.methodName (arguments if any); – refer the super-class data member in case of name- conflict between super and sub-class data members. super.memberName; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. ‘static’ Keyword • used to represent class members • Variables can be declared with the “static” keyword. static int y = 0; • When a variable is declared with the keyword “static”, its called a “class variable”. • All instances share the same copy of the variable. • A class variable can be accessed directly with the class, without the need to create a instance. • Note: There's no such thing as static classs. “static” in front of class creates compilation error. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. static methods • Methods can also be declared with the keyword “static”. • When a method is declared static, it can be used without creating an object. class T2 { static int triple (int n) {return 3*n;} } class T1 { public static void main(String[] arg) { System.out.println( T2.triple(4) ); T2 x1 = new T2(); System.out.println( x1.triple(5) ); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. • Methods declared with “static” keyword are called “class methods”. • Otherwise they are “instance methods”. • Static Methods Cannot Access Non-Static Variables. • The following gives a compilation error, unless x is also static. class T2 { int x = 3; static int returnIt () { return x;} } class T1 { public static void main(String[] arg) { System.out.println( T2.returnIt() ); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. ‘this’ Keyword • 'this' is used for pointing the current class instance. • Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. class ThisDemo1{ int a = 0; int b = 0; ThisDemo1(int x, int y) { this.a = x; this.b = y; } public static void main(String [] args) { ThisDemo1 td = new ThisDemo1(10,12); ThisDemo1 td1 = new ThisDemo1(100,23); System.out.println(td.a); System.out.println(td.B); System.out.println(td1.a); System.out.println(td1.B); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Chaining of constructors using this keyword • Chaining of constructor means calling one constructor from other constructor. • We can invoke the constructor of same class using ‘this()’ keyword. • We can invoke the super class constructor from subclass constructor using ‘super ()’ Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. class ThisDemo{ public ThisDemo() { this(10); System.out.println("First Constructor"); } public ThisDemo(int a) // overloaded constructor { this(10,20); System.out.println("Second Constructor"); } public ThisDemo( int a, int B) // another overloaded constructor { this(“Ravi Kant"); System.out.println("Third Constructor"); } public ThisDemo(String s) // and still another { System.out.println("Fourth Constructor"); } public static void main(String args[]) { ThisDemo first = new ThisDemo(); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)