SlideShare a Scribd company logo
Method overloading
&
final keyword
LECTURE 5
Method Overloading in Java
 If a class have multiple methods by same name but different
parameters, it is known as Method Overloading.
 Suppose you have to perform addition of the given numbers but there
can be any number of arguments, if you write the method such as
a(int,int) for two parameters, and b(int,int,int) for three parameters then
it may be difficult for you as well as other programmers to understand
the behaviour of the method because its name differs. So, we perform
method overloading to figure out the program quickly.
 Different ways to Overload a method
 By changing number of arguments
 By changing the data type
Note: In java, Method Overloading is
not possible by changing the return
type of the method.
1.Method Overloading by changing the
no. of arguments
1. class Calculation{
2. void sum(int a,int b)
3. { System.out.println(a+b); }
4. void sum(int a,int b,int c)
5. { System.out.println(a+b+c); }
6.
7. public static void main(String args[ ]){
8. Calculation obj=new Calculation();
9. obj.sum(10,10,10);
10. obj.sum(20,20);
11. }
12. }
2. Method Overloading by changing
data type of argument
1. class Calculation2{
2. void sum(int a,int b)
3. { System.out.println(a+b); }
4. void sum(double a,double b)
5. { System.out.println(a+b); }
6.
7. public static void main(String args[]){
8. Calculation2 obj=new Calculation2();
9. obj.sum(10.5,10.5);
10. obj.sum(20,20);
11. }
12. }
Method Overloading and Type Promotion
Method Overloading and Type Promotion
1. class OverloadingCalculation1{
2. void sum(int a,long b)
3. { System.out.println(a+b); }
4. void sum(int a,int b,int c)
5. { System.out.println(a+b+c); }
6.
7. public static void main(String args[]){
8. OverloadingCalculation1 obj=new OverloadingCalculation1();
9. obj.sum(20,20); //now second int literal will be promoted to long
10. obj.sum(20,20,20);
11.
12. }
13. }
Note: One type is not de-promoted
implicitly for example double cannot
be de-promoted to any type
implicitly.
Difference between
Method Overloading
and
Method Overriding
S# Method Overloading Method Overriding
1 Method overloading is used to
increase the readability of the
program
Method overriding is used to provide the
specific implementation of the method
that is already provided by its super class.
2 Method overloading is performed
within class.
Method overriding occurs in two classes
that have IS-A (inheritance) relationship.
3 In case of method overloading,
parameter must be different.
In case of method overriding, parameter
must be same.
4 Method overloading is the example
of compile time polymorphism.
Method overriding is the example of run
time polymorphism.
5 In java, method overloading can't be
performed by changing return type of
the method only. Return type can be
same or different in method
overloading. But you must have to
change the parameter.
Return type must be same or covariant in
method overriding.
Final Keyword
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:
 variable
 method
 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.
Final Variable
 If you make any variable as final, you cannot change the
value of final variable(It will be constant).
 Example
1. class Bike9{
2. final int speedlimit=90; //final variable
3. void run( ){
4. speedlimit=400; //Error. Cannot change final Variable
5. }
6. public static void main(String args[]){
7. Bike9 obj=new Bike9( );
8. obj.run( );
9. }
10. }
Final Method
 If you make any method as final, you cannot override it.
 Example
1. class Bike{
2. final void run( ){ System.out.println("running"); }
3. }
4. class Honda extends Bike{
5. void run( ) //ERROR: Final method cannot be overridden
6. { System.out.println("running safely with 100kmph"); }
7.
8. public static void main(String args[ ]){
9. Honda honda= new Honda();
10. honda.run( );
11. }
12. }
Final Class
 If you make any class as final, you cannot extend it.
 Example
1. final class Bike{
2. Bike( ) { System.out.println(“Bike Class”); }
3. }
4. class Honda1 extends Bike{ //ERROR: Cannot extend final Class
5. void run( ){ System.out.println("running safely with 100kmph"); }
6. public static void main(String args[ ]){
7. Honda1 honda= new Honda( );
8. honda.run();
9. }
10. }
Inheritance of Final Methods
 Final Method is inherited but final method cannot be overridden.
 Example
1. class Bike{
2. final void run( ){ System.out.println("running..."); }
3. }
4. class Honda2 extends Bike{
5. public static void main(String args[ ]){
6. new Honda2().run();
7. }
8. }
Blank or uninitialized final variables
 A final variable that is not initialized
at the time of declaration is known
as blank final variable.
 If you want to create a variable
that is initialized at the time of
creating object and once
initialized may not be changed, it
is useful. For example Reg# in
Student Class.
 It can be initialized only in
