0% found this document useful (0 votes)
15 views23 pages

Lecture No 8

The document discusses polymorphism and overloading in Java, defining polymorphism as the ability of a message to take multiple forms, with two types: static (overloading) and dynamic (overriding). It explains method overloading, its advantages, and provides examples of how to overload methods by changing the number, type, and sequence of parameters. Additionally, it covers constructor overloading, emphasizing its similarity to method overloading and providing tasks for implementing overloaded constructors in Java classes.

Uploaded by

Batool Murtaza
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)
15 views23 pages

Lecture No 8

The document discusses polymorphism and overloading in Java, defining polymorphism as the ability of a message to take multiple forms, with two types: static (overloading) and dynamic (overriding). It explains method overloading, its advantages, and provides examples of how to overload methods by changing the number, type, and sequence of parameters. Additionally, it covers constructor overloading, emphasizing its similarity to method overloading and providing tasks for implementing overloaded constructors in Java classes.

Uploaded by

Batool Murtaza
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/ 23

Lecture

No.8
Polymorphism &
Overloading
Polymorphism

 We can define polymorphism as the ability of a message to be displayed in more than one form.

 It is one of the core concepts of object-oriented programming (OOP).


Types of Polymorphism

• Java supports 2 types of


polymorphism:
1. static or compile-time
(Overloading)
2. dynamic (Overriding)
Static or compile-time (Overloading)

• Overloading is related to compile-time (or static) polymorphism.


• Overloading allows different methods to have the same name in same class.
• But different signatures where the signature can differ by
i. The number of input parameters
ii. Type of input parameters
iii. Sequence of input parameters type.
Method Overloading
• The below points brief what does method signature
means in Java

i. Returntype of method is not part of method signature in Java.


ii. Number of argument to a method is part of method signature.
iii. Type of argument to a method is also part of a method
signature
iv. Order of argument also forms a part of method signature
provided they are of different type.
Advantages of method overloading
 Overloading in Java is the ability to create multiple methods of the same
name, but with different parameters.

 The main advantage of this is cleanliness of code.

 Method overloading increases the readability of the program.

 Overloaded methods give programmers the flexibility to call a similar method


for different types of data.

 Overloading is also used on constructors to create new objects given different


amounts of data.
Different Ways to overload method

• By changing the no. of arguments.


• By changing the data types of
arguments.
• By changing the sequence of
arguments.
Example ( By changing the no. of arguments )

class Calculator{
public int add(int
a,int b){ return
a+b;
}
public int add(int a,int
b,int c){ return
a+b+c;
}
public int add(int a, int b,int
c,int d){ return
Example (By changing the data types of arguments)
class Calculator{
public int add(int
a,int b){ return
a+b;
}
public int add(int
a,float b){ return
a+b;
}
public string add(int
a,string b){ return
Example (By changing the sequence of arguments)

class Calculator{
public double add(int a,float
b,double c){ return a+b+c;
}
public double add(float a,int
b,double c){ return a+b+c;
}
public double add(double a,float
b,int c){ return a+b+c;
}
}
Task
Create a class called StringHelper with two methods named concatenate. One method takes in two
strings and returns their concatenation. The other method takes in three strings and returns their
concatenation. Create a StringHelper object and call both methods with appropriate parameters.
Task
Create a class called OverloadDemo with three methods named test. One method takes no
parameters, the second method takes in an integer parameter and third method takes in two integer
parameters. Create a forth method takes in double parameter and returns their product. Create an
OverloadDemo object and call above mentioned methods with appropriate parameters
Automatic type conversions apply to
overloading
Constructor Overloading

• Like methods, constructors can also be overloaded.

• Constructor overloading is a concept of having more than one constructor


with different parameters list, in such a way so that each constructor
performs a different task.

• Constructor overloading is a technique in Java in which a class can have any


number of constructors that differ in parameter list.

• The compiler differentiates these constructors by taking into account the


number of parameters in the list and their type.
Important points related to Constructor
overloading
 Constructor overloading is similar to method overloading in Java.

 You can call overloaded constructor by using this() keyword in Java.

 Overloaded constructor must be called from another constructor only.

 If an overloaded constructor called, it must be the first statement of


constructor in java.
Constructor Overloading
Task
Write a Java program to create a class called Person with instance variables name, age, and gender.
Implement the two parameterized constructors:

•One constructor takes String and integer as parameters.

•The other constructor takes String, Integer and String as parameters.

•Print the values of the variables for each constructor.


Task
Write a Java program to create a class called Book with instance variables title, author, and price.
Implement a default constructor and two parameterized constructors:

•One constructor takes title and author as parameters.

•The other constructor takes title, author, and price as parameters.

•Print the values of the variables for each constructor.


Output:

You might also like