Inheritence questions

Hi

I've a Bird class and two deriving classes
NonFlyingBird and FlyingBird

In the Bird class I do

public abstract bool getFlyingInfo();

I also have the field

protected bool wingsFunction;

Now in NonFlyingBird I do this

class NonFlyingBird : Bird
    {
        public NonFlyingBird()
        {
            wingsFunction = false;
        }

        public bool getFlyingInfo()
        {
            return this.wingsFunction;
        }

        //fields
        //private bool wingsFunction;

    }

However I get these errors

Warning      1      'InheritanceExample.NonFlyingBird.getFlyingInfo()' hides inherited member 'InheritanceExample.Bird.getFlyingInfo()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Error      2      'InheritanceExample.NonFlyingBird' does not implement inherited abstract member 'InheritanceExample.Bird.getFlyingInfo()'

Anyone any ideas?

Thanks
Paul
paulwhelanAsked:
Who is Participating?

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

x
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.

Ravi SinghSenior Software EngineerCommented:
In your NonFlyingBird class you need explicitly override the abstract method getFlyingInfo(), the syntax for overriding is:

public override bool getFlyingInfo()
{
     return this.wingsFunction;
}
paulwhelanAuthor Commented:
Doh!
I thought only virtual methods needed the override in the deriving classes?
Paul
paulwhelanAuthor Commented:
Also I do this

Mammal DerekTheDodo = new NonFlyingBird();
            textBox1.Text = DerekTheDodo.

yet when I hit '.' (where the intellisense comes up) I thought I would see getFlyingInfo but I dont.

I see
Equals
GetHashCode
getLifeExpectancy
GetType
saySomething
setLifeExpectancy
ToString

but no getFlyingInfo even though its within the NonFlyingBird class like this

public override bool getFlyingInfo()
        {
            return this.wingsFunction;
        }

Thanks
Paul
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Ravi SinghSenior Software EngineerCommented:
Hi, its because Mammal is higher up in the inheritance hierarchy, and doesn't contain the definition for getFlyingInfo()... if you try declaring the type as Bird then polymorphism comes into play and will call the correct getFlyingInfo method:

Bird DerekTheDodo =  new NonFlyingBird();
textBox1.Text = DerekTheDodo.getFlyingInfo().ToString();
paulwhelanAuthor Commented:
Cool that worked.

Bird DerekTheDodo =  new NonFlyingBird();

So why do I need to declare it as a Bird and not a Mammal?

Because Mammal doesn't contain a getFlyingInfo method?

Cheers


Ravi SinghSenior Software EngineerCommented:
Yes, if you need access to the getFlyingInfo method then you'll have to declare it as Bird (in which you can store NonFlyingBird and FlyingBird references) as its defined in that class
paulwhelanAuthor Commented:
And could I just declare it as

NonFlyingBird DerekTheDodo = new NionFlyingBird();

whats the point in declaring it as

Bird DerekTheDodo =  new NonFlyingBird();

or

Mammal DerekTheDodo =  new NonFlyingBird();
Ravi SinghSenior Software EngineerCommented:
yes you can declare it as:

NonFlyingBird DerekTheDodo = new NonFlyingBird();

If you declare the type as Bird, then you can store objects of type NonFlyingBird and FlyingBird in the Bird variable:

Bird nfb = new NonFlyingBird();

or

Bird fb = new FlyingBird();

so for example you could have an array of type Bird:

Bird[] birdArray = new Bird[2];
birdArray[0] = new NonFlyingBird();
birdArray[1] = new FlyingBird();

i.e. store derived types into the super type bird and you can call the getFlyingInfo() method on each element and still be sure that the correct getFlyingInfo method is called (the non flying birds or the flying birds)... one form of polymorphism


paulwhelanAuthor Commented:
The thing that I find hard to understand is that I always thought I instantiated a class like this

NonFlyingBird nfb = new NonFlyingBird();

or

FlyingBird fb = new FlyingBird();

I never knew you could mix and match =)

So Im allowed have the array of type Bird and within that place instantiations of Bird or anything that derives from it?

Thanks
Paul
Ravi SinghSenior Software EngineerCommented:
paulwhelanAuthor Commented:
Im having trouble with this too

I do this

            Bird nfb = new NonFlyingBird();
            textBox1.Text = "nfb canfly? " + nfb.getFlyingInfo().ToString();

            Bird fb = new FlyingBird();
            textBox2.Text = "fb canfly? " + fb.getFlyingInfo().ToString();

            textBox3.Text = "nfb le = " + nfb.getLifeExpectancy().ToString();
            textBox4.Text = "fb le = " + fb.getLifeExpectancy().ToString();

Now this line is the problem

            textBox3.Text = "nfb le = " + nfb.getLifeExpectancy().ToString();
           
Within the Bird class default constructor I say
lifeExpectancy = 5;
But within the NonFlyingBird class default constructor I say
lifeExpectancy = 1;
(all the land based predators ;) )

Anyway when I debug and step through the code it goes

textBox3.Text = "nfb le = " + nfb.getLifeExpectancy().ToString();

then into the Bird class

public override int getLifeExpectancy()
        {
            return lifeExpectancy;
        }

but when I hover over
return lifeExpectancy;
it says lifeExpectancy is 1

Now thats fine as I created it as
Bird nfb = new NonFlyingBird();

but surely it should say that lifeExpectancy is 5 within the Bird class?

Thanks
Paul
paulwhelanAuthor Commented:
Increased points!
:)
Ravi SinghSenior Software EngineerCommented:
Thats because when you instantiate the NonFlyingBird object, all constructors in the type hierarchy are called (so in this case, object's constructor gets called first, ..., then Bird's, then NonFlyingBird's).. and because lifeExpectancy is declared higher up in the hierarchy the Bird constructor first sets it to 5 and its finally set to 1 in the NonFlyingBird constructor... thats why your seeing a 1 for lifeExpectency (in Bird)... remember that the lifeExpectency variable is inherited down, so each 'class' does not contain its own copy of it - they can all modify it

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
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
C#

From novice to tech pro — start learning today.