{"id":9080,"date":"2025-03-06T13:21:46","date_gmt":"2025-03-06T13:21:46","guid":{"rendered":"http:\/\/localhost:8080\/csharp-test\/docs\/csharp\/polymorphism\/polymorphism-with-interfaces\/"},"modified":"2025-03-26T03:48:09","modified_gmt":"2025-03-26T03:48:09","slug":"polymorphism-with-interfaces","status":"publish","type":"docs","link":"https:\/\/www.completecsharptutorial.com\/docs\/csharp\/polymorphism\/polymorphism-with-interfaces\/","title":{"rendered":"Polymorphism with Interfaces"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"9080\" class=\"elementor elementor-9080\">\n\t\t\t\t<div class=\"elementor-element elementor-element-4d00a64 e-flex e-con-boxed e-con e-parent\" data-id=\"4d00a64\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-49c1147 elementor-widget elementor-widget-heading\" data-id=\"49c1147\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\">Mastering Polymorphism with Interfaces in C#<\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-d45d67a e-flex e-con-boxed e-con e-parent\" data-id=\"d45d67a\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-b02b985 elementor-widget elementor-widget-text-editor\" data-id=\"b02b985\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h4>\ud83d\ude80 Introduction<\/h4>\n<p>Hey there, future C# pro! \ud83c\udfaf Have you ever wondered how different classes can share a common behavior while implementing their own unique way of doing things? That\u2019s where <strong>Polymorphism with Interfaces in C#<\/strong> comes in!<\/p>\n<p>Imagine you\u2019re playing a video game. You press the same &#8220;Jump&#8221; button, but different characters (a human, a robot, or a bird) jump in their own unique ways. That\u2019s polymorphism in action! \ud83c\udfae<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-07ac832 e-flex e-con-boxed e-con e-parent\" data-id=\"07ac832\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-99421e9 elementor-widget elementor-widget-docly_alerts_box\" data-id=\"99421e9\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_alerts_box.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"alert media message_alert alert-info fade show\" role=\"alert\">\r\n                <i ><\/i>\r\n                <div class=\"media-body\">\r\n                                            <h5 class=\"title\"> \ud83d\udcda What You Are Going to Learn in This Lesson<\/h5>\r\n                                        <p class=\"alert\">\u2714\ufe0f What is <strong>Polymorphism with Interfaces in C#<\/strong>?<br \/>\u2714\ufe0f Why do we need it?<br \/>\u2714\ufe0f Syntax and explanation \ud83d\udcdd<br \/>\u2714\ufe0f Hands-on coding examples \ud83d\udcbb<br \/>\u2714\ufe0f A real-world example to make it crystal clear \ud83c\udf0e<br \/>\u2714\ufe0f Common mistakes and best practices \ud83c\udfc6<\/p>                                    <\/div>\r\n            <\/div>\r\n        \r\n        \r\n        \r\n        \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-0b9c1e8 e-flex e-con-boxed e-con e-parent\" data-id=\"0b9c1e8\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-0a84b3a elementor-widget elementor-widget-text-editor\" data-id=\"0a84b3a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3>\ud83e\uddd0 What is Polymorphism with Interfaces in C#?<\/h3><p>Polymorphism means <strong>&#8220;many forms.&#8221;<\/strong> When a class implements an interface, it provides its own version of the methods declared in that interface.<\/p><p>An interface only defines <strong>what<\/strong> needs to be done, but the class decides <strong>how<\/strong> to do it!<\/p><p>\u00a0<\/p><h3>\u2753 Why is Polymorphism with Interfaces in C# Important?<\/h3><p class=\"alert\">\u2705 Encourages <strong>code reusability<\/strong><br \/>\u2705 Supports <strong>multiple inheritance<\/strong> (since a class can implement multiple interfaces)<br \/>\u2705 Makes <strong>code flexible and scalable<\/strong><br \/>\u2705 Helps <strong>decouple<\/strong> code for better maintainability<\/p><h4>\u00a0<\/h4><h4>\ud83d\udccc Syntax of Interfaces in C#<\/h4>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ed9a722 elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"ed9a722\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism\" data-lng-type=\"csharp\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-csharp\">\r\n\t\t\t\t\tinterface IAnimal  \r\n{  \r\n    void MakeSound();  \/\/ Method without implementation\r\n}  \r\n\r\nclass Dog : IAnimal  \r\n{  \r\n    public void MakeSound()  \r\n    {  \r\n        Console.WriteLine(&quot;Dog says: Woof! \ud83d\udc36&quot;);  \r\n    }  \r\n}  \r\n\r\nclass Cat : IAnimal  \r\n{  \r\n    public void MakeSound()  \r\n    {  \r\n        Console.WriteLine(&quot;Cat says: Meow! \ud83d\udc31&quot;);  \r\n    }  \r\n}  \r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-aaf6392 e-flex e-con-boxed e-con e-parent\" data-id=\"aaf6392\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-fbb0bec elementor-widget elementor-widget-heading\" data-id=\"fbb0bec\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">\ud83d\udcbb Example 1: Basic Polymorphism with Interfaces<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c5630cd elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"c5630cd\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism\" data-lng-type=\"csharp\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-csharp\">\r\n\t\t\t\t\tusing System;\r\n\r\ninterface IShape\r\n{\r\n    void Draw();\r\n}\r\n\r\nclass Circle : IShape\r\n{\r\n    public void Draw()\r\n    {\r\n        Console.WriteLine(&quot;Drawing a Circle \ud83d\udfe2&quot;);\r\n    }\r\n}\r\n\r\nclass Square : IShape\r\n{\r\n    public void Draw()\r\n    {\r\n        Console.WriteLine(&quot;Drawing a Square \ud83d\udfe5&quot;);\r\n    }\r\n}\r\n\r\nclass Program\r\n{\r\n    static void Main()\r\n    {\r\n        IShape shape1 = new Circle();\r\n        IShape shape2 = new Square();\r\n\r\n        shape1.Draw();\r\n        shape2.Draw();\r\n    }\r\n}\r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3a23a78 elementor-widget elementor-widget-text-editor\" data-id=\"3a23a78\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5>\ud83c\udfc6 Output:<\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-45d3553 elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"45d3553\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism-dark\" data-lng-type=\"markup\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-markup\">\r\n\t\t\t\t\tDrawing a Circle \ud83d\udfe2  \r\nDrawing a Square \ud83d\udfe5  \r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1f56f10 elementor-widget elementor-widget-text-editor\" data-id=\"1f56f10\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h4>\ud83d\udd0d Explanation<\/h4><ul class=\"ordered-list steps\"><li><strong>IShape<\/strong> is an interface that defines a method <code>Draw()<\/code>.<\/li><li><strong>Circle<\/strong> and <strong>Square<\/strong> classes implement the interface and provide their own versions of <code>Draw()<\/code>.<\/li><li>In <code>Main()<\/code>, we use polymorphism by assigning objects of <code>Circle<\/code> and <code>Square<\/code> to the <code>IShape<\/code> type.<\/li><li>When we call <code>Draw()<\/code>, it executes the method specific to the actual object.<\/li><\/ul><h3>\u00a0<\/h3><h3>\ud83c\udf0d Real-World Example: Payment System<\/h3><p>Imagine an <strong>online shopping website<\/strong> \ud83d\uded2. Users can pay with a <strong>credit card, PayPal, or cryptocurrency<\/strong>. Each payment method follows the same process but has different implementations.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-688c78d e-flex e-con-boxed e-con e-parent\" data-id=\"688c78d\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-86f039e elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"86f039e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism\" data-lng-type=\"csharp\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-csharp\">\r\n\t\t\t\t\tusing System;\r\n\r\ninterface IPayment\r\n{\r\n    void ProcessPayment(double amount);\r\n}\r\n\r\nclass CreditCard : IPayment\r\n{\r\n    public void ProcessPayment(double amount)\r\n    {\r\n        Console.WriteLine($&quot;Processing Credit Card Payment of ${amount} \ud83d\udcb3&quot;);\r\n    }\r\n}\r\n\r\nclass PayPal : IPayment\r\n{\r\n    public void ProcessPayment(double amount)\r\n    {\r\n        Console.WriteLine($&quot;Processing PayPal Payment of ${amount} \ud83c\udd7f\ufe0f&quot;);\r\n    }\r\n}\r\n\r\nclass Bitcoin : IPayment\r\n{\r\n    public void ProcessPayment(double amount)\r\n    {\r\n        Console.WriteLine($&quot;Processing Bitcoin Payment of ${amount} \u20bf&quot;);\r\n    }\r\n}\r\n\r\nclass Program\r\n{\r\n    static void Main()\r\n    {\r\n        IPayment paymentMethod;\r\n\r\n        paymentMethod = new CreditCard();\r\n        paymentMethod.ProcessPayment(100);\r\n\r\n        paymentMethod = new PayPal();\r\n        paymentMethod.ProcessPayment(50);\r\n\r\n        paymentMethod = new Bitcoin();\r\n        paymentMethod.ProcessPayment(200);\r\n    }\r\n}\r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-51de4d0 elementor-widget elementor-widget-text-editor\" data-id=\"51de4d0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5>\ud83c\udfc6 Output:<\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-26f7cdb elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"26f7cdb\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism-dark\" data-lng-type=\"markup\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-markup\">\r\n\t\t\t\t\tProcessing Credit Card Payment of $100 \ud83d\udcb3  \r\nProcessing PayPal Payment of $50 \ud83c\udd7f\ufe0f  \r\nProcessing Bitcoin Payment of $200 \u20bf  \r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-8290ab5 e-flex e-con-boxed e-con e-parent\" data-id=\"8290ab5\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-b7e7fa7 elementor-widget elementor-widget-text-editor\" data-id=\"b7e7fa7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h4>\ud83d\udd0d Explanation<\/h4><ul class=\"ordered-list steps\"><li><code>IPayment<\/code> defines a <code>ProcessPayment()<\/code> method.<\/li><li><code>CreditCard<\/code>, <code>PayPal<\/code>, and <code>Bitcoin<\/code> implement <code>IPayment<\/code> in different ways.<\/li><li>In <code>Main()<\/code>, we assign different payment methods to the <code>IPayment<\/code> type and call <code>ProcessPayment()<\/code>.<\/li><li>The correct method runs based on the actual object.<\/li><\/ul><h3>\u00a0<\/h3><h3>\u26a0\ufe0f Common Mistakes and Best Practices<\/h3><p class=\"alert\">\u274c <strong>Mistake:<\/strong> Forgetting to implement all methods from an interface.<br \/>\u2705 <strong>Fix:<\/strong> Always provide implementations for all interface methods.<\/p><p class=\"alert\">\u274c <strong>Mistake:<\/strong> Trying to create an object of an interface.<br \/>\u2705 <strong>Fix:<\/strong> You <strong>cannot<\/strong> instantiate an interface directly. Use a class that implements it.<\/p><h3>\u00a0<\/h3><h3>\ud83c\udfaf Conclusion<\/h3><p>You did it! \ud83c\udf89 Now you understand <strong>Polymorphism with Interfaces in C#<\/strong> and why it\u2019s so powerful. Interfaces allow us to enforce a contract in different classes while enabling flexibility. Whether it\u2019s drawing shapes, processing payments, or handling game characters, interfaces make it easy to achieve <strong>polymorphism<\/strong>.<\/p><p>\ud83d\udc49 Keep practicing! Try creating your own interfaces and implementing them in different ways.<\/p><h3>\u00a0<\/h3><h3>\u23ed\ufe0f Next What?<\/h3><p>In the <strong>next chapter<\/strong>, you will learn <strong>Polymorphism with Abstract Classes in C#<\/strong>. It&#8217;s going to be another fun ride! Stay tuned. \ud83d\ude80<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Learn Polymorphism with Interfaces in C# with real-world examples, code, and explanations. Understand why it&#8217;s important and how to use it effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":6255,"menu_order":162,"comment_status":"open","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-9080","docs","type-docs","status-publish","hentry","no-post-thumbnail"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs\/9080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/comments?post=9080"}],"version-history":[{"count":4,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs\/9080\/revisions"}],"predecessor-version":[{"id":9087,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs\/9080\/revisions\/9087"}],"up":[{"embeddable":true,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs\/6255"}],"wp:attachment":[{"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/media?parent=9080"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/doc_tag?post=9080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}