• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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!
1 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();
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"
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now