SlideShare a Scribd company logo
PRESENTED BY:
ANSHU SHARMA
Object Oriented Using Java -
Super and Final Keyword
SUPER KEYWORD IN JAVA
 The super keyword in Java is a reference variable
which is used to refer immediate parent class object.
 Whenever you create the instance of subclass, an
instance of parent class is created implicitly which is
referred by super reference variable.
 It is used to call superclass methods, and to access
the superclass constructor.
 The most common use of the super keyword is to
eliminate the confusion between superclasses and
subclasses that have methods with the same name.
USE OF SUPER KEYWORD
 super can be used to refer immediate parent class instance
variable.
 super can be used to invoke immediate parent class
method.
 super() can be used to invoke immediate parent class
constructor.
 super () calls the parent class constructor with no
argument.
 super.methodname calls method from parents class.
 It is used to call the parents class variable.
USE OF SUPER KEYWORD
EXAMPLES: 1) SUPER IS USED TO REFER
IMMEDIATE PARENT CLASS INSTANCE VARIABLE.
 We can use super keyword to access the data member or field of
parent class. It is used if parent class and child class have same
fields.
class vehicle{
String color="white";
}
class car extends vehicle{
String color="black";
void printColor(){
System.out.println(color);//prints color of car class
System.out.println(super.color);//prints color of vehicle class
}
}
EXAMPLE CONTD.
class TestSuper1{
public static void main(String args[]){
car c=new car();
c.printColor();
In the above example, vehicle and car both classes
have a common property color. If we print color
property, it will print the color of current class by
default. To access the parent property, we need to
use super keyword.
2) SUPER CAN BE USED TO
INVOKE PARENT CLASS METHOD
 The super keyword can also be used to invoke parent class
method. It should be used if subclass contains the same method
as parent class. In other words, it is used if method is overridden.
class person{
void eat(){System.out.println("eating...");}
}
class student extends person{
void eat(){System.out.println("eating ...");}
void read(){System.out.println(“Reading...");}
void work(){
super.eat();
read();
}
}
EXAMPLE CONTD.
class TestSuper2{
public static void main(String args[]){
student d=new student();
d.work();
}}
In the above example person and student both classes
have eat() method if we call eat() method from
student class, it will call the eat() method of student
class by default because priority is given to local.
3) SUPER IS USED TO INVOKE
PARENT CLASS CONSTRUCTOR.
 The super keyword can also be used to invoke the parent class
constructor. Let's see a simple example:
class person{
person()
{
System.out.println(“person class");}
}
class student extends person{
student()
{
super();
System.out.println(“student class");
}
}
EXAMPLE CONTD.
class TestSuper3{
public static void main(String args[]){
student d=new student();
}}
Output:
person class
Student class
As we know well that default constructor is provided by compiler
automatically if there is no constructor. But, it also adds super() as the
first statement.
FINAL KEYWORD IN JAVA
 The final keyword in java is used to restrict the user. The
java final keyword can be used in many context. Final can
be:
• A variable
• A method
• A class
 The final keyword can be applied with the variables, a final
variable that have no value it is called blank final variable or
uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also
which will be initialized in the static block only.
FINAL KEYWORD IN JAVA
1) JAVA FINAL VARIABLE
 If you make any variable as final, you cannot
change the value of final variable(It will be
constant).
Example of final variable
 There is a final variable speedlimit, we are going to
change the value of this variable, but It can't be
changed because final variable once assigned a
value can never be changed.
EXAMPLE:
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
Output:
Compile Time Error
2) JAVA FINAL METHOD
 If you make any method as final, you cannot override it.
Example of final method
class Bike{
final void run()
{
System.out.println("running");}
}
class Honda extends Bike{
void run()
{
System.out.println("running safely with 100kmph");
}
EXAMPLE CONTD.
public static void main(String args[])
{
Honda honda= new Honda();
honda.run();
}
}
Output:
Compile Time Error
3) JAVA FINAL CLASS
If you make any class as final, you cannot extend it.
Example of final class
final class Bike{}
class Honda1 extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
}
Output:
Compile Time Error
Thankyou

More Related Content

What's hot (20)

Java Collections
Java  Collections Java  Collections
Java Collections
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Inheritance
InheritanceInheritance
Inheritance
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Final keyword
Final keywordFinal keyword
Final keyword
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Constructor in java
Constructor in javaConstructor in java
Constructor 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
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Java package
Java packageJava package
Java package
 
Java threads
Java threadsJava threads
Java threads
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 

Similar to Super and final in java

OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdfvenud11
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 eehomeworkping9
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.pptParikhitGhosh1
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphismTOPS Technologies
 
BCA Super Keyword.pptx
BCA Super Keyword.pptxBCA Super Keyword.pptx
BCA Super Keyword.pptxsarthakgithub
 
super keyword.pptx
super keyword.pptxsuper keyword.pptx
super keyword.pptxAdarshADS4
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class featuresRakesh Madugula
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdfParvizMirzayev2
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 

Similar to Super and final in java (20)

Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Java- language Lecture 6
Java- language Lecture 6Java- language Lecture 6
Java- language Lecture 6
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphism
 
BCA Super Keyword.pptx
BCA Super Keyword.pptxBCA Super Keyword.pptx
BCA Super Keyword.pptx
 
super keyword.pptx
super keyword.pptxsuper keyword.pptx
super keyword.pptx
 
This and Static Keyword
This and Static KeywordThis and Static Keyword
This and Static Keyword
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 

More from anshu_atri

Types of input and output devices
Types of input and output devicesTypes of input and output devices
Types of input and output devicesanshu_atri
 
Types of computers
Types of computersTypes of computers
Types of computersanshu_atri
 
Intro of computers
Intro of computersIntro of computers
Intro of computersanshu_atri
 
Observer pattern
Observer patternObserver pattern
Observer patternanshu_atri
 
Examination process (1)
Examination process (1)Examination process (1)
Examination process (1)anshu_atri
 
Iterator pattern
Iterator patternIterator pattern
Iterator patternanshu_atri
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design patternanshu_atri
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro anshu_atri
 
Generic programming in java
Generic programming in javaGeneric programming in java
Generic programming in javaanshu_atri
 
Acem web designing
Acem web designingAcem web designing
Acem web designinganshu_atri
 
Aravali college of engg. and management
Aravali college of engg. and managementAravali college of engg. and management
Aravali college of engg. and managementanshu_atri
 

More from anshu_atri (11)

Types of input and output devices
Types of input and output devicesTypes of input and output devices
Types of input and output devices
 
Types of computers
Types of computersTypes of computers
Types of computers
 
Intro of computers
Intro of computersIntro of computers
Intro of computers
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Examination process (1)
Examination process (1)Examination process (1)
Examination process (1)
 
Iterator pattern
Iterator patternIterator pattern
Iterator pattern
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro
 
Generic programming in java
Generic programming in javaGeneric programming in java
Generic programming in java
 
Acem web designing
Acem web designingAcem web designing
Acem web designing
 
Aravali college of engg. and management
Aravali college of engg. and managementAravali college of engg. and management
Aravali college of engg. and management
 

Recently uploaded

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticspragatimahajan3
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...sanghavirahi2
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryEugene Lysak
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointELaRue0
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxbennyroshan06
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 

Recently uploaded (20)

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 

Super and final in java

