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

  • SEARCH
  • Home
  • 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 8095987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:18:56+00:00 2026-06-05T21:18:56+00:00

My brother wrote this code for me to show an example of polymorhism. But

  • 0

My brother wrote this code for me to show an example of polymorhism. But I don’t really understand how it all works, starting with the parameters. Can someone dissect the code and tell me a little bit about how it all works together and how it functions?

EDIT: Here’s a specific question: mammals.Add(new Cat(“Snow white”, 1, 0, “White”, “blue”)); What do the numbers do? What is their purpose and how do they work with the code?

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            List<Mammal> mammals = new List<Mammal>();

            mammals.Add(new Cat("Snow white", 1, 0, "White", "blue"));
            mammals.Add(new HumanFemale("Krysten", 72, 15, "White", "Blue"));
            mammals.Add(new Cat("Larry", 7, 2, "Orange", "Hazel"));
            mammals.Add(new Dog("Walt", 3, 5, "Brown", "Hazel"));
            mammals.Add(new HumanMale("Ryan", 72, 31, "White", "Blue"));
            mammals.Add(new Cat("Blacky", 5, 10, "Black", "Brown"));

            foreach (Mammal m in mammals)
            {
                m.Speak();
                m.Walk();
            }
            Console.ReadKey();
        }
    } // end of Program class declaration




    public abstract class Mammal
    {
        public Mammal(string name, int heightInInches, int age, string color, string eyeColor)
        {
            this.Name = name;
            this.HeightInInches = heightInInches;
            this.Age = age;
            this.Color = color;
            this.EyeColor = eyeColor;
        }

        private int _HeightInInches;
        private int _Age;

        /// <summary>
        /// Gets or sets the age (in years).
        /// </summary>
        public int Age
        {
            get 
            { 
                return _Age; 
            }
            set 
            {
                if (value < 0)
                {
                    throw new Exception("Invalid Age!");
                }
                _Age = value; 
            }
        }

        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        public int HeightInInches
        {
            get 
            { 
                return _HeightInInches; 
            }
            set 
            {
                if (value < 0)
                {
                    throw new Exception("Invalid height!");
                }
                _HeightInInches = value; 

            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the skin or fur color.
        /// </summary>
        public string Color { get; set; }


        /// <summary>
        /// Gets or sets the eye color.
        /// </summary>
        public string EyeColor { get; set; }


        /// <summary>
        /// Causes the mammal to speak.
        /// </summary>
        public virtual void Speak()
        {
            Console.WriteLine("The mammal spoke.");
        }

        /// <summary>
        /// Causes the mammal to walk.
        /// </summary>
        public virtual void Walk()
        {
            Console.WriteLine("The mammal is walking.");
        }

    } // end of Mammal class declaration


    public class Dog : Mammal
    {
        public Dog(string name, int heightInInches, int age, string color, string eyeColor) 
            : base (name, heightInInches, age, color, eyeColor)
        {
        }

        public override void Speak()
        {
            Console.WriteLine("{0} the Dog says: 'Ruff!'", this.Name);
        }

        public override void Walk()
        {
            if (this.Age == 0)
            {
                Console.WriteLine("{0}, the {1} newborn puppy was just born and is too little to walk yet!", this.Name, this.Color);
            }

            else if (Age > 0 && Age <= 5)
            {
                Console.WriteLine("{0}, the {1} puppy is {2} years old and is running around like a hyper puppy, chewing up all the furniture. Oh nooo!", this.Name, this.Color, this.Age);
            }

            else if (Age > 5 && Age <= 10)
            {
                Console.WriteLine("{0}, the {1} dog is {2} years old and walks conservatively", this.Name, this.Color, this.Age);
            }

            else if (Age > 10)
            {
                Console.WriteLine("{0}, the {1} dog is {2} years old and walks very slowly, and has arthiritus in the joints.", this.Name, this.Color, this.Age);
            }
        }

    } // end of Dog class


    public class Cat : Mammal
    {
        public Cat(string name, int heightInInches, int age, string color, string eyeColor)
            : base(name, heightInInches, age, color, eyeColor)
        {
        }

        public override void Speak()
        {
            Console.WriteLine("{0} the Cat says: 'Meow!'", this.Name);
        }

        public override void Walk()
        {

            if (this.Age == 0)
            {
                Console.WriteLine("{0}, the {1} newborn Kitten was just born and is too little to walk yet!", this.Name, this.Color);
            }

            else if (Age > 0 && Age <= 5)
            {
                Console.WriteLine("{0}, the {1} Kitten is {2} years old and is running around like a hyper kitten!", this.Name, this.Color, this.Age);
            }

            else if (Age > 5 && Age <= 10)
            {
                Console.WriteLine("{0}, the {1} cat is {2} years old and walks conservatively", this.Name, this.Color, this.Age);
            }

            else if (Age > 10)
            {
                Console.WriteLine("{0}, the {1} cat is {2} years old and walks very slowly", this.Name, this.Color, this.Age);
            }
        }
    } // end of Cat class

    public abstract class Human : Mammal
    {
        public Human(string name, int heightInInches, int age, string color, string eyeColor)
            : base(name, heightInInches, age, color, eyeColor)
        {
        }


        public override void Walk()
        {

            if (this.Age == 0)
            {
                Console.WriteLine("{0}, the {1} newborn baby was just born and is too little to walk yet!", this.Name, this.Color);
            }

            else if (Age > 0 && Age <= 2)
            {
                Console.WriteLine("{0}, the {1} toddler is {2} years old and is crawling around!", this.Name, this.Color, this.Age);
            }

            else if (Age > 2 && Age <= 5)
            {
                Console.WriteLine("{0}, the {1} kid is {2} years old and is walking around!", this.Name, this.Color, this.Age);
            }

            else if (Age > 5 && Age <= 12)
            {
                Console.WriteLine("{0}, the {1} kid is {2} years old and walks briskly", this.Name, this.Color, this.Age);
            }

            else if (Age > 12 && Age <= 19)
            {
                Console.WriteLine("{0}, the {1} audlt is {2} years old and walks briskly", this.Name, this.Color, this.Age);
            }

            else if (Age > 20 && Age <= 60)
            {
                Console.WriteLine("{0}, the {1} adult is {2} years old and walks conservatively", this.Name, this.Color, this.Age);
            }

            else if (Age > 60)
            {
                Console.WriteLine("{0}, the {1} old person is {2} years old and walks with a cane", this.Name, this.Color, this.Age);
            }
        }

        public  override void Speak()
        {
            Console.WriteLine("The human spoke");
        }
    } // end of Human class

    public class HumanMale : Human
    {
        public HumanMale(string name, int heightInInches, int age, string color, string eyeColor)
            : base(name, heightInInches, age, color, eyeColor)
        {

        }

        public override void Speak()
        {
            if (this.Age == 0)
            {
                Console.WriteLine("{0}, the newborn baby boy was just born and is too young to speak", this.Name);
            }
            if (this.Age > 0 && this.Age <= 3)
            {
                Console.WriteLine("{0}, the toddler boy babbles 'goo goo ga ga'", this.Name);
            }

            if (this.Age > 3 && this.Age <= 5)
            {
                Console.WriteLine("{0}, the toddler boy says, 'I like fire trucks'", this.Name);
            }

            if (this.Age > 5 && this.Age <= 12)
            {
                Console.WriteLine("{0}, the young boy says: I want to be a fireman'", this.Name);
            }

            if (this.Age > 12 && this.Age <= 20)
            {
                Console.WriteLine("{0}, the teenage boy says: I want a girlfriend'", this.Name);
            }

            if (this.Age > 20)
            {
                Console.WriteLine("{0}, the adult male says, 'Hey hey hey!'", this.Name);
            }

        }

    } // end of HumanMale class

    public class HumanFemale : Human
    {
        public HumanFemale(string name, int heightInInches, int age, string color, string eyeColor)
            : base(name, heightInInches, age, color, eyeColor)
        {

        }

        public override void Speak()
        {
            if (this.Age == 0)
            {
                Console.WriteLine("{0}, the newborn baby girl was just born and is too young to speak", this.Name);
            }
            if (this.Age > 0 && this.Age <= 3)
            {
                Console.WriteLine("{0}, the toddler girl babbles 'da da goo goo ga ga'", this.Name);
            }

            if (this.Age > 3 && this.Age <= 5)
            {
                Console.WriteLine("{0}, the girl says 'I wanna be a princess'", this.Name);
            }

            if (this.Age > 5 && this.Age <= 12)
            {
                Console.WriteLine("{0}, the young girl says: I AM a princess'", this.Name);
            }

            if (this.Age > 12 && this.Age <= 20)
            {
                Console.WriteLine("{0}, the teenage girl says: Like, totally. Did you see that other chick? Like, what was she wearing'", this.Name);
            }

            if (this.Age > 20)
            {
                Console.WriteLine("{0}, the adult female says, 'Yep, I'm a woman.'", this.Name);
            }

        }

    } // end of HumanFemale class


} // end of namespace declaration
  • 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. Editorial Team
    Editorial Team
    2026-06-05T21:18:58+00:00Added an answer on June 5, 2026 at 9:18 pm

    For the specific question in the EDIT:

    mammals.Add(new Cat("Snow white", 1, 0, "White", "blue")); 
    

    The line can be rewritten as:

    Cat aCat = new Cat("Snow white", 1, 0, "White", "blue"); 
    mammals.Add(aCat);
    

    In the first line you are calling the constructor of Cat:

    public Cat(string name, int heightInInches, int age, string color, string eyeColor)
                : base(name, heightInInches, age, color, eyeColor)
    

    passing “Snow white” as name, 1 as heightInInches, 0 as age, “White” as color and “blue” as eyeColor. The constructor is empty (it does nothing, but it is based upon the Mammal constructor (this is the meaning of base(name, heightInInches, age, color, eyeColor)), which is called passing the same parameters. This means that public Mammal(string name, int heightInInches, int age, string color, string eyeColor) is executed, so you end up with an instance of Cat where all the properties have the values that were passed as parameters.

    Now you execute

     mammals.Add(aCat);
    

    Here you are adding the instance of Cat to a list of Mammal. You ca do that because Add expect a Mammal as argument, and aCat is a Mammal, since its class is derived from Mammal.

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

Sidebar

Related Questions

I'm sorry to bother you with this, but i'm stuck with it for too
I am cleaning up some code in a C# app that I wrote and
Unfortunately, I wrote a lot of code using mysql_query, mysql_fetch_array and mysql_num_rows calls. It's
I am totally confused with ArrayList behavior. Wrote really long post, then realized no
This question may seem rather basic, but coming from an engineering (non computer-science) background,
My compiler, MPLAB C30 (GCC v3.23) is not compiling this code: if(font_info.flags & FONT_LOWERCASE_ONLY)
I wrote the following code when trying to make a doubly-linked list with an
So, first off, this is homework, albeit not mine. It's my brother-in-law's. He asked
I've recently started using code coverage tools (particularily Emma and EclEmma), and I really
I wrote some simple code in C++, and I built it and ran it

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.