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

Lecture5_Method_overloading_Final power point presentation

  • 1.
    Method overloading & finalkeyword LECTURE 5
  • 2.
    Method Overloading inJava  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 bychanging 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 Overloadingby 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 andType Promotion
  • 6.
    Method Overloading andType 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.
  • 7.
  • 8.
    S# Method OverloadingMethod 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.
  • 9.
  • 10.
    Final Keyword InJava  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  Ifyou 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  Ifyou 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  Ifyou 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 FinalMethods  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 uninitializedfinal 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  Ifyou 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. }