Click here to Skip to main content
15,899,026 members
Home / Discussions / C#
   

C#

 
AnswerRe: Classes inheritance and something that confuses me PinPopular
Wayne Gaylard2-Jun-11 21:36
professionalWayne Gaylard2-Jun-11 21:36 
AnswerRe: Classes inheritance and something that confuses me Pin
Richard MacCutchan2-Jun-11 21:44
mveRichard MacCutchan2-Jun-11 21:44 
GeneralRe: Classes inheritance and something that confuses me Pin
nstk2-Jun-11 22:02
nstk2-Jun-11 22:02 
GeneralRe: Classes inheritance and something that confuses me Pin
Richard MacCutchan2-Jun-11 22:28
mveRichard MacCutchan2-Jun-11 22:28 
GeneralRe: Classes inheritance and something that confuses me Pin
MicroVirus3-Jun-11 1:40
MicroVirus3-Jun-11 1:40 
GeneralRe: Classes inheritance and something that confuses me Pin
Richard MacCutchan3-Jun-11 3:31
mveRichard MacCutchan3-Jun-11 3:31 
GeneralRe: Classes inheritance and something that confuses me Pin
nstk3-Jun-11 11:48
nstk3-Jun-11 11:48 
GeneralRe: Classes inheritance and something that confuses me Pin
BobJanova3-Jun-11 4:29
BobJanova3-Jun-11 4:29 
In short, such a declaration is useful because of polymorphism. Code which does not know about ContractEmployee can still call methods on it if they are overrides of virtual (or abstract or interface-defined) methods of a base class. For example consider the class hierarchy (another common OO tutorial one):

abstract class Animal {
 public abstract string Noise { get; }
 public virtual string Feed() { return "Mm, tasty."; }
}

class Dog : Animal {
 public override string Noise { get { return "Woof."} }
 public void GiveBone() { Console.WriteLine("Chomp."); }
}

class Cow : Animal {
 public override string Noise { get { return "Moo."; } }
 public override string Feed() { return "I'll only eat it if it's green."; }
}


Now, because the methods are virtual, polymorphism will ensure that the appropriate one is called when they are dispatched on an instance of Animal, and 'down-casting' to feed instances to a more generic (in the normal English sense of the word) method is appropriate:

void FeedAndListen(Animal animal){
 Console.WriteLine(animal.Feed());
 Console.WriteLine(animal.Noise);
 // You can't do this, because the animal type is not known
 // animal.GiveBone();
}

static void Main(){
 List<Animal> animals = new List<Animal>();
 animals.Add(new Dog());
 animals.Add(new Cow());
 foreach(Animal animal in animals) FeedAndListen(animal);
}


Note how FeedAndListen produces the output from the derived methods, even though the instances are 'downcasted' into a List<Animal> and passed to it as Animal, not their actual declaration class. Also note how you can put the different subclasses into the one list. Finally, see how Dog inherits the Feed behaviour because it is not overridden. And you can't call methods which are not defined on the base class.

I agree with the other comment which says that as you complete your OO training you will naturally see how these concepts work.
GeneralRe: Classes inheritance and something that confuses me Pin
nstk3-Jun-11 20:35
nstk3-Jun-11 20:35 
AnswerRe: Classes inheritance and something that confuses me Pin
Łukasz Nowakowski2-Jun-11 22:05
Łukasz Nowakowski2-Jun-11 22:05 
AnswerRe: Classes inheritance and something that confuses me Pin
_Erik_3-Jun-11 3:35
_Erik_3-Jun-11 3:35 
AnswerRe: Classes inheritance and something that confuses me Pin
Ravi Bhavnani3-Jun-11 5:48
professionalRavi Bhavnani3-Jun-11 5:48 
AnswerRe: Classes inheritance and something that confuses me Pin
Abhinav S3-Jun-11 21:23
Abhinav S3-Jun-11 21:23 
QuestionBasic Question On Constructor Stuff Pin
PozzaVecia2-Jun-11 10:10
PozzaVecia2-Jun-11 10:10 
AnswerRe: Basic Question On Constructor Stuff Pin
Ian Shlasko2-Jun-11 10:15
Ian Shlasko2-Jun-11 10:15 
GeneralRe: Basic Question On Constructor Stuff Pin
PozzaVecia2-Jun-11 10:21
PozzaVecia2-Jun-11 10:21 
AnswerRe: Basic Question On Constructor Stuff Pin
PIEBALDconsult2-Jun-11 16:23
mvePIEBALDconsult2-Jun-11 16:23 
GeneralRe: Basic Question On Constructor Stuff Pin
PozzaVecia3-Jun-11 11:10
PozzaVecia3-Jun-11 11:10 
AnswerRe: Basic Question On Constructor Stuff Pin
BobJanova3-Jun-11 4:34
BobJanova3-Jun-11 4:34 
QuestionGrid rendering problem in windows forms Pin
venomation2-Jun-11 5:38
venomation2-Jun-11 5:38 
GeneralRe: Grid rendering problem in windows forms Pin
thatraja2-Jun-11 8:21
professionalthatraja2-Jun-11 8:21 
AnswerRe: Grid rendering problem in windows forms Pin
Luc Pattyn2-Jun-11 8:47
sitebuilderLuc Pattyn2-Jun-11 8:47 
GeneralRe: Grid rendering problem in windows forms Pin
venomation2-Jun-11 9:28
venomation2-Jun-11 9:28 
AnswerRe: Grid rendering problem in windows forms Pin
Luc Pattyn2-Jun-11 9:36
sitebuilderLuc Pattyn2-Jun-11 9:36 
GeneralRe: Grid rendering problem in windows forms Pin
venomation2-Jun-11 9:57
venomation2-Jun-11 9:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.