We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Inheritance - instantiating

paulwhelan asked
Medium Priority
Last Modified: 2010-04-16
Ok I'm trying to get my head around this so bear with me.

I've an example of Inheritance which uses a base class Mammal
from that I have Human, Dog, Bird
from Bird I have NonFlyingBird and FlyingBird

so my question is when I instantiate.

So if I want to create a 'DerektheDodo' - an object that is a NonFlyingBird - how do I do it?

I'm getting confused between all of these ...

Mammal example1 = new Mammal();
Mammal example1 = new Bird();
Mammal example1 = new NonFlyingBird();
Bird example1 = new Bird();
NonFlyingBird example1 = new NonFlyingBird ();

Should I now just do
NonFlyingBird example1 = new NonFlyingBird ();

Will this have access to all the methods and fields etc that it inherits from (ie Bird and Mammal)?

Thanks for bearing with me!
Watch Question

So if I want to create a 'DerektheDodo' - an object that is a NonFlyingBird - how do I do it?

you just write:

NonFlyingBird DerektheDodo = new NonFlyingBird();

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

If you need one instance of the NonFlyingBird, use:
NonFlyingBird example1 = new NonFlyingBird ();

But if you need to keep array of objects, each of them can be Mammal, Bird, NonFlyingBird etc., you can declare Mammal[] array and assign Mammal, Bird or NonFlyingBird instances to it's members. Polymorphism is designed for this case.

Examples like Mammal example1 = new NonFlyingBird() are not practical, they are used only for demonstration of inheritance or polymorphism.
just out of curiosity - since when are Birds MAMMALS - are you defining a new type of biology?


If you are properly designing this taxonomy, Mammal should be an abstract base class.  Meaning you create the class with the keyword abstract in front of the name of the class.  Like this:

abstract public class Mammal
    public Mammal()

That way, you cannont directly instantiate a new 'Mammal', but can only instantiate a sub classes derived from that base class.  (If you ever see a creature with warm blood and hair (i.e. a mammal), what exactly is that creature? It is TYPE OF 'Mammal' but it is not itself an instantiation of 'Mammal'.  It is a dog, or a cat, or a human, etc. 'Mammal' is not a 'thing' you can hold in your hand or touch.  It is an abstract classification of more specialized objects. It is a concept.  So basically you have your 'Dog' class that inherits from 'Mammal' and you instantiate a new 'Dog' named 'Skippy'.  etc.  But there is no object in the universe that is itself a 'Mammal'.

To make a 'Dog' class that inherits from the abstract 'Mammal' class, use this:
public class Dog : Mammal
    public Dog()

The colon character after the word 'Dog' tells the compiler to inherit the stuff from 'Mammal' (or whatever class or interface you put there) into this class called 'Dog'.

Now you can instantiate a more specific creature in you main program code:

Dog Skippy = new Dog();

It sounds like you could use a good book to read!  There are many out there that explain this kind of Object Oriented Programming in pretty understandable language.  

"C# 2005 for Dummies" Has a whole section devoted to this kind of stuff.  It's consice, It's a quick read for what you need to know.  And it's cheap.

If you want more comprehensive information, try "Foundations of Object-Oriented Programming Using .NET 2.0 Patterns"
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.