0% found this document useful (0 votes)
23 views34 pages

Day9 Polymorphism

Java

Uploaded by

mohamedjaaved19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views34 pages

Day9 Polymorphism

Java

Uploaded by

mohamedjaaved19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Polymorphism

TNS India Foundation | ‹#›


Partners in Economic Transformation
Recap

• Interfaces - It is a mechanism to achieve abstraction.


• Wrapper Classes - Convert primitive data types into objects.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. Which of these access specifiers can be used for an interface?

A public

B protected

C private

D Default

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. Which is the superclass of wrapper classes like Boolean and Character?

A Number

B Wrapper

C Object

D Math

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hook
class Test
{
public static void main(String args[])
{
void m1(char ch)
{
System.out.println(“Char-arg Constructor”);
}
void m1(int a)
{
System.out.println(“int-arg Constructor”);
}
}
}
What will happen if we have the same method name with one parameter twice in one
class ??
Is it possible for us to do so ???
TNS India Foundation | ‹#›
Partners in Economic Transformation
Learning Objective

● Understanding the concept of polymorphism


● Identifying the characteristics and features inherent in polymorphism
● Examining the benefits derived from utilizing polymorphism
● Investigating the drawbacks associated with polymorphism
● Categorizing the various types of polymorphism
● a. Compile-time polymorphism
● b. Runtime polymorphism
● In-depth Analysis of Runtime Polymorphism:
● Scrutinizing the rules governing method overriding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Polymorphism

• One thing can exhibits more than one form is called polymorphism.
• Polymorphism is a Greek word poly means many and morphism means forms.
• Polymorphism shows same functionality(method name same) with different logics
execution.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Characteristics / Features of Polymorphism

• The functionality of a method behaves differently in different scenarios.

• The behavior of a method depends on the data provided.

• It allows the same name for a member or method in a class with different types.

• Polymorphism supports implicit type conversion.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Advantages of Polymorphism

● Code Reusability

● Reduces coupling

● Supports a single variable name .

TNS India Foundation | ‹#›


Partners in Economic Transformation
Disadvantages of Polymorphism

● Reduces readability of the code

● Programmers find Polymorphism a little challenging to implement.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Types of Polymorphism

TNS India Foundation | ‹#›


Partners in Economic Transformation
Compile time /Static polymorphism /Early Binding
If java class allows two methods with same name but different number of arguments such type
of methods are called overloaded methods.

• We can overload the methods in two ways.


• By passing different number of arguments to the same methods.
void m1(int a) { }
void m1(int a,int b){ }
• Provide the same number of arguments with different
data types.
void m1(int a) { }
void m1(char ch){ }

• If we want achieve overloading concept one class is enough.

• It is possible to overload any number of methods in single java class.


TNS India Foundation | ‹#›
Partners in Economic Transformation
Types of Overloading

• Method overloading
explicitly by the programmer
• Constructor overloading

• Operator overloading. implicitly by the JVM ( ‘+’ addition & concatenation)

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Constructor Overloading

If class has more than one constructors, then they are called as overloaded
constructors. When constructors are overloaded, they must differ in

● Number of parameters
● Type of parameters
● Order of parameters

TNS India Foundation | ‹#›


Partners in Economic Transformation
Constructor Overloading

TNS India Foundation | ‹#›


Partners in Economic Transformation
Rules for overloading

• Same Method Name:


• All overloaded methods must have the same name.
• Different Parameter Lists:
• The parameter lists of the overloaded methods must differ in either the number of
parameters or the data types of the parameters, or both.
• Different Parameter Order:
• The order of parameters also contributes to method overloading. If the order of
parameters is different, it is considered a different method.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Rules for overloading

• Different Data Types:


• Overloaded methods can have different data types for the parameters, as long as
the compiler can distinguish between them.
• Return Type is Not Considered:
• The return type alone is not sufficient to differentiate overloaded methods. The
parameter list must be distinct.
• Access Modifiers or Exceptions:
• The access modifiers (public, private, protected) or the exceptions thrown by the
methods do not participate in method overloading. Overloaded methods can have
the same or different access modifiers and can throw the same or different
exceptions.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Real Time Example

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Runtime polymorphism /Dynamic or Late Binding

● In a class hierarchy, when a method in a subclass has the same name and type
signature as a method in its super class, then the subclass method overrides
the super class method

