SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler offline
Programming in Java
Lecture 8: 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)
Keywords and classes

Mais conteúdo relacionado

Mais procurados

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
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
Ravi Kant Sahu
 

Mais procurados (19)

Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Packages
PackagesPackages
Packages
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
 
Oops Concept Java
Oops Concept JavaOops Concept Java
Oops Concept Java
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interface
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using Java
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 

Destaque (11)

Exception handling
Exception handlingException handling
Exception handling
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Event handling
Event handlingEvent handling
Event handling
 
Packages
PackagesPackages
Packages
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Operators in java
Operators in javaOperators in java
Operators in java
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Applets
AppletsApplets
Applets
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 

Semelhante a Keywords and classes

String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi_Kant_Sahu
 

Semelhante a Keywords and classes (20)

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
 
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
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
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
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
This keyword and final keyword
This keyword and final  keywordThis keyword and final  keyword
This keyword and final keyword
 
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
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
BCA Super Keyword.pptx
BCA Super Keyword.pptxBCA Super Keyword.pptx
BCA Super Keyword.pptx
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 

Mais de Ravi_Kant_Sahu (7)

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
 
Event handling
Event handlingEvent handling
Event handling
 
List classes
List classesList classes
List classes
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Jdbc
JdbcJdbc
Jdbc
 
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
 

Último

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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

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?
 
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...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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
 

Keywords and classes

  • 1. Programming in Java Lecture 8: 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)