Java/C2/Method-overloading/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Method Overloading

Author: Prathamesh Salunke

Keywords: video tutorial, method, overloading


Visual Cue
Narration
Slide 1

Opening slide

Welcome to the Spoken Tutorial on method overloading in java.
Slide 2

Learning Objectives

In this tutorial we will learn

- What is method overloading.

- To overload method.



Slide 3

System Requirements

Here we are using
  • Ubuntu version 11.10
  • Java Development Environment jdk 1.6
  • Eclipse 3.7


Slide 4

Prerequisites

To follow this tutorial you must know

how to create methods and

to overload constructor in java using eclipse.


If not, for relevant tutorials please visit our website which is as shown,

(http://www.spoken-tutorial.org)

Slide 5

Method Overloading

Define two or more methods with same name within a class.

They must differ in number or types of parameters.

These methods are called overloaded methods.

The process is called method overloading.

Switch to Eclipse


int a = 10;

int b = 5;

Let us now see how to overload method.

In eclipse, I have a class Addition.

Let us declare two integer variables.

So typre int a is equalto 10 and int b is equalto 5.

Type:

void add()

{

System.out.println(a+b);

}

Let us create a method to add these two integers.

So type void add parentheses.

Within curly brackets System dot out dot println.

Within parentheses a+b.

So this method will give us the sum of two integers.

Type:

void addTwoNumbers(int num1,int num2)

{

System.out.println(num1, num2);

}

Let us create a method that takes two numeric arguments.

The method prints the sum of two numbers.

So type void addTwoNumbers.

Within parentheses int num1 comma int num2.

Within curly brackets System dot out dot println num1 plus num2.

Type:

Addition obj = new Addition();


obj.add();

obj.addTwoNumbers(2.5,3.5);

Let us create an object of the class and call the methods.

So in Main type Addition obj is equalto new Addition parentheses.

Obj.add parentheses.

Obj.addTwonumbers parentheses.

Within parentheses we have to pass integer arguments.

Suppose if we pass floating point values.

So type 2.5 comma and integer 3.

Point to the error. We see an error that method addTwoNumbers double, double is undefined.
addTwoNumbers(double num1,double num2) So what we do is in the method instead of int we will give double.

So instead of int type double

Point to addTwoNumbers(2.5,3.5); So we see that the error is resolved.

And as we know that Java automatically i.e. implicitly coverts int to double.

So here we can pass integer arguments as well.

Save and Run Save and Run the program
Point to the output In the output we see the sum of two integers we declared.

And the sum of two numeric arguments that we passed.

Point to the methods Now we see that both the method perfroms same operation.

The difference is that first method takes no parameter. While the other method takes two parameters.

So in such case java provides us with method overloading.

Replace addTwoNumbers with add So what we do is give same name to both the methods.

So replace addTwoNumbers with add.

Type:

void add(double n1,double n2,double n3)


Highlight

void add(double n1,double n2,double n3)

We will define one more method with same opearation.

So type void add.

Within parentheses double n1 comma double n2 comma double n3.

So we gave three parameters to the method.



{

System.out.println(n1+n2+n3);

}

Within curly brackets System dot out dot println.

Within parentheses n1 plus n2 plus n3.

So this method will give sum of three numbers.

Type:

obj.add(1,5,6);

Now let us call the method.

So type add within parentheses 1 comma 5 comma 6

Save and Run Save and Run the program.
Point to the output So in the output we see the sum of three number as well.

Java compiler overloads the proper method depending on the parameters.

It checks the number and type of parameters.

So as a programmer we dont have worry about the method name.

Or even the type or number of argument passed.

Type:

void add(String s1,String s2)

{

System.out.println(s1+s2);

}

We can create one more overload method which append strings.

So type void add.

Within parentheses String s1 comma String s2.

Within curly brackets System dot out dot println.

Within parentheses s1 plus s2.

Type:

obj.add(“Hello ”,“World”);

Let us now call the method.

So type obj dot add.

Within parentheses in double quotes Hello comma in double quotes space World.

Save and Run Save and Run the program.
Point to the output So in the output we see Hello space World.

So the add method with two string arguments, appends the string.

Type:

int add(){}

Suppose now we declare add method with return type.

So type int add parentheses curly brackets.

Point to the error


Remove int add(){}

We get an error that it's a duplicate method.

This is because we have already defined a method add with no parameters.

So remember that to overload method the parameters must differ.

Having different return types will not overload the method.

So remove this part and Save the file.

This is how method overloading is done.

Summary Slide So in this tutorial we learnt,

- About method overloading.

- To overload method.

- Advantage of method overloading.

Slide

Assignment

For self assessment, create multiple overload methods for class Employee.

Overload the constructor.



Slide

About Slide

To know more about the Spoken Tutorial Project
  • If you do not have good bandwidth, you can download and watch it


Slide

About Slide


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


Slide Acknowledgment Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


We have come to the end of this tutorial.

Thanks for joining.

This is Prathamesh Salunke signing off.

Jai Hind.

Contributors and Content Editors

Chandrika