Polymorphism-Method
Overloading
Session No.:2
Course Name: Java Programming
Course Code: R1UC304C
Instructor Name: Chandiraprakash.N
Galgotias University 1
key concepts of session no. 1
▪ Introduction to Object Oriented Programming Concepts
▪ Features of OOPs
▪ Introduction to Java Programming Language
▪ Features of Java
▪ JDK, JVM and Java Compiler
Galgotias University 2
Reflect on the
responses of
post session
activity
Galgotias University 3
👉Imagine you are designing a calculator app. You want the
“add() function to handle different scenarios: adding two
integers, two doubles or even three integers.
👉 How would you implement this in Java without using different
method names likes addIntegers, addDoubles or
addThreeNumbers?
Galgotias University 4
5
At the end of this session students will be able to
LO 1: Define polymorphism in Java and
distinguish between compile-time
(overloading) and run-time (overriding)..
LO 2: Explain
method overloading
with syntax and examples..
Galgotias University 6
1 Overview of Polymorphism
2 Core Concepts of Method Overloading
Session 3 Implementation of Method overloading
4
Outline 4 Activity
5 Summary
Galgotias University 7
Polymorphism in Java
• Polymorphism in Java is a concept by which we can perform a single action in
different ways. Polymorphism is derived from 2 Greek words: poly and morphs.
The word "poly" means many and "morphs" means forms. So polymorphism
means many forms.
• An individual can have different relationships with different people. A woman
can be a mother, a daughter, a sister, and a friend, all at the same time, i.e. she
performs different behaviours in different situations.
Why Use Polymorphism In Java?
• Code Reusability: Polymorphism allows the same method or class to be
used with different types of objects, which makes the code more useable.
• Reduced Code Complexity: Polymorphism helps reduce code complexity by
promoting a modular and hierarchical class structure, making it easier to
understand, maintain, and extend large-scale software systems.
GSCALE full form and date 8
Types of Polymorphism in Java
In Java Polymorphism is mainly divided into two types:
[Link]-Time Polymorphism (Static)
[Link] Polymorphism (Dynamic)
GSCALE full form and date 9
GSCALE full form and date 10
1. Compile-Time
Polymorphism
• Compile-Time Polymorphism in Java is also known as static polymorphism
and also known as method overloading.
• This happens when multiple methods in the same class have the same name
but different parameters.
• This is a form of compile-time polymorphism, where the compiler determines
which specific method to invoke based on the method signature (the
method's name and the number, type, and order of its parameters).
GSCALE full form and date 11
Key characteristics of
method overloading:
Same Method Name: All overloaded methods within a class must share the
same name.
Different Parameter Lists: The parameter lists of overloaded methods must
differ in at least one of the following ways:
1. Number of parameters: The methods can have a different count of
parameters.
2. Data Type of Parameters : The methods can accept parameters of
different data types.
3. Order of parameters: If the data types are the same, their order can be
different.
GSCALE full form and date 12
Return Type (Optional): While not a requirement for overloading, overloaded
methods can have different return types. However, overloading cannot be
achieved solely by changing the return type; the parameter list must still differ.
Benefits of Method Overloading:
Improved Readability and Maintainability:
Using the same method name for similar operations with varying inputs makes
the code more intuitive and easier to understand.
Code Reusability: It allows for a single conceptual operation to be applied to
different data types or numbers of arguments without creating entirely new
method names.
GSCALE full form and date 13
class Calculator {
int add(int a, int b)
{ return a + b; } // Overloaded method
1
double add(double a, double b) // Overloaded method 2
{ return a + b; }
}
class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
[Link]([Link](5, 10)); // calls int version
[Link]([Link](5.5, 2.5)); // calls double version
}
} GSCALE full form and date 14
Learning Activity 1:
Peer to Peer discussion
▪ Explain what is being done in the code?
▪ Tell the output of the above code
Galgotias University 15
Reflection-
Learning
Activity 1
▪ The activity encouraged critical thinking and collaborative learning,
helping students understand method overloading.
Galgotias University 16
// Program to illustrate Overloading
class overloadingmethod public static void main(String args[])
{ {
static float add(int x , int y) int x = 100; int y=200;
{ float m =15.5f;
return x+y; float n = 10.5f;
}
String s1="125";
static float add(float t1,float t2)
{ String s2="145";
return t1+t2; [Link](add(x,y));
[Link](add(m,n));
} [Link](add(s1,s2));
static float add(String s1,String s2)
}
{
}
float sum;
Output:
sum=[Link](s1)+[Link](s
2); 300.0
return sum; 26.0
GSCALE full form and date 17
} 270.0
Example
Changing the Order of the Parameters of Methods
Method overloading can also be implemented by rearranging the parameters of
two or more overloaded methods.
For example, if the parameters of method 1 are (String name, int roll_no) and
the other method is (int roll_no, String name) but both have the same name,
then these 2 methods are considered to be overloaded with different
sequences of parameters.
Example: This example demonstrates method overloading by changing the
order of parameters in the StudentId() method.
Galgotias University 18
// Java Program to Illustrate Method Overloading By changing the Order of the Parameters
// Importing required classes
import [Link].*;
// Class 1
// Helper class
class Student {
// Method 1
public void StudentId(String name, int roll_no)
{
[Link]("Name: " + name + " “ + "Roll-No: " + roll_no);
}
Galgotias University 19
// Method 2 // Class 2
public void StudentId(int roll_no, String name) // Main class
{ class Geeks {
// Again printing name and id of person
[Link]("Roll-No: " + roll_no + " " // Main function
+ "Name: " + name); public static void main(String[] args)
} {
// Creating object of above class
}
Student obj = new Student();
// Passing name and id
// Note: Reversing order
[Link]("Sweta", 1);
[Link](2, "Gudly");
}
}
Galgotias University 20
Learning Activity 2:
Think, Pair and Share
❑Discuss in pairs:
▪What will be the output of
the above code?
▪Can method overloading be
done by only changing the
return type ? Why or why
not?
Galgotias University 21
Reflection-
Learning
Activity 2
❑ By thinking individually, students make their understanding of method
overloading by changing order of parameters.
❑ Through pair discussions, they exchanged perspectives, which helped in
clarifying doubts and reinforcing key concepts.
❑ Class-wide sharing of insights allowed students to learn different approaches and
solutions, promoting deeper understanding.
Galgotias University 22
Summary
Method overloading in Java allows a class to have multiple methods with the
same name but different parameter lists (number, type, or order of parameters).
Here's a concise summary:
Definition: Multiple methods in the same class share the same name but differ in
their method signatures (parameter list).
Purpose: Enhances code readability and reusability by allowing different ways to
call a method for similar tasks.
Rules:
Methods must have different parameter lists (number, type, or order).
Return type alone cannot differentiate overloaded methods.
Access modifiers, exceptions, or static/non-static nature don’t affect overloading.
Galgotias University 23
Ensure attainment of LOs in
alignment to the learning
activities: outcomes (1-2)
Outcome 1: Student could understand the
concept of Polymorphism and Method
overloading in activity 1.
Outcome 2: Student will be able to write method
overloading code hence their practical coding
skills are built in Activity 2.
Galgotias University 24
Discussion on the post
session activities
Students have to attempt quiz based on Method
Overloading
Galgotias University 25
Next topic of the course:
Constructor and Constructor Overloading
• A constructor in Java is a special method that is used to
initialize objects.
• The constructor is called when an object of a class is created.
• Note that the constructor name must be same as the class
name, and it cannot have a return type (like void).
• Constructors can be overloaded.
Galgotias University 26
Review and Reflection
from students
Galgotias University 27