Inheritance - instantiating

Posted on 2006-03-24
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!
Question by:paulwhelan
    LVL 10

    Accepted Solution

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

    Expert Comment

    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.
    LVL 44

    Expert Comment

    just out of curiosity - since when are Birds MAMMALS - are you defining a new type of biology?

    LVL 2

    Expert Comment

    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"

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now