constructor
1. public class Student {
2. private final int reg_no;
3. private String name;
4. private String address;
5. public Student(int r, String n, String a){
6. reg_no = r;
7. name = n;
8. address = a; }
9. public static void main(String[ ] args) {
10. new Student(111, "Omer", "Rwp");
11. new Student(222, "Hamza", "Rwp");
12. }
13. }
Final parameter
 If you declare any parameter as final, you cannot change the value of it.
 Example
1. class Bike{
2. int cube(final int n){
3. n=n+2; //can't be changed as n is final
4. n*n*n;
5. }
6. public static void main(String args[]){
7. Bike b=new Bike();
8. b.cube(5);
9. }
10. }

More Related Content

PPTX
Polymorphism in java
PPTX
Basics to java programming and concepts of java
PDF
polymorphismpresentation-160825122725.pdf
PPTX
Polymorphism presentation in java
PDF
Unit 2 Part 1 POLYMORPHISM.pdf
PPTX
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
PPTX
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
PPTX
Polymorphism in java
Polymorphism in java
Basics to java programming and concepts of java
polymorphismpresentation-160825122725.pdf
Polymorphism presentation in java
Unit 2 Part 1 POLYMORPHISM.pdf
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
Polymorphism in java

Similar to Lecture_5_Method_overloading_Final.pptx in Java (20)

PDF
CIS 1403 lab 3 functions and methods in Java
PDF
Java Polymorphism: Types And Examples (Geekster)
DOCX
The Java Learning Kit Chapter 5 – Methods and Modular.docx
PDF
Learn java lessons_online
DOCX
Core java notes with examples
PPT
oops with java modules i & ii.ppt
PPTX
polymorphism method overloading and overriding .pptx
PPTX
Introduction of Object Oriented Programming Language using Java. .pptx
PDF
11.Object Oriented Programming.pdf
PDF
Web Technology-Method .pdf
PPT
Introduction to method overloading & method overriding in java hdm
PPTX
Inheritance
PPTX
inheritance and interface in oops with java .pptx
PPTX
Oop concepts classes Method Overriding.pptx
PDF
Exception handling and packages.pdf
PPT
Super and final in java
PDF
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
PPTX
Basics of Java
CIS 1403 lab 3 functions and methods in Java
Java Polymorphism: Types And Examples (Geekster)
The Java Learning Kit Chapter 5 – Methods and Modular.docx
Learn java lessons_online
Core java notes with examples
oops with java modules i & ii.ppt
polymorphism method overloading and overriding .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
11.Object Oriented Programming.pdf
Web Technology-Method .pdf
Introduction to method overloading & method overriding in java hdm
Inheritance
inheritance and interface in oops with java .pptx
Oop concepts classes Method Overriding.pptx
Exception handling and packages.pdf
Super and final in java
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
Basics of Java
Ad

More from ssuser5d6130 (7)

PPT
packages in java programming language ppt
PPT
Arrays in java programming language slides
PPT
Arrays in Java Programming Language slides
PPT
Arrays in Java Programming Language slides
PPT
Inner classes or nested classes in Java Program
PPT
Control statements in Java Programming Language
PPTX
API-led.pptx
packages in java programming language ppt
Arrays in java programming language slides
Arrays in Java Programming Language slides
Arrays in Java Programming Language slides
Inner classes or nested classes in Java Program
Control statements in Java Programming Language
API-led.pptx
Ad

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
Onica Farming 24rsclub profitable farm business
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
From loneliness to social connection charting
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Pharma ospi slides which help in ospi learning
PDF
Open folder Downloads.pdf yes yes ges yes
Insiders guide to clinical Medicine.pdf
Week 4 Term 3 Study Techniques revisited.pptx
How to Manage Starshipit in Odoo 18 - Odoo Slides
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Cardiovascular Pharmacology for pharmacy students.pptx
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Onica Farming 24rsclub profitable farm business
Module 3: Health Systems Tutorial Slides S2 2025
human mycosis Human fungal infections are called human mycosis..pptx
UPPER GASTRO INTESTINAL DISORDER.docx
O5-L3 Freight Transport Ops (International) V1.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
From loneliness to social connection charting
The Final Stretch: How to Release a Game and Not Die in the Process.
Pharma ospi slides which help in ospi learning
Open folder Downloads.pdf yes yes ges yes

