0% found this document useful (0 votes)
8 views20 pages

Lecture 7

Method overloading in Java allows multiple methods with the same name but different parameters, enhancing code readability. It can be achieved by changing the number or data types of arguments, but not by changing the return type alone. Method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass, supporting runtime polymorphism.

Uploaded by

marmasunmoon9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views20 pages

Lecture 7

Method overloading in Java allows multiple methods with the same name but different parameters, enhancing code readability. It can be achieved by changing the number or data types of arguments, but not by changing the return type alone. Method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass, supporting runtime polymorphism.

Uploaded by

marmasunmoon9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Method Overloading

• If a class has multiple methods having same name but different in


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 behavior of the method because its name differs.
• Method overloading increases the readability of the program
• Different ways to overload the method
• There are two ways to overload the method in java
1.By changing number of arguments
2.By changing the data type
In Java, Method Overloading is not possible by changing the return type of the
method only.
Method Overloading: changing no. of arguments

1.class Adder{
2.static int add(int a,int b){return a+b;}
3.static int add(int a,int b,int c)
{return a+b+c;}
4.}
5.class TestOverloading1{
6.public static void main(String[] args){
7.System.out.println(Adder.add(11,11));
8.System.out.println(Adder.add(11,11,11));
9.}}
Method Overloading: changing data type of arguments

1.class Adder{
2.static int add(int a, int b){return a+b;}
3.static double add(double a, double b)
{return a+b;}
4.}
5.class TestOverloading2{
6.public static void main(String[] args){
7.System.out.println(Adder.add(11,11));
8.System.out.println(Adder.add(12.3,12.6));
9.}}
Can we overload java main() method?
• Yes, by method overloading. You can have any number of
main methods in a class by method overloading. But JVM
calls main() method which receives string array as
arguments only.
1.class TestOverloading4{
2.public static void main(String[] args)
{System.out.println("main with String[]");}
3.public static void main(String args)
{System.out.println("main with String");}
4.public static void main()
{System.out.println("main without args");}
5.}
Method Overloading and Type Promotion
• One type is promoted to another implicitly if no matching
datatype is found.
Example of Method Overloading with TypePromotion

