SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Polymorphism
• Poly means Many.
• Morph means to change shape
• In programming polymorphism means changing behavior of methods
• Polymorphic methods have the same signature, that is, the name and
parameters are the same
• The methods are in different classes but related through inheritance
Class Card {
protected double amount;
protected double discount;
public virtual double Discount() {
discount = amount * 0.01;
}
}
Class GoldCard : Card {
:
public override double Discount() {
discount = amount * 0.05;
}
}
Saifut
1
Polymorphism
• Card Class is a base class Gold Class is Derived from Card
• Card has a method Discount() Gold’s Discount() method has same signature
• Discount() method has a default Discount() method can override or replace the
implementation default implementation and give its’ own
• The word virtual indicates that this The word override indicates that the method
method is Virtual wants to change the implementation/behavior
Saifut
2
Class Card {
protected double amount;
protected double discount;
public virtual int Discount() {
discount = amount * 0.01;
}
Class GoldCard : Card {
public override int Discount() {
discount = amount * 0.05;
}
Polymorphism
• Let us look at a complete example of using Polymorphism
The program is an air line baggage information application. The
program asks the user to select the type of ticket they are interested in
and provides information about how many bags and weight per bag is
allowed for that type of ticket. The design is shown below:
Saifut
3
Polymorphism
Out put of the program when each radio button is selected followed by
clicking the purchase button
The classes and code behind the button is given in the next slides
Saifut
4
Polymorphism
Saifut
5
Class Ticket {
protected double weight; // allowed weight per bag
protected double nBags; // allowed number of bags
public virtual void AllowedBaggage() {
weight = 23;
nBags = 1;
}
Class Economy : Ticket {
:
// use default implementation
// of AllowedBaggage of base class
}
Class Business : Ticket {
:
Public override void AllowedBaggage() {
weight = 23;
nBags = 2;
}
Class First : Ticket {
:
Public override void AllowedBaggage() {
weight = 25;
nBags = 2;
}
Polymorphism
Saifut
6
Ticket t = null;
// create a Business, First or Economy ticket
// depending on which type of ticket was selected
// from the radio buttons on the form
if (rdoBusines.Checked )
t = new Business();
else if (rdoFirst.Checked)
t = new First();
else if (rdoEconomy.Checked)
t = new Economy();
// set the allowed baggage
// Note: C# will call the AllowedBaggage method of the class
// corresponding the object created. For example, if the object, t,
// was created as a First then First class’ method will be called
t.AllowedBaggage();
// print baggage information for the ticket
lstTicketInfo.Items.Add("Number of bags allowed: " + t.BAGS + “ Weight per bag: " + t.WEIGHT);

Weitere ähnliche Inhalte

Was ist angesagt?

Algorithms and flowcharts1
Algorithms and flowcharts1Algorithms and flowcharts1
Algorithms and flowcharts1Lincoln School
 
1153 algorithms%20and%20flowcharts
1153 algorithms%20and%20flowcharts1153 algorithms%20and%20flowcharts
1153 algorithms%20and%20flowchartsDani Garnida
 
Programming fundamentals lecture 4
Programming fundamentals lecture 4Programming fundamentals lecture 4
Programming fundamentals lecture 4Raja Hamid
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1luhkahreth
 
Algorithms and how to write an algorithms
Algorithms and how to write an algorithmsAlgorithms and how to write an algorithms
Algorithms and how to write an algorithmsAhmed Nobi
 
Chapter 2 Representation Of Algorithms 2
Chapter 2  Representation Of  Algorithms 2Chapter 2  Representation Of  Algorithms 2
Chapter 2 Representation Of Algorithms 2Li-Anne Serrano
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statementspragya ratan
 
Algorithms and flowcharts by Haseeb Khan
Algorithms and flowcharts by Haseeb KhanAlgorithms and flowcharts by Haseeb Khan
Algorithms and flowcharts by Haseeb KhanHaseeb Shalmani
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and FlowchartsDeva Singh
 
Algorithms and flowcharts ppt (seminar presentation)..
 Algorithms and flowcharts  ppt (seminar presentation).. Algorithms and flowcharts  ppt (seminar presentation)..
Algorithms and flowcharts ppt (seminar presentation)..Nagendra N
 
Algorithmsandflowcharts2
Algorithmsandflowcharts2Algorithmsandflowcharts2
Algorithmsandflowcharts2Darlene Interno
 
Algorithms, flow charts and pseudocodes
Algorithms, flow charts and pseudocodesAlgorithms, flow charts and pseudocodes
Algorithms, flow charts and pseudocodesSatveer Mann
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchartJordan Delacruz
 

Was ist angesagt? (20)

Algorithms and flowcharts1
Algorithms and flowcharts1Algorithms and flowcharts1
Algorithms and flowcharts1
 
Penyelesaian masalah
Penyelesaian masalahPenyelesaian masalah
Penyelesaian masalah
 
1153 algorithms%20and%20flowcharts
1153 algorithms%20and%20flowcharts1153 algorithms%20and%20flowcharts
1153 algorithms%20and%20flowcharts
 
Programming fundamentals lecture 4
Programming fundamentals lecture 4Programming fundamentals lecture 4
Programming fundamentals lecture 4
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Algorithms and how to write an algorithms
Algorithms and how to write an algorithmsAlgorithms and how to write an algorithms
Algorithms and how to write an algorithms
 
Chapter 2 Representation Of Algorithms 2
Chapter 2  Representation Of  Algorithms 2Chapter 2  Representation Of  Algorithms 2
Chapter 2 Representation Of Algorithms 2
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statements
 
Algorithms and flowcharts by Haseeb Khan
Algorithms and flowcharts by Haseeb KhanAlgorithms and flowcharts by Haseeb Khan
Algorithms and flowcharts by Haseeb Khan
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 
Algorithm itabq
Algorithm itabqAlgorithm itabq
Algorithm itabq
 
Algorithms and flowcharts ppt (seminar presentation)..
 Algorithms and flowcharts  ppt (seminar presentation).. Algorithms and flowcharts  ppt (seminar presentation)..
Algorithms and flowcharts ppt (seminar presentation)..
 
Algorithmsandflowcharts2
Algorithmsandflowcharts2Algorithmsandflowcharts2
Algorithmsandflowcharts2
 
Algorithms, flow charts and pseudocodes
Algorithms, flow charts and pseudocodesAlgorithms, flow charts and pseudocodes
Algorithms, flow charts and pseudocodes
 
Math 7 inequalities and intervals
Math 7   inequalities and intervalsMath 7   inequalities and intervals
Math 7 inequalities and intervals
 
Pseudocode
PseudocodePseudocode
Pseudocode
 
Algo and flowchart
Algo and flowchartAlgo and flowchart
Algo and flowchart
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchart
 

Mehr von abdullah619

Testing chapter updated (1)
Testing chapter updated (1)Testing chapter updated (1)
Testing chapter updated (1)abdullah619
 
Exception handling
Exception handlingException handling
Exception handlingabdullah619
 
4 oo inheritance in c# (1)
4   oo inheritance in c# (1)4   oo inheritance in c# (1)
4 oo inheritance in c# (1)abdullah619
 
3 instantiating an object in c# (1)
3  instantiating an object in c# (1)3  instantiating an object in c# (1)
3 instantiating an object in c# (1)abdullah619
 
Methods in c# (1)
Methods in c# (1)Methods in c# (1)
Methods in c# (1)abdullah619
 

Mehr von abdullah619 (12)

Testing chapter updated (1)
Testing chapter updated (1)Testing chapter updated (1)
Testing chapter updated (1)
 
Queue
QueueQueue
Queue
 
Stacks
StacksStacks
Stacks
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 
Lists
Lists Lists
Lists
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
 
Exception handling
Exception handlingException handling
Exception handling
 
Abstrcation
AbstrcationAbstrcation
Abstrcation
 
4 oo inheritance in c# (1)
4   oo inheritance in c# (1)4   oo inheritance in c# (1)
4 oo inheritance in c# (1)
 
3 instantiating an object in c# (1)
3  instantiating an object in c# (1)3  instantiating an object in c# (1)
3 instantiating an object in c# (1)
 
Methods in c# (1)
Methods in c# (1)Methods in c# (1)
Methods in c# (1)
 
5. linked list
5. linked list5. linked list
5. linked list
 

Kürzlich hochgeladen

SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 

Kürzlich hochgeladen (20)

SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 

Polumorphism

  • 1. Polymorphism • Poly means Many. • Morph means to change shape • In programming polymorphism means changing behavior of methods • Polymorphic methods have the same signature, that is, the name and parameters are the same • The methods are in different classes but related through inheritance Class Card { protected double amount; protected double discount; public virtual double Discount() { discount = amount * 0.01; } } Class GoldCard : Card { : public override double Discount() { discount = amount * 0.05; } } Saifut 1
  • 2. Polymorphism • Card Class is a base class Gold Class is Derived from Card • Card has a method Discount() Gold’s Discount() method has same signature • Discount() method has a default Discount() method can override or replace the implementation default implementation and give its’ own • The word virtual indicates that this The word override indicates that the method method is Virtual wants to change the implementation/behavior Saifut 2 Class Card { protected double amount; protected double discount; public virtual int Discount() { discount = amount * 0.01; } Class GoldCard : Card { public override int Discount() { discount = amount * 0.05; }
  • 3. Polymorphism • Let us look at a complete example of using Polymorphism The program is an air line baggage information application. The program asks the user to select the type of ticket they are interested in and provides information about how many bags and weight per bag is allowed for that type of ticket. The design is shown below: Saifut 3
  • 4. Polymorphism Out put of the program when each radio button is selected followed by clicking the purchase button The classes and code behind the button is given in the next slides Saifut 4
  • 5. Polymorphism Saifut 5 Class Ticket { protected double weight; // allowed weight per bag protected double nBags; // allowed number of bags public virtual void AllowedBaggage() { weight = 23; nBags = 1; } Class Economy : Ticket { : // use default implementation // of AllowedBaggage of base class } Class Business : Ticket { : Public override void AllowedBaggage() { weight = 23; nBags = 2; } Class First : Ticket { : Public override void AllowedBaggage() { weight = 25; nBags = 2; }
  • 6. Polymorphism Saifut 6 Ticket t = null; // create a Business, First or Economy ticket // depending on which type of ticket was selected // from the radio buttons on the form if (rdoBusines.Checked ) t = new Business(); else if (rdoFirst.Checked) t = new First(); else if (rdoEconomy.Checked) t = new Economy(); // set the allowed baggage // Note: C# will call the AllowedBaggage method of the class // corresponding the object created. For example, if the object, t, // was created as a First then First class’ method will be called t.AllowedBaggage(); // print baggage information for the ticket lstTicketInfo.Items.Add("Number of bags allowed: " + t.BAGS + “ Weight per bag: " + t.WEIGHT);