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 8583019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:26:13+00:00 2026-06-11T21:26:13+00:00

I am trying to understand how to use or access multiple classes in C#,

  • 0

I am trying to understand how to use or access multiple classes in C#, can someone explain to me what this code does?

public class Mammal : Animal
{   
    public Mammal(String name) : base(name) { }

    public override void Eat(Food food)
    {
        Console.WriteLine("Mammal \"" + Name + "\" eats " + food.Name);
    }
}

What is the purpose of public override void Eat(Food food)? I mean what does it do??

namespace ConsoleApplication1
{
    public class Food
    {
        private String name;

        Food(String name)
        {
            this.name = name;
        }

        public String Name
        {
            get 
            {
                return name;
            }
            set
            {
                name = value;
            }
        }   
    }

    public class Animal
    {
        private String name = "no name";

        public String Name {
            get 
            { 
                return name; 
            }
            set 
            { 
                name = value; 
            }
        }

        private Animal() { }

        public Animal(String name)
        {
            this.name = name;
        }

        public virtual void Eat(Food food)
        {
            Console.WriteLine("Animal \"" + Name + "\" eats " + food.Name);
        }

        public virtual void Born() 
        {
            Console.WriteLine("Animal \"" + Name + "\" is born");
        }

        public virtual void Die() 
        {
            Console.WriteLine("Animal \"" + Name + "\" is died");
        }
    }

    public class Mammal : Animal
    {   
        public Mammal(String name) : base(name) { }

        public override void Eat(Food food)
        {
            Console.WriteLine("Mammal \"" + Name + "\" eats " + food.Name);
        }

        public override void Born()
        {
            Console.WriteLine("Mammal \"" + Name + "\" is born");
        }

        public override void Die()
        {
            Console.WriteLine("Mammal \"" + Name + "\" is died");
        }

        public virtual void FedMilk(Mammal[] children)
        {
            for (Int32 i = 0; i < children.Length; i++)
            {
                Console.WriteLine("Mammal \"" + Name + "\" feds milk child \"" + children[i].Name + "\"");
            }
        }
    }

    public class Horse : Mammal
    {
        public Horse(String name) : base(name) { }

        public override void Eat(Food food)
        {
            Console.WriteLine("Horse \"" + Name + "\" eats " + food.Name);
        }

        public override void Born()
        {
            Console.WriteLine("Horse \"" + Name + "\" is born");
        }
    }
}
  • 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-11T21:26:14+00:00Added an answer on June 11, 2026 at 9:26 pm

    Ok,

    You have defined a basic class called mammal and then you created different types of mammals like Animal and then a specific animal (Horse ).

    So every mammal needs to eat, so that why you have created a functions called eat..

    But every animal which is a mammal eats the same things? NO!!!

    But every mammal needs to eats .

    So in that place the override attribute comes handy, it allows you to override the basic function of “eat” so you would be able to specified what eats every specific animal.

    So when you create a dog class you will override the eat function and specified whom eat some dog food.

    But because all of your specific animal are also animals you can refer to them as animals and print the eat function.

    lets say you want to see what eats every animal. you will create a loop of a list of animals and print the eat function.

    because you have override the eat function and specified every food. you will get the correct food for each animal.

    am i making my self clear?

    for example see this code

    List<Animal> animals = new List<Animal>();
    
                Horse hr = new Horse();
                Dog dg = new Dog();
                Bird br = new Bird();
    
                animals.Add(hr);
                animals.Add(dg);
                animals.Add(br);
    
                foreach(var animal in Animals)
                {
                    Console.WriteLine(animal.eat());
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand whether and under what circs one should use Python classes
I'm trying to understand amazon php sdk for AWS but I really can't use
I am trying to understand how to use the package mmap to access large
I'm trying to understand when to use an entity parent on GAE. Is this
I'm trying to use a mutex to protect access to some hardware from multiple
I am trying to see if you can use Microsoft Access Services, as available
I'm new to JavaScript and trying to understand the behavior of this code, which
I am learning Django and I am trying to understand the use of models.py
I'm trying to understand how to use firebug to debug my Javascript. So I
I am trying to understand how to use instance variable in Perl OO -

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.