1.class OverloadingCalculation1{
2. void sum(int a,long b){System.out.println(a+b);}
3. void sum(int a,int b,int c)
{System.out.println(a+b+c);}
4.
5. public static void main(String args[]){
6. OverloadingCalculation1 obj=new OverloadingCalcu
lation1();
7. obj.sum(20,20);//
now second int literal will be promoted to long
8. obj.sum(20,20,20);
9.
10. }
11.}
• If there are matching type arguments in the method, type
promotion is not performed.
1.class OverloadingCalculation2{
2. void sum(int a,int b)
{System.out.println("int arg method invoked");}
3. void sum(long a,long b)
{System.out.println("long arg method invoked");}
4.
5. public static void main(String args[]){
6. OverloadingCalculation2 obj=new OverloadingCalc
ulation2();
7. obj.sum(20,20);//
now int arg sum() method gets invoked
8. }
9.}
ambiguity
• If there are no matching type arguments in the method, and
each method promotes similar number of arguments, there
will be ambiguity.
1.class OverloadingCalculation3{
2. void sum(int a,long b)
{System.out.println("a method invoked");}
3. void sum(long a,int b)
{System.out.println("b method invoked");}
4.
5. public static void main(String args[]){
6. OverloadingCalculation3 obj=new OverloadingCalcul
ation3();
7. obj.sum(20,20);//now ambiguity
8. }
9.}
Method Overriding
• If subclass (child class) has the same method as declared in
the parent class, it is known as method overriding in
Java.
• Usage of Java Method Overriding
1. Method overriding is used to provide the specific
implementation of a method which is already provided by
its superclass.
2. Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
1.The method must have the same name as in the parent
class
2.The method must have the same parameter as in the
parent class.
3.There must be an IS-A relationship (inheritance).
problem without method overriding
• Let's understand the problem that we may face in the
program if we don't use method overriding.
1.class Vehicle{
2. void run()
{System.out.println("Vehicle is running");}
3.}
4.//Creating a child class
5.class Bike extends Vehicle{
6. public static void main(String args[]){
7. //creating an instance of child class
8. Bike obj = new Bike();
9. //calling the method with child class instance
10. obj.run();
11. }
12.}
Example of method overriding
1.class Vehicle{
2. //defining a method
3. void run()
{System.out.println("Vehicle is running");}
4.}
5.//Creating a child class
6.class Bike2 extends Vehicle{
7. //
defining the same method as in the parent class
8. void run()
{System.out.println("Bike is running safely");}
9.
10. public static void main(String args[]){
11. Bike2 obj = new Bike2();//creating object
12. obj.run();//calling method
13. }
14.}
real example of Java Method Overriding
• Consider a scenario where Bank is a class that provides
functionality to get the rate of interest. However, the rate of
interest varies according to banks. For example, SBI, ICICI
and AXIS banks could provide 8%, 7%, and 9% rate of
interest.
1.class Bank{ 1.class Test2{
2.int getRateOfInterest() 2.public static void main(String args[]){
{return 0;} 3.SBI s=new SBI();
3.} 4.ICICI i=new ICICI();
4.//Creating child classes. 5.AXIS a=new AXIS();
5.class SBI extends Bank{ 6.System.out.println("SBI Rate of Interest: "+s.getRateO
6.int getRateOfInterest() fInterest());
{return 8;} 7.System.out.println("ICICI Rate of Interest: "+i.getRate
7.} OfInterest());
8. 8.System.out.println("AXIS Rate of Interest: "+a.getRate
9.class ICICI extends Bank{ OfInterest());
10.int getRateOfInterest() 9.}
{return 7;} 10.}
11.}
12.class AXIS extends Bank{
13.int getRateOfInterest()
{return 9;}
14.}
Can we override static method?
• No, a static method cannot be overridden. It can be proved
by runtime polymorphism, so we will learn it later.
• It is because the static method is bound with class whereas
instance method is bound with an object. Static belongs to
the class area, and an instance belongs to the heap area.
• Can we override java main method?
• No, because the main is a static method.
Covariant Return Type
• The covariant return type specifies that the return type may
vary in the same direction as the subclass.
1.class A{
2.A get(){return this;}
3.}
4.
5.class B1 extends A{
6.@Override
7.B1 get(){return this;}
8.void message()
{System.out.println("welcome to covariant return type");}
9.
10.public static void main(String args[]){
11.new B1().get().message();
12.}
13.}
1.class A3 extends A2
2.{ @Override
1.class A1
3. A1 foo()
2.{ A1 foo()
4. { return this; }
3. { return this; }
5. @Override
4. void print()
6. void print()
5. { System.out.println("Inside the cl
7. { System.out.println("Inside the class A3");
ass A1");
} }
6. }
8.public class CovariantExample
7.} // A2 is the child class of A1
9.{ // main method
8.class A2 extends A1
10. public static void main(String argvs[])
9.{ @Override
11. { A1 a1 = new A1();
10. A1 foo()
12. // this is ok
11. { return this;
13. a1.foo().print();
12. }
14. A2 a2 = new A2();
13. void print()
15. ((A2)a2.foo()).print();
14. { System.out.println("Inside the
16. A3 a3 = new A3();
class A2");
17. // doing the type casting
15. } }
18. ((A3)a3.foo()).print();
16.// A3 is the child class of A2
19. } }
• think of a situation where the hierarchical structure goes
down to 10 - 15 classes or even more, and in each class, the
method foo() has the same return type. That is enough to
give a nightmare to the reader and writer of the code.
1.class A2 extends A1
1.class A1
2.{
2.{
3. @Override
3. A1 foo()
4. A2 foo()
4. {
5. {
5. return this;
6. return this;
6. }
7. }
7. void print()
8. void print()
8. {
9. {
9. System.out.println("Inside the cl
10. System.out.println("Inside the cl
ass A1");
ass A2");
10. }
11. }
11.}
12.}
1.public class CovariantExample
1.class A3 extends A2
2.{
2.{
3. // main method
3. @Override
4. public static void main(String argvs[])
4. A3 foo()
5. {
5. {
6. A1 a1 = new A1();
6. return this;
7. a1.foo().print();
7. }
8. A2 a2 = new A2();
8.
9. a2.foo().print();
9. @Override
10. A3 a3 = new A3();
10. void print()
11.
11. {
12. a3.foo().print();
12. System.out.println("Inside the cl
13.
ass A3");
14. }
13. }
15.}
14.}
The End

You might also like