Click here to Skip to main content
15,905,028 members
Home / Discussions / C#
   

C#

 
AnswerRe: Inheritance/Polymorphism Pin
Not Active4-Feb-10 20:07
mentorNot Active4-Feb-10 20:07 
GeneralRe: Inheritance/Polymorphism Pin
Sundeepan Sen4-Feb-10 20:28
Sundeepan Sen4-Feb-10 20:28 
AnswerRe: Inheritance/Polymorphism Pin
Wamuti4-Feb-10 21:58
Wamuti4-Feb-10 21:58 
AnswerRe: Inheritance/Polymorphism Pin
PIEBALDconsult5-Feb-10 3:19
mvePIEBALDconsult5-Feb-10 3:19 
GeneralRe: Inheritance/Polymorphism Pin
Sundeepan Sen5-Feb-10 7:04
Sundeepan Sen5-Feb-10 7:04 
GeneralRe: Inheritance/Polymorphism Pin
PIEBALDconsult5-Feb-10 7:50
mvePIEBALDconsult5-Feb-10 7:50 
GeneralRe: Inheritance/Polymorphism Pin
Sundeepan Sen5-Feb-10 9:24
Sundeepan Sen5-Feb-10 9:24 
AnswerRe: Inheritance/Polymorphism Pin
Keith Barrow5-Feb-10 5:23
professionalKeith Barrow5-Feb-10 5:23 
I actually almost say the contrary is true.

Mark Nischalke's answer holds true, you do have access to both the Moo() and Foo() methods by passing the subclass. However to do so couples the method to the subclass, and other instances of the animal class cannot be passed:
class Program
{
    static void MakeAnimalSpeak(Animal animal)
    {
        
        animal.MakeNoise();
    }

    static void MakeAnimalSpeakCoupled(Cow animal)
    {
        // This is more tighltly coupled to cow: 
        // this method cannot be called for duck
        animal.MakeNoise();
    }


    static void Milk(Cow animal)
    {
        animal.Milk();
        animal.MakeNoise(); // Cow is mooing with happiness
    }



    static void Main(string[] args)
    {
        Cow cow = new Cow();
        Duck duck = new Duck();
        MakeAnimalSpeak(cow);
        MakeAnimalSpeak(duck);
        MakeAnimalSpeakCoupled(cow);
        //MakeAnimalSpeakCoupled(duck); //Coupled to cow: this is a worse implementation in this context
        Milk(cow);
        //Milk(duck); //Can't, but not a problem. Milking a duck is nonsensical!
    }
}



abstract class Animal
{   
    public abstract void MakeNoise();  
}

class Cow : Animal
{
    public override void MakeNoise() { Console.WriteLine("Moo"); }
    public void Milk() { Console.Write("Sloooosh"); }
}

class Duck : Animal
{
    public override void MakeNoise() { Console.WriteLine("Quack"); }    
}


Of course it is horses for courses, as with everythign in the code world. In the example Mark gave, you should pass the cow if you need access to both Moo() and Foo() but otherwise IMO is is better to pass an Animal instead. Better yet, coding to Intefraces produces cleaner results still, and helps with testing (as mock objects can be passed more easily)

Cpianism: I have a negative number in my Rep so please fix it.
Chris Maunder: That isn't a bug.

GeneralRe: Inheritance/Polymorphism Pin
Sundeepan Sen5-Feb-10 10:05
Sundeepan Sen5-Feb-10 10:05 
QuestionWindowsAPICodePack GlassForm Question Pin
harsimranb4-Feb-10 17:06
harsimranb4-Feb-10 17:06 
AnswerRe: WindowsAPICodePack GlassForm Question Pin
#realJSOP5-Feb-10 0:17
professional#realJSOP5-Feb-10 0:17 
GeneralRe: WindowsAPICodePack GlassForm Question Pin
harsimranb5-Feb-10 9:14
harsimranb5-Feb-10 9:14 
GeneralRe: WindowsAPICodePack GlassForm Question Pin
#realJSOP5-Feb-10 9:37
professional#realJSOP5-Feb-10 9:37 
GeneralRe: WindowsAPICodePack GlassForm Question Pin
harsimranb5-Feb-10 14:45
harsimranb5-Feb-10 14:45 
GeneralRe: WindowsAPICodePack GlassForm Question Pin
#realJSOP5-Feb-10 22:28
professional#realJSOP5-Feb-10 22:28 
QuestionHow would i get the current Index ID of the currently displayed record Pin
tonyonlinux4-Feb-10 15:51
tonyonlinux4-Feb-10 15:51 
AnswerRe: How would i get the current Index ID of the currently displayed record Pin
PIEBALDconsult4-Feb-10 16:39
mvePIEBALDconsult4-Feb-10 16:39 
QuestionUsing objects written in C++ Pin
irc07a4-Feb-10 10:38
irc07a4-Feb-10 10:38 
AnswerRe: Using objects written in C++ Pin
AspDotNetDev4-Feb-10 11:35
protectorAspDotNetDev4-Feb-10 11:35 
QuestionUnable to start the remoting service Pin
indian1434-Feb-10 8:31
indian1434-Feb-10 8:31 
AnswerRe: Unable to start the remoting service Pin
Super Lloyd4-Feb-10 16:46
Super Lloyd4-Feb-10 16:46 
QuestionFileSystemWatcher doesn't notice files downloaded to the watched directory via IE? Pin
SCADirector4-Feb-10 8:25
SCADirector4-Feb-10 8:25 
AnswerRe: FileSystemWatcher doesn't notice files downloaded to the watched directory via IE? Pin
AspDotNetDev4-Feb-10 11:43
protectorAspDotNetDev4-Feb-10 11:43 
GeneralRe: FileSystemWatcher doesn't notice files downloaded to the watched directory via IE? Pin
SCADirector4-Feb-10 13:51
SCADirector4-Feb-10 13:51 
AnswerRe: FileSystemWatcher doesn't notice files downloaded to the watched directory via IE? Pin
Dave Kreskowiak4-Feb-10 15:14
mveDave Kreskowiak4-Feb-10 15:14 

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.