Vous êtes sur la page 1sur 17

A Presentation on Method Overloading

and Method Overriding

Presented By
Md. Rayhanul Islam
Institute of Information Technology
University of Dhaka

6/12/2017 1
Outline
What is Method?
Parts of Method
Method Overloading
Example of Method Overloading
Method Overriding
Example of Method Overriding
Rules for Method Overriding

6/12/2017 2
What is Method?
A collection of statements that are grouped together to perform an operation
A method has the following syntax:

modifier return_Value_Type Method_Name(list of parameters){


// Method body;
}

6/12/2017 3
Parts of Method
1. Modifiers:
Tells the compiler how to call the method
Defines the access type of the method

2. Return Types
The data type of the value the method returns
The return type void when no value is returned

6/12/2017 4
Parts of Method (Cont)
Method Name
The actual name of the method
Method name and the parameter list together constitute the method signature

Parameters
When a method is invoked, a value is passed to parameter
This value is referred to as actual parameter or argument
The parameter list refers to the type, order, and number of the parameters

6/12/2017 5
Parts of Method (Cont)
Method Body
The method body contains a collection of statements that define what the method does

6/12/2017 6
Method Overloading
Method with same name but different behavior
Method with same but different method signature
Overloaded methods may or may not have different return type but it should have different
argument(s)
int test (){ }
int test(int a){ }
int test(double a){ }
int test (double a, int a){ }

6/12/2017 7
Code Example
public class MethodOverloading {
// Method 1
public int Addition(int a, int b) {
System.out.println(Method 1 is called);
return a + b;
}
// Method 2
public int Addition(int a, int b, int c) {
System.out.println(Method 2 is called);
return a + b + c;
}
}

6/12/2017 8
Code Example (Cont)
public static void main( String[]args ) {
int Answer1 = Addition(5, 6);
// In this case Method 1 will be called
System.out.println(Answer 1 = + Answer1);
System.out.println(----------------------------);
int Answer2 = Addition(5, 6, 7);
// In this case Method 2 will be called
System.out.println(Answer 2 = + Answer2);
}

6/12/2017 9
Output
Output of the code
Method 1 is called
Answer 1 = 11
----------------------------
Method 2 is called
Answer 2 = 18

6/12/2017 10
Method Overriding
The child class provides alternative implementation for parent class method.
The key benefit of overriding is the ability to define behavior that's specific to a particular
subclass type.
Overridden method: In the superclass.
Overriding method: In the subclass.

6/12/2017 11
Example of Method Overriding
public class Car {
public void maxSpeed(){
System.out.println("Max speed is 60 mph");
}
}
class Ferrari extends Car{
public void maxSpeed() {
System.out.println("Max speed is 120 mph");
}
public void msc(){}
}

6/12/2017 12
Method Overriding
public class TestCar
{
public static void main(String args[]){
Ferrari f=new Ferrari();
f.maxSpeed();
}
OR
public static void main(String args[]){
Car f=new Ferrari();
f.maxSpeed();
}
}

6/12/2017 13
Rules for Method Overriding
EXACT argument list matching
public class Car{
public void maxSpeed(int speed){
System.out.println("Max speed is +speed+ mph");
}
}
class Ferrari extends Car{
public void maxSpeed(float speed){
System.out.println("Max speed is +speed+ mph");
}
public void msc(){}
}

6/12/2017 14
Rules for Method Overriding
The return type must be the same as, or subtype of the return type declared in the parent class
class Alpha {
Alpha doStuff(char c) {
return new Alpha();
}
}
class Beta extends Alpha {
Beta doStuff(char c) {
return new Beta();
}
}

6/12/2017 15
Rules for Method Overriding
Possible only through Inheritance
public class Car {
protected void maxSpeed(){
System.out.println("Max speed is 60 mph");
}
}
class Ferrari {
public void maxSpeed(){
System.out.println("Max speed is 120 mph");
}
public void msc(){}
}

6/12/2017 16
Thank you

Although these two terms sound similar but distinct in meaning.


For more information:
The Complete Reference by Herbert Schildt
Starting out with C++ by Tony Gaddis

6/12/2017 17

Vous aimerez peut-être aussi