Failed Scientist
Failed Scientist

Reputation: 2027

Polymorphism using switch statement

We have some document-types:

class Document
{
    public void virtual Print()
}

class PDF : Document
{
    public void override Print()
    {
        Console.WriteLine("PDF Printed");
    }
}

class Excel : Document
{
    public void override Print()
    {
        Console.WriteLine("Excel Printed");
    }
}

Suppose we have a list of documents (Document objects) and we call the virtual function Print() on all of them.

foreach(Document doc in DocumentsList)
{
    doc.Print();
}

I know Polymorphism is much sophisticated way of implementing it but can we really do the same using switch statement as well? I had a long argument with a fellow on it and he says it's possible. Is it?

Upvotes: 0

Views: 1638

Answers (4)

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16017

Yes, you can always use a switch (or an if-else chain), asking for the type. You can keep document type information in your object, for example as an enum, and use it in a switch.

Why you shouldn't do that? Because you have to extend all these switches when you add a new derived type.

To avoid that, you can implement something more automatic: Have a list of pointers to functions (or delegates in C#) which you fill for each single object with the functions suitable for this particular object (for example, the print delegate would point to print_pdf() for PDF files and to print_excel() for excel files). And hey, there you have hand-crafted a poor man's polymorphism.

The sweet thing about polymorphism is that the code using a polymorphic object can be type agnostic. It does not know what precise type the object actually has, and does not want to know. The precise type may not have existed back when the code handling the object was written. All the detail information is hidden within the type which is awesome for maintenance.

Upvotes: 5

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122228

Its a bit of a funny question, because the example using the switch is usually used to demonstrate how you usually dont want to do it.

Instead of using inheritance, you could have your document class like this:

class Doc {
    public: 
        enum DocType{PDF,EXCEL}
    private:
        DocType docType;
}

And then you simply use

switch (doc.getDocType()){
    ...etc...
}

However, there are several reasons why usually it is nicer to use inheritance. Imagine for example, you want to add another document type. Then not only you have to add another enum field, but (and here comes the big problem) you also would have to change every single switch statement in all code that is using your Document class. That is annoying and usually you do not want to do this.

Upvotes: 4

user3382570
user3382570

Reputation:

Yes you can, of course you can interrogate the member of the list on each iteration, and there are plenty of techniques that can achieve this (e.g attributes, internal Type field etc).

In fact everything you do in C# you can also do in Assembler! However I can hardly see any benefit of avoiding the advantages of OOP.

Upvotes: 5

JunaidKirkire
JunaidKirkire

Reputation: 918

Polymorphism is the way to go for this. If you choose switch statement, you would need to use Reflection or is keyword to figure out which object's Print() you want to invoke.

Upvotes: 0

Related Questions