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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:52:20+00:00 2026-05-28T07:52:20+00:00

Got a task to make a program that registers animals and the object is

  • 0

Got a task to make a program that registers animals and the object is to get familiar with inheritance, polymorphism and so on.

One thing that pussles me is no matter how much I read about it just seems pointless.

I create my main class which is animal with some generic fields that apply to all animals lets say name, age and species.

So far so good all animals has this info but every animal has a unique field aswell so ill create my cat as public class cat : animal and give the cat the field teeth for example.

Now I want to make a new animal which is a cat, im taking data from several listboxes so I would need a constructor that takes those parameters and this is what I dont get, do I have to declare them in every child class aswell?

I know that my animal should have 3 parameters from the animal class plus another from the cat class so the new cat should accept (name, age, species, teeth) but it seems that I have to tell the constructor in the cat class to accept all of these and theres my question, what purpose does the animal class serve? If I still need to write the code in all subclasses why have the base class? Probably me not getting it but the more I read the more confused I become.

  • 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-05-28T07:52:21+00:00Added an answer on May 28, 2026 at 7:52 am

    Like Sergey said, its not only about constructors. It saves you having to initialize the same fields over and over. For example,

    Without inheritance

    class Cat
    {
        float height;
        float weight;
        float energy;
        string breed;
    
        int somethingSpecificToCat;
    
        public Cat()
        {
            //your constructor. initialize all fields
        }
    
        public Eat()
        {
            energy++;
            weight++;
        }
    
        public Attack()
        {
            energy--;
            weight--;
        }   
    
    }
    
    class Dog
    {
        float height;
        float weight;
        float energy;
        string breed;
    
        int somethingSpecificToDog;
    
        public Dog()
        {
            //your constructor. initialize all fields
        }
    
        public Eat()
        {
            energy++;
            weight++;
        }
    
        public Attack()
        {
            energy--;
            weight--;
        }   
    
    }
    

    With Inheritance

    Everything common to animals gets moved to the base class. This way, when you want to setup a new animal, you don’t need to type it all out again.

    abstract class Animal
    {
        float height;
        float weight;
        float energy;
        string breed;
    
        public Eat()
        {
            energy++;
            weight++;
        }
    
        public Attack()
        {
            energy--;
            weight--;
        }   
    }
    class Cat : Animal
    {   
        int somethingSpecificToCat;
    
        public Cat()
        {
            //your constructor. initialize all fields
        }   
    }
    
    class Dog : Animal
    {   
        int somethingSpecificToDog;
    
        public Dog()
        {
            //your constructor. initialize all fields
        }   
    }
    

    Another advantage is, if you want to tag every animal with a unique ID, you don’t need to include that in each constructor and keep a global variable of the last ID used. You can easily do that in the Animal constructor since it will be invoked everytime the a derived class is instantiated.

    Example

    abstract class Animal
    {
        static int sID = 0;
    
        float height;
        float weight;
        int id;
    
        public Animal()
        {
            id = ++sID;
        }
    }
    

    Now when you do;

    Dog lassie = new Dog();  //gets ID = 1
    Cat garfield = new Cat(); // gets ID = 2
    

    If you want a list of all Animals in your ‘farm’,

    without inheritance

    List<Cat> cats = new List<Cat>();   //list of all cats
    List<Dog> dogs = new List<Dog>(); //list of all dogs
    ...etc
    

    With inheritance

    List<Animal> animals = new List<Animal>();  //maintain a single list with all animals
    animals.Add(lassie as Animal);
    animals.Add(garfield as Animal);
    

    This way, if you want to see if you have an animal called Pluto, you just need to iterate over a single list (animals) rather than multiple lists (Cats, Dogs, Pigs etc.)

    EDIT in response to your comment

    You don’t need to instantiate Animal. You simply create an object of whichever Animal you want to. In fact, since an Animal will never be a generic Animal, you can create Animal as an abstract class.

    abstract class Animal
    {
        float height;
        float weight;
        float energy;
        string breed;
    
        public Eat()
        {
            energy++;
            weight++;
        }
    
        public Attack()
        {
            energy--;
            weight--;
        }   
    }
    class Cat : Animal
    {   
        int somethingSpecificToCat;
    
        public Cat()
        {
            //your constructor. initialize all fields
        }   
    }
    
    class Dog : Animal
    {   
        int somethingSpecificToDog;
    
        public Dog()
        {
            //your constructor. initialize all fields
        }   
    }
    
    Cat garfield = new Cat();
    garfield.height = 24.5;
    garfield.weight = 999; //he's a fat cat
    //as you can see, you just instantiate the object garfield
    //and instantly have access to all members of Animal
    
    Animal jerry = new Animal(); //throws error
    //you cannot create an object of type Animal
    //since Animal is an abstract class. In this example
    //the right way would be to create a class Mouse deriving from animal and then doing
    
    Mouse jerry = new Mouse();
    

    Edit to your comment

    If you store it in a list of Animals, you still have access to all fields. You just have to cast it back to its original type.

    List<Animal> animals = new List<Animal>();
    animals.Add(garfield as Animal);
    animals.Add(lassie as Animal);
    
    //if you do not cast, you cannot access fields that were specific to the derived class.
    Console.WriteLine(animals[0].height);   //this is valid. Prints Garfield's height
    Console.WriteLine(animals[0].somethingSpecificToCat); //invalid since you haven't casted
    Console.WriteLine((animals[0] as Cat).somethingSpecificToCat); //now it is valid
    
    //if you want to do it in a loop
    
    foreach(Animal animal in animals)
    {
        //GetType() returns the derived class that the particular animal was casted FROM earlier
    
        if(animal is Cat)
        {
            //the animal is a cat
            Cat garfield = animal as Cat;
            garfield.height;
            garfield.somethingSpecificToCat;
        }
        else if (animal is Dog)
        {
            //animal is a dog
            Dog lassie = animal as Dog;
            lassie.height;
            lassie.somethingSpecificToDog;
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I got this task to make a program which will allow the user
I've got a common task that I do with some Activities - downloading data
I've got a ant build.xml that uses the <copy> task to copy a variety
Is there any handy tool that can make updating tables easier? Usually I got
I am trying to make make a task list for one of my projects.
I got the libraries that I needed to be recognized by the javac task,
So I've got a task that can be preformed by my GUI that will
I've got the task of making a car rental program, which uses linked lists
I got a task that makes me a little crazy, there is the section
So, I've got task from my boss to install opendocman on our newly installed

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.