SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados (20)

Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Generics
GenericsGenerics
Generics
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Data types in java
Data types in javaData types in java
Data types in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
 
Files in java
Files in javaFiles in java
Files in java
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 

Destaque

Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keywordtanu_jaswal
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword pptVinod Kumar
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23myrajendra
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Selenium ide material (2)
Selenium ide material (2)Selenium ide material (2)
Selenium ide material (2)Sriram Angajala
 
Keyword of java
Keyword of javaKeyword of java
Keyword of javaJani Harsh
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34myrajendra
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
Performance management system slide
Performance management system slidePerformance management system slide
Performance management system slideraizanamiza
 

Destaque (20)

Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
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
 
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
 
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
 

Semelhante a Java keywords

Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
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 & AppletsHelen SagayaRaj
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructorsRavi_Kant_Sahu
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and MethodsNilesh Dalvi
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introductionteach4uin
 
This keyword and final keyword
This keyword and final  keywordThis keyword and final  keyword
This keyword and final keywordkinjalbirare
 
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 KuteTushar B Kute
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 

Semelhante a 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
 
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
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 

Mais de Ravi_Kant_Sahu

Mais 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
 

Último

What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Último (20)

What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

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)