0% found this document useful (0 votes)
16 views

Functions in Java Lesson 6 New

Uploaded by

anoushka2312
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Functions in Java Lesson 6 New

Uploaded by

anoushka2312
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

RECAP

Connect to prior learning

Difference Between Call by Value


and Call by Reference
 Identify the need for multiple functions in a class having
the same name

 Understand the concept of Function Overloading

 Define multiple functions with the same name, using


different parameters (known as Overloaded functions)

 Differentiate between the signature and the prototype


of a function

 Understand that functions with same names are


differentiated by their signature and not by their return
type
CONNECTING
(to objective/intention/map of the unit)

Learning Intention
Function overloading is a feature in Object Oriented Programming
languages that is used to reduce complexity and increase the efficiency
of the program.

In big programs there can be several tasks that are somewhat similar
but not same. These tasks are ideally placed inside separate methods
to make the code more organized and manageable. But keeping track
of such methods and their parameters is a challenging task for a
programmer.

This challenge can be over come by using the Function Overloading


feature in Java.
SUCCESS CRITERIA
Introduce/co- create

Success criteria for the Chapter/Unit:


Students will be able to
identify the need for multiple functions in a class that have the same name
understand the concept of Function Overloading
identify that the functions can be differentiated by the compiler at the
time of calling the function by using different parameter list (which
denotes the function’s signature)
understand how Type conversion plays a significant role in matching the
function call with the function definition
trace the output of a given code snippet
create overloaded functions in a program
Apply overloaded functions in a program
STIMULUS / STARTER
Define the following functions in a class called Add.
Each of the following methods receive two parameters
of the same type, adds them and returns the result.
public static int addnum(int a, int y)
{
return x+y;
}
public static String addString(String a, String y)
{
return a +” “+y;
}
public static double addnumbers(double d1, double d2)
{
return d1+d2;
}
INTRODUCTION(essential idea/real life connection)

 The methods shown in the previous slide perform tasks that


are similar but not same.
 Each of the methods has been given a unique name.

Disadvantages:
 It is the responsibility of the programmer to invoke the
appropriate method with the appropriate data types. The
programmer may have to choose the appropriate method
based on the data types using “if” constructs or “switch”
constructs.
 In a big program, keeping track of the methods and their
parameters is a challenging task for a programmer.

The above disadvantages can be overcome with a feature called


Function Overloading.
 When two or more methods in a class share
the same name but differ in the number, type
and order of arguments, then it is said to be
Function Overloading.

 This feature is useful when we need to


implement essentially the same method for
different types of data and the functionality is
somewhat similar but not same.

 Very useful for a programmer, as too many


function names need not be kept track of.
 When an overloaded method is invoked, Java
uses the signature of the function, that is the
type, number and the order of arguments as
its guide to determine which version of the
overloaded method to call.

 Overloaded methods may have different


return types, but the return type is not used
to distinguish two versions of a method at
the time of method call.
 Polymorphism is one of Java's most exciting and useful
features. “Poly” means many and “morph” means form – it
means a method existing in many forms.

 Method overloading is a feature where there can be


multiple methods in a class with the same name and
different signatures. It is one of the ways of implementing
polymorphism in Java.

 In Java, the appropriate methods will be called by the


compiler based on the arguments passed.

 In languages that do not support method overloading, each


method must be given a unique name and the programmer
must invoke the appropriate method.
 When an overloaded method is called, Java compiler
looks for an exact match between the Actual
parameters in the function call and the Formal
parameters in the function definition and binds or
connects the function call statement with the
appropriate function definition.

 If the compiler is not able to find an exact match,


then it uses implicit type conversions to convert the
actual parameters to the closest matching formal
parameter and binds the function call statement with
the function definition. However, if implicit
conversion is not possible, then it gives a compilation
error.
CHOICE BOARD
(learning engagements and resources )
CHECKS FOR LEARNING
(quick checks for progress/ questions: ongoing ,throughout the lesson )

Call the functions in the previous


slide from main() and test your code.
CLOSURE

https://forms.office.com/

You might also like