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 (20)

Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java input
Java inputJava input
Java input
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Data types in java
Data types in javaData types in java
Data types in java
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Method overloading
Method overloadingMethod overloading
Method overloading
 

En vedette (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.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
 
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
 
Java exception
Java exception Java exception
Java exception
 

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 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
 

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

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

Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 

Dernier (20)

Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 

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)