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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:10:04+00:00 2026-05-18T22:10:04+00:00

Bit of a basic question I guess, but if I want an object to

  • 0

Bit of a basic question I guess, but if I want an object to own another object of type either A or B, how can the application using the object access the specific attributes? E.g.

public abstract class Animal
{
     private int Age;
     // Get Set
}

public class Tiger: Animal
{
     private int NoStripes;
     // Get Set
}

public class Lion : Animal
{
     private bool HasMane;
     // Get Set
}

public class Zoo
{
     private Animal animal;
     // Get Set
}

public static void Main()
{
     Zoo zoo = new Zoo();
     zoo.animal = new Tiger();

     // want to set Tiger.NoStripes
}
  • 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-18T22:10:05+00:00Added an answer on May 18, 2026 at 10:10 pm

    This is the gist of inheritance and polymorphism.

    If you are able to determine that an instance of Animal is in fact an instance of Tiger, you can cast it:

    ((Tiger)zoo.Animal).NoStripes = 1;
    

    However, if you try do do this on an instance of Animal that isn’t a Tiger, you’ll get a runtime exception.

    for example:

    Zoo zoo = new Zoo();
    zoo.animal = new Tiger();
    ((Tiger)zoo.Animal).NoStripes = 1; //Works Fine
    
    ((Lion)zoo.Animal).NoStripes = 1; //!Boom - The compiler allows this, but at runtime it will fail.
    

    There is an alternative casting syntax that using the “as” keyword, which returns null, instead of an exception if the cast fails. This sounds great, but in practice you’re likely to get subtle bugs later on when the null object gets consumed.

    Tiger temp = zoo.Animal as Tiger; //This will not throw an exception
    temp.NoStripes = 1; //This however, could throw a null reference exception - harder to debug
    zoo.Animal = temp;
    

    To avoid the null reference exception, you can null check of course

    Tiger temp = zoo.Animal as Tiger; //This will not throw an exception
    if (temp != null)
    {
        temp.NoStripes = 1; //This however, could throw a null reference exception - harder to debug
        zoo.Animal = temp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this maybe a very basic question but I'm having a bit of
A bit of a neophyte haskell question, but I came across this example in
This is a bit of a long shot, but if anyone can figure it
This might sound a bit of an odd question but I know what I
A bit of background first: I am using base code from a remote SVN
This question is related to (but perhaps not quite the same as): Does Django
I know this is not a very technical question, but it's for all technical
I'm sorry this question is going to be a bit vague because I'm not
I want port some my Python scripts into web apps so that others can
Bit of an obscure one this. My setup is all running on my local

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.