We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Inheritence questions

paulwhelan
paulwhelan asked
on
Medium Priority
186 Views
Last Modified: 2011-09-20
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
Comment
Watch Question

Ravi SinghSenior Software Engineer

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

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

Author

Commented:
Doh!
I thought only virtual methods needed the override in the deriving classes?
Paul

Author

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
Ravi SinghSenior Software Engineer

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

Author

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 Engineer

Commented:
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

Author

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 Engineer

Commented:
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


Author

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 Engineer

Commented:

Author

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

Author

Commented:
Increased points!
:)
Senior Software Engineer
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.