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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:44:15+00:00 2026-05-17T14:44:15+00:00

Suppose we have declared these two classes: public class Animal { //….. } public

  • 0

Suppose we have declared these two classes:

public class Animal
{
    //.....
}

public class Dog : Animal
{
    //.....
}

Well, my question is: why below line of code is valid?

Animal animal = new Dog();

EDIT:

In e-book, “Professional C# 2008”, there is a paragraph that says:

It’s always safe to store a derived type within a base class reference.

  • 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-17T14:44:16+00:00Added an answer on May 17, 2026 at 2:44 pm

    You are creating a new Dog, but then you are treating (using) it as an Animal.

    This is especially useful when you want to have some behaviour that is owned or exposed by the Animal, but the Dog may then override that with something different. The Dog can be used as either a Dog or as an Animal, because it is both.

    Edit: here is a quick example, and i’m going to use an abstract base class so you can see the real value of this arrangement:

    public abstract class Animal
    {
        public abstract void Move();
    
        public virtual void MakeSignatureSound() 
        {
            Console.WriteLine("Ugggg");
        }
    }
    
    public class Dog : Animal 
    {
        public override void Move() 
        {
            RunLikeAPuppy();
        }
    
        public override void MakeSignatureSound()
        {
            Console.WriteLine("Woof");
        }
    }
    
    public class CaveMan : Animal
    {
        public override void Move() 
        {
            RunLikeANeanderthal();
        }
    }
    
    public class Cat : Animal
    {
        public override void Move() 
        {
            RunLikeAKitteh();
        }
    
        public override void MakeSignatureSound()
        {
            Console.WriteLine("Meioww");
        }
    }
    

    Notice two things here:

    • all three derivatives from the Animal class had to override the Move() function, because in my base class i made the decision that all animals should have a Move(), but i didn’t specify how they should move – that is up to the individual animal to specify
    • the CaveMan class didn’t override the MakeSignatureSound() function, because it had already been defined in the base class and it was adequate for him

    Now if i do this:

    Animal caveman = new CaveMan();
    Animal dog = new Dog();
    caveman.MakeSignatureSound();
    dog.MakeSignatureSound();
    

    i will get this on the console:

    Ugggg
    Woof
    

    But because i used an abstract base class, i cannot instantiate an instance of it directly, i can’t do this:

    Animal animal = new Animal();
    

    As a developer, i want to ensure that when others (or myself) create a new Animal, it has to be a specific type, not just a pure Animal that had no behavior or characteristics.

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

Sidebar

Related Questions

No related questions found

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.