SlideShare a Scribd company logo
2
Most read
14
Most read
17
Most read
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)

PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPTX
Static keyword ppt
Vinod Kumar
 
PDF
Methods in Java
Jussi Pohjolainen
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PPTX
Inner classes in java
PhD Research Scholar
 
PPTX
Inheritance in java
RahulAnanda1
 
PPTX
Control structures in java
VINOTH R
 
PPTX
Virtual base class
Tech_MX
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
Access Modifier.pptx
Margaret Mary
 
PPTX
Multithreading in java
Raghu nath
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
PPTX
Java interface
BHUVIJAYAVELU
 
PPTX
Php string function
Ravi Bhadauria
 
PPT
Java Streams
M Vishnuvardhan Reddy
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Static keyword ppt
Vinod Kumar
 
Methods in Java
Jussi Pohjolainen
 
Inheritance in java
Tech_MX
 
PHP FUNCTIONS
Zeeshan Ahmed
 
Inner classes in java
PhD Research Scholar
 
Inheritance in java
RahulAnanda1
 
Control structures in java
VINOTH R
 
Virtual base class
Tech_MX
 
Type casting in java
Farooq Baloch
 
Access Modifier.pptx
Margaret Mary
 
Multithreading in java
Raghu nath
 
Classes objects in java
Madishetty Prathibha
 
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
Java interface
BHUVIJAYAVELU
 
Php string function
Ravi Bhadauria
 
Java Streams
M Vishnuvardhan Reddy
 

Similar to Super and final in java (20)

PPTX
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
PPTX
Basics to java programming and concepts of java
1747503gunavardhanre
 
PDF
Java inheritance
Hamid Ghorbani
 
PDF
Java Inheritance
Rosie Jane Enomar
 
PPTX
OCA Java SE 8 Exam Chapter 5 Class Design
İbrahim Kürce
 
DOCX
Core java by amit
Thakur Amit Tomer
 
PDF
Java- language Lecture 6
Hatem Abd El-Salam
 
PDF
java_inheritance.pdf
JayMistry91473
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PDF
1669617800196.pdf
venud11
 
PPTX
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
PPTX
Object oriented concepts
Gousalya Ramachandran
 
DOC
116824015 java-j2 ee
homeworkping9
 
PPT
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
PPT
InheritanceAndPolymorphismprein Java.ppt
gayatridwahane
 
PPSX
Learn java objects inheritance-overriding-polymorphism
TOPS Technologies
 
PPTX
BCA Super Keyword.pptx
sarthakgithub
 
PPTX
super keyword.pptx
AdarshADS4
 
PPTX
This and Static Keyword
Dhrumil Panchal
 
PPT
Md06 advance class features
Rakesh Madugula
 
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
Basics to java programming and concepts of java
1747503gunavardhanre
 
Java inheritance
Hamid Ghorbani
 
Java Inheritance
Rosie Jane Enomar
 
OCA Java SE 8 Exam Chapter 5 Class Design
İbrahim Kürce
 
Core java by amit
Thakur Amit Tomer
 
Java- language Lecture 6
Hatem Abd El-Salam
 
java_inheritance.pdf
JayMistry91473
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
1669617800196.pdf
venud11
 
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
Object oriented concepts
Gousalya Ramachandran
 
116824015 java-j2 ee
homeworkping9
 
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
InheritanceAndPolymorphismprein Java.ppt
gayatridwahane
 
Learn java objects inheritance-overriding-polymorphism
TOPS Technologies
 
BCA Super Keyword.pptx
sarthakgithub
 
super keyword.pptx
AdarshADS4
 
This and Static Keyword
Dhrumil Panchal
 
Md06 advance class features
Rakesh Madugula
 
Ad

More from anshu_atri (11)

PPTX
Types of input and output devices
anshu_atri
 
PPTX
Types of computers
anshu_atri
 
PPTX
Intro of computers
anshu_atri
 
PPTX
Observer pattern
anshu_atri
 
PPTX
Examination process (1)
anshu_atri
 
PPT
Iterator pattern
anshu_atri
 
PPTX
Singleton design pattern
anshu_atri
 
PPT
Oops design pattern intro
anshu_atri
 
PPT
Generic programming in java
anshu_atri
 
PPTX
Acem web designing
anshu_atri
 
PPTX
Aravali college of engg. and management
anshu_atri
 
Types of input and output devices
anshu_atri
 
Types of computers
anshu_atri
 
Intro of computers
anshu_atri
 
Observer pattern
anshu_atri
 
Examination process (1)
anshu_atri
 
Iterator pattern
anshu_atri
 
Singleton design pattern
anshu_atri
 
Oops design pattern intro
anshu_atri
 
Generic programming in java
anshu_atri
 
Acem web designing
anshu_atri
 
Aravali college of engg. and management
anshu_atri
 
Ad

Recently uploaded (20)

PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
PPTX
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
PPTX
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Basics and rules of probability with real-life uses
ravatkaran694
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
John Keats introduction and list of his important works
vatsalacpr
 

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