Inheritance - instantiating

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!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.