Lecture_5_Method_overloading_Final.pptx in Java

  • 2. Method Overloading in Java  If a class have multiple methods by same name but different parameters, it is known as Method Overloading.  Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behaviour of the method because its name differs. So, we perform method overloading to figure out the program quickly.  Different ways to Overload a method  By changing number of arguments  By changing the data type Note: In java, Method Overloading is not possible by changing the return type of the method.
  • 3. 1.Method Overloading by changing the no. of arguments 1. class Calculation{ 2. void sum(int a,int b) 3. { System.out.println(a+b); } 4. void sum(int a,int b,int c) 5. { System.out.println(a+b+c); } 6. 7. public static void main(String args[ ]){ 8. Calculation obj=new Calculation(); 9. obj.sum(10,10,10); 10. obj.sum(20,20); 11. } 12. }
  • 4. 2. Method Overloading by changing data type of argument 1. class Calculation2{ 2. void sum(int a,int b) 3. { System.out.println(a+b); } 4. void sum(double a,double b) 5. { System.out.println(a+b); } 6. 7. public static void main(String args[]){ 8. Calculation2 obj=new Calculation2(); 9. obj.sum(10.5,10.5); 10. obj.sum(20,20); 11. } 12. }
  • 5. Method Overloading and Type Promotion
  • 6. Method Overloading and Type Promotion 1. class OverloadingCalculation1{ 2. void sum(int a,long b) 3. { System.out.println(a+b); } 4. void sum(int a,int b,int c) 5. { System.out.println(a+b+c); } 6. 7. public static void main(String args[]){ 8. OverloadingCalculation1 obj=new OverloadingCalculation1(); 9. obj.sum(20,20); //now second int literal will be promoted to long 10. obj.sum(20,20,20); 11. 12. } 13. } Note: One type is not de-promoted implicitly for example double cannot be de-promoted to any type implicitly.
  • 8. S# Method Overloading Method Overriding 1 Method overloading is used to increase the readability of the program Method overriding is used to provide the specific implementation of the method that is already provided by its super class. 2 Method overloading is performed within class. Method overriding occurs in two classes that have IS-A (inheritance) relationship. 3 In case of method overloading, parameter must be different. In case of method overriding, parameter must be same. 4 Method overloading is the example of compile time polymorphism. Method overriding is the example of run time polymorphism. 5 In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter. Return type must be same or covariant in method overriding.
  • 10. 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:  variable  method  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.
  • 11. Final Variable  If you make any variable as final, you cannot change the value of final variable(It will be constant).  Example 1. class Bike9{ 2. final int speedlimit=90; //final variable 3. void run( ){ 4. speedlimit=400; //Error. Cannot change final Variable 5. } 6. public static void main(String args[]){ 7. Bike9 obj=new Bike9( ); 8. obj.run( ); 9. } 10. }
  • 12. Final Method  If you make any method as final, you cannot override it.  Example 1. class Bike{ 2. final void run( ){ System.out.println("running"); } 3. } 4. class Honda extends Bike{ 5. void run( ) //ERROR: Final method cannot be overridden 6. { System.out.println("running safely with 100kmph"); } 7. 8. public static void main(String args[ ]){ 9. Honda honda= new Honda(); 10. honda.run( ); 11. } 12. }
  • 13. Final Class  If you make any class as final, you cannot extend it.  Example 1. final class Bike{ 2. Bike( ) { System.out.println(“Bike Class”); } 3. } 4. class Honda1 extends Bike{ //ERROR: Cannot extend final Class 5. void run( ){ System.out.println("running safely with 100kmph"); } 6. public static void main(String args[ ]){ 7. Honda1 honda= new Honda( ); 8. honda.run(); 9. } 10. }
  • 14. Inheritance of Final Methods  Final Method is inherited but final method cannot be overridden.  Example 1. class Bike{ 2. final void run( ){ System.out.println("running..."); } 3. } 4. class Honda2 extends Bike{ 5. public static void main(String args[ ]){ 6. new Honda2().run(); 7. } 8. }
  • 15. Blank or uninitialized final variables  A final variable that is not initialized at the time of declaration is known as blank final variable.  If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed, it is useful. For example Reg# in Student Class.  It can be initialized only in constructor 1. public class Student { 2. private final int reg_no; 3. private String name; 4. private String address; 5. public Student(int r, String n, String a){ 6. reg_no = r; 7. name = n; 8. address = a; } 9. public static void main(String[ ] args) { 10. new Student(111, "Omer", "Rwp"); 11. new Student(222, "Hamza", "Rwp"); 12. } 13. }
  • 16. Final parameter  If you declare any parameter as final, you cannot change the value of it.  Example 1. class Bike{ 2. int cube(final int n){ 3. n=n+2; //can't be changed as n is final 4. n*n*n; 5. } 6. public static void main(String args[]){ 7. Bike b=new Bike(); 8. b.cube(5); 9. } 10. }