  • 1. PRESENTED BY: ANSHU SHARMA Object Oriented Using Java - Super and Final Keyword
  • 2. SUPER KEYWORD IN JAVA  The super keyword in Java is a reference variable which is used to refer immediate parent class object.  Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.  It is used to call superclass methods, and to access the superclass constructor.  The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
  • 3. USE OF SUPER KEYWORD  super can be used to refer immediate parent class instance variable.  super can be used to invoke immediate parent class method.  super() can be used to invoke immediate parent class constructor.  super () calls the parent class constructor with no argument.  super.methodname calls method from parents class.  It is used to call the parents class variable.
  • 4. USE OF SUPER KEYWORD
  • 5. EXAMPLES: 1) SUPER IS USED TO REFER IMMEDIATE PARENT CLASS INSTANCE VARIABLE.  We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields. class vehicle{ String color="white"; } class car extends vehicle{ String color="black"; void printColor(){ System.out.println(color);//prints color of car class System.out.println(super.color);//prints color of vehicle class } }
  • 6. EXAMPLE CONTD. class TestSuper1{ public static void main(String args[]){ car c=new car(); c.printColor(); In the above example, vehicle and car both classes have a common property color. If we print color property, it will print the color of current class by default. To access the parent property, we need to use super keyword.
  • 7. 2) SUPER CAN BE USED TO INVOKE PARENT CLASS METHOD  The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden. class person{ void eat(){System.out.println("eating...");} } class student extends person{ void eat(){System.out.println("eating ...");} void read(){System.out.println(“Reading...");} void work(){ super.eat(); read(); } }
  • 8. EXAMPLE CONTD. class TestSuper2{ public static void main(String args[]){ student d=new student(); d.work(); }} In the above example person and student both classes have eat() method if we call eat() method from student class, it will call the eat() method of student class by default because priority is given to local.
  • 9. 3) SUPER IS USED TO INVOKE PARENT CLASS CONSTRUCTOR.  The super keyword can also be used to invoke the parent class constructor. Let's see a simple example: class person{ person() { System.out.println(“person class");} } class student extends person{ student() { super(); System.out.println(“student class"); } }
  • 10. EXAMPLE CONTD. class TestSuper3{ public static void main(String args[]){ student d=new student(); }} Output: person class Student class As we know well that default constructor is provided by compiler automatically if there is no constructor. But, it also adds super() as the first statement.
  • 11. FINAL KEYWORD IN JAVA  The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: • A variable • A method • A class  The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.
  • 13. 1) JAVA FINAL VARIABLE  If you make any variable as final, you cannot change the value of final variable(It will be constant). Example of final variable  There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.
  • 14. EXAMPLE: class Bike9{ final int speedlimit=90;//final variable void run(){ speedlimit=400; } public static void main(String args[]){ Bike9 obj=new Bike9(); obj.run(); } }//end of class Output: Compile Time Error
  • 15. 2) JAVA FINAL METHOD  If you make any method as final, you cannot override it. Example of final method class Bike{ final void run() { System.out.println("running");} } class Honda extends Bike{ void run() { System.out.println("running safely with 100kmph"); }
  • 16. EXAMPLE CONTD. public static void main(String args[]) { Honda honda= new Honda(); honda.run(); } } Output: Compile Time Error
  • 17. 3) JAVA FINAL CLASS If you make any class as final, you cannot extend it. Example of final class final class Bike{} class Honda1 extends Bike{ void run(){System.out.println("running safely with 100kmph");} public static void main(String args[]){ Honda1 honda= new Honda1(); honda.run(); } } Output: Compile Time Error