?
Solved

Inheritence questions

Posted on 2006-03-23
15
Medium Priority
?
169 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
0
Comment
Question by:paulwhelan
  • 7
  • 6
13 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16270804
In your NonFlyingBird class you need explicitly override the abstract method getFlyingInfo(), the syntax for overriding is:

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

Author Comment

by:paulwhelan
ID: 16270901
Doh!
I thought only virtual methods needed the override in the deriving classes?
Paul
0
 

Author Comment

by:paulwhelan
ID: 16270925
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16271052
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();
0
 

Author Comment

by:paulwhelan
ID: 16271184
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


0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16271226
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
0
 

Author Comment

by:paulwhelan
ID: 16271254
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();
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16271338
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


0
 

Author Comment

by:paulwhelan
ID: 16278693
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
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16278755
0
 

Author Comment

by:paulwhelan
ID: 16278806
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
0
 

Author Comment

by:paulwhelan
ID: 16278808
Increased points!
:)
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 200 total points
ID: 16282593
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
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
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 Month17 days, 9 hours left to enroll

829 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