● Overridden methods allow Java to support run-time polymorphism

TNS India Foundation | ‹#›


Partners in Economic Transformation
Rules for overriding

• While overriding child class method signature & parent class method signatures must
be same otherwise we are getting compilation error.

• The return types of overridden method & overriding method must be same.

• While overriding the methods it is possible to maintain same level permission or


increasing order but not decreasing order , if you are trying to reduce the permission
compiler generates error message “Attempting to assign weaker access privileges”.

• Final methods are preventing overriding.

• Static methods are bounded with class hence we are unable to override static
methods.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Compile time Vs Run time polymorphism
The method call is handled by the compiler. The compiler cannot control the method call
in run time.
Compile-Time Polymorphism is less Run-Time Polymorphism exhibits higher
flexible, as it needs to handle all method flexibility as the method calls get handled at
calls in compile-time run-time

The execution period for the Compile-Time The execution period for the Run-Time
Polymorphism is less Polymorphism is more

Integrating the right method call with the Combining the correct method call with the
proper method is done in compile-time right method is done in run-time

Occurs during Method Overloading and Occurs during Method Overriding


Operator Overloading
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz

Q1. Which keyword is used to annotate a method as intended for method


overriding in Java?

A final

B private

C Override

D @Override

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. Which method signature in Java allows you to override a superclass method
while changing the return type?

A Changing the method name

B Changing the method parameters

C Changing the method access modifier

D Changing the method parameters and the return type

TNS India Foundation | ‹#›


Partners in Economic Transformation
Summary

• The ability to appear in more forms is called polymorphism.


• There are two types of polymorphism.
• Compile time polymorphism.
Example: Method Overloading
• Runtime polymorphism.
Example: Method Overriding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question

Make Payment using Overloading

Consider doing an extra feature for the stage show organizers. Bring up an interactive
console application for Billing so that our application looks unique from other competitors.
Customers pay using cash, online wallets, and credit card. For each category obtain the
necessary information from the user. You also require a receipt for all the transactions
which should be printed at the end of the transaction. Let's increase our coding proficiency
by implementing Function overloading for the payments. Hence write a program meeting all
the above specifications.

Create a class named TicketBooking with the following private attributes.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question

Include getters and setters for the class.

Include default and parameterized constructors.

The format for a parameterized constructor is TicketBooking(String stageEvent, String


customer, Integer noOfSeats)

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question

The TicketBooking class has the following methods.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question

Create a driver class called Main. In the Main method, obtain input from the user in CSV
format and call appropriate methods for transactions. If a choice other than specified is
chosen, print "Invalid choice".

Note: display one digit after the decimal point for double values.

[Strictly adhere to the Object-Oriented Specifications given in the problem statement.

All class names, attribute names, and method names should be the same as specified in
the problem statement.]

TNS India Foundation | ‹#›


Partners in Economic Transformation
Input Format
The first input is ticket booking details. Format for TicketBooking Input is stageEvent,
customer, and noOfSeats separated by a comma.

The next integer value is Payment mode. If 1. Cash payment, 2.Wallet payment and
3.Credit card payment.

If the Payment mode is 1, then read the integer input which represents the amount

If the Payment mode is 2, then read integer input which represents the amount, followed by
the wallet number(string)

If the Payment mode is 3, then read string input which represents the cardholder name,
followed by the amount (integer), credit card type(string), and CCV number(String)

Output Format
TNS India Foundation | ‹#›
Partners in Economic Transformation
Refer to the sample Output for formatting.
Sample Input and Output:
Magic show,Lunu,43 Stage event:Magic show
1 Customer:Lunu
500 Number of seats:43
Amount 500.0 paid in cash
PCB workshop,Ahamed,3 Stage event:PCB workshop
2 Customer:Ahamed
300 Number of seats:3
ASD-951 Amount 300.0 paid using wallet
number ASD-951
Electronics,Raja,2 Stage event:Electronics
3 Customer:Raja
Raja Number of seats:2
200 Holder name:Raja
Master Amount 200.0 paid using Master
9874-4758-9856 card
CCV:9874-4758-9856
Foodies,Alice,1
TNS India Foundation | ‹#›
Partners in Economic Transformation
Invalid choice
Let’s Reflect -

1. What is your major learning from today’s session?


2. How are you going to use this learning in your journey on the job?

TNS India Foundation | ‹#›


Partners in Economic Transformation

You might also like