O'Reilly Forums: Overriding Methods In Base Class - Chapter 6 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Overriding Methods In Base Class - Chapter 6

#1 User is offline   Stewie.N 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 15-March 09

Posted 21 March 2009 - 12:10 AM

CODE
public class Bird {
public void Fly() {
// code to fly
}
}

public class Pigeon : Bird {
public void Coo() { . . . }
}

public class Penguin : Bird {
public void Swim() { . . . }
}


I don't understanding why I need adding 'virtual' keyword and 'override' keyword to make Penguin Fly() override Bird Fly(). [page 226]

I think
CODE
public class Penguin: Bird {
public void Fly() {
MessageBox.Show(" Cant fly");
}
}


should override the Fly() in Bird class when I call penguin.fly because C # call the most specific method. In page 221, Dog's MakeNoise method is called instead of the MakeNoise method in Animal class.
0

#2 User is offline   johns311 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 03-August 09

Posted 03 August 2009 - 06:20 PM

QUOTE (Stewie.N @ Mar 21 2009, 12:10 AM) <{POST_SNAPBACK}>
CODE
public class Bird {
public void Fly() {
// code to fly
}
}

public class Pigeon : Bird {
public void Coo() { . . . }
}

public class Penguin : Bird {
public void Swim() { . . . }
}


I don't understanding why I need adding 'virtual' keyword and 'override' keyword to make Penguin Fly() override Bird Fly(). [page 226]

I think
CODE
public class Penguin: Bird {
public void Fly() {
MessageBox.Show(" Cant fly");
}
}


should override the Fly() in Bird class when I call penguin.fly because C # call the most specific method. In page 221, Dog's MakeNoise method is called instead of the MakeNoise method in Animal class.



I have been wondering the same thing. Did you ever get an answer?
0

#3 User is offline   johns311 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 03-August 09

Posted 03 August 2009 - 06:31 PM

I have been wondering the same thing. Like you said the pages before this said it would look in the immediate class and work its way up until it found the method (pg 221). Then it says you must use the "virtual" keyword in the base class and "override" in the subclass ( pg 227).

If this is not true, when would you use "virtual".

I think this forum would be a lot better if they would break the posts into a chapter hierarchy. Then I wouldn't have had to spend so much time looking for this post that has never been answered.
0

#4 User is offline   walbaloushi 

  • Active Member
  • PipPipPipPip
  • Group: Members
  • Posts: 100
  • Joined: 25-July 09
  • Gender:Male

Posted 03 August 2009 - 08:03 PM

The difference is that if you do something like this:

CODE
Bird penguin = new Penguin();
penguin.Fly();


you will get the Fly code from the base class Bird. But if you use virtual and override, the same code above will run the Penguin Fly method.
0

#5 User is offline   scyth3s 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 30-September 09

Posted 22 October 2009 - 05:41 PM

The two keywords are not necessary. However, if you do not use virtual and override, the IDE will give you a warning.

This warning means nothing, but being OCD, I cant stand to have any warnings, so I use the 2 keywords tongue.gif
0

#6 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 623
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 24 October 2009 - 05:58 AM

Virtual and override are definitely necessary if you want your inheritance to work the way you expect it to -- they're not just decorative, they really do change the way the software behaves. It's actually something that I've run into in real-life programming. In fact, it can lead to some pretty nasty bugs if you're not careful.

walbaloushi's right with the Penguin example. Let's say you've got Penguin inheriting from Bird, where Penguin.Fly() says "I don't fly!" and Bird.Fly() says "Flap, flap". And let's say you leave out the override keyword. If you create a new instance of Penguin like this:

Penguin penguin = new Penguin();
penguin.Fly();

it'll say "I don't fly!" just like you expect. But if you declare it like this:

Bird bird = new Penguin();
bird.Fly();

then it'll say, "Flap, flap". But if you put that override keyword back, it'll say "I don't fly!" like you'd expect. But you can only put that override keyword for Penguin.Fly() if you've marked Bird.Fly() virtual -- if you don't, your program won't build.

This may seem like crazy behavior, but it actually makes sense. Sometimes you'll want exactly this behavior, where your object behaves differently based on how it's declared and what context it's used in.

I've been meaning to do another C# blog post aimed at helping people learn C#. I'll give it some thought and see if I can come up with a good example that can help you explore this, since it's something that doesn't quite make sense until you see it.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#7 User is offline   scyth3s 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 30-September 09

Posted 27 October 2009 - 11:57 AM

Thats so badass! My wrong answer was used in part of the blog biggrin.gif
0

#8 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 623
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 27 October 2009 - 02:24 PM

QUOTE (scyth3s @ Oct 27 2009, 11:57 AM) <{POST_SNAPBACK}>
Thats so badass! My wrong answer was used in part of the blog biggrin.gif


One of my favorite quotes: http://despair.com/mis24x30prin.html

Seriously, though, your wrong answer will probably teach a whole bunch of people the right way to override methods. Don't be surprised if you see Skeptical Girl quoting you verbatim in the second edition!

For those of you who didn't see it, I gave an extended answer in a blog post on the O'Reilly C# page.

I hope this helps! I'll do a main topic post to make sure people see it.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#9 User is offline   scyth3s 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 30-September 09

Posted 02 November 2009 - 12:57 PM

QUOTE (AndrewStellman @ Oct 27 2009, 02:24 PM) <{POST_SNAPBACK}>
One of my favorite quotes: http://despair.com/mis24x30prin.html


I knew I was good for something!

Plus, having a quote in 2nd addition would be awesome. Id buy it just for that
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users