Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 116529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:11:32+00:00 2026-05-11T03:11:32+00:00

How can polymorphism replace an if-else statement or Switch inside of a loop? In

  • 0

How can polymorphism replace an if-else statement or Switch inside of a loop? In particular can it always replace an if-else? Most of the if-thens I use inside of loops are arithmetic comparisons. This question is spawned from this question.

int x; int y; int z;  while (x > y) {      if (x < z)      {          x = z;      } } 

How would this work with polymorphism?
NOTE: I wrote this in Java but am interested in this for any OOL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:11:33+00:00Added an answer on May 11, 2026 at 3:11 am

    Polymorphism usually replaces switch statements when each case corresponds to a different type. So instead of having:

    public class Operator {     string operation;      public int Execute(int x, int y)     {          switch(operation)          {              case 'Add':                  return x + y;              case 'Subtract':                  return x - y;              case 'Multiply':                  return x * y;              case 'Divide':                  return x / y;              default:                  throw new InvalidOperationException('Unsupported operation');          }     } } 

    you’d have:

    public abstract class Operator {     public abstract int Execute(int x, int y); }  public class Add : Operator {     public override int Execute(int x, int y)     {         return x + y;     } }  // etc 

    However, for the comparison type of decision you provided, polymorphism really doesn’t help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Related: How can I use polymorphism in XML Serialization? I have a class I
Possible Duplicate: How can I simulate OO-style polymorphism in C? I'm trying to use
I should use polymorphism over conditionals but can I use it in below case
Replace conditional with polymorphism is elegant only when type of object you're doing switch/if
I always think of having to use pointers for polymorphism. Using the canonical example:
can we implement polymorphism using interface in c#? any example .
Do polymorphism and operator overloading mix together? You can't do polymorphism without pointers, as
How can I unproxy a hibernate object, such that polymorphism would be supported? Consider
Many statically typed languages have parametric polymorphism. For example in C# one can define:
Possible Duplicate: Try to describe polymorphism as easy as you can What is polymorphism?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.