Learn how to a build a cloud-first strategyRegister Now


Inheritance - instantiating

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

Accepted Solution

dkloeck earned 300 total points
ID: 16278878
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

ID: 16278886
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

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


Expert Comment

ID: 16327142
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 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