• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Oops » What is Polymorphism in C#
Next →
← Prev

What is Polymorphism in C#

By Dinesh Thakur

Method overriding means same name methods in different classes.

Polymorphism is a feature to use one name in many forms. There ·Polymorphism can be achieved in following ways in C#:

• Method Overloading
• Method Overriding
• Method Hiding

Method overloading, means multiple methods of same name with different arguments in a single class or program. Method overriding and hiding makes use of the following three method-head keywords

• new
• virtual,
• override

The main difference between hiding and overriding relates to the choice of which method to call where the declared class of a variable is different to· the run-time class of the object it references. This point is explained further below.

Method Overriding

Suppose that we define a Square class which inherits from a Rectangle class (a square being a special case of a rectangle). Each of these classes also specifies a ‘getarea’ instance method, returning the area of the given instance.

For the Square class to ‘override’ the Rectangle class’ getArea method, the Rectangle class’ method must have first declared that it is happy to be overridden. One way in which it can do this is with the ‘virtual’ keyword. So, for instance, the Rectangle class’ getArea method might be specified like this:

publicvirtualdouble getArea(){return length * width;}

To override this method the Square class would then specify the overriding method with the ‘override’ keyword. For example: 

publicoverridedouble getArea(){return length * width;}

Note that for one method to override another, the overridden method must not be static, and it must be declared as either ‘virtual’, ‘abstract’ or ‘override’. Furthermore, the access modifiers for each method must be the same.

The major implication of the specifications above is that if we construct a new Square instance and then call its ‘getArea’ method, the method actually called will be the Square instance’s getArea method. So, for instance, if we run the following code: 

square sq =new Square(S);

double area = sq.getArea();

then the getArea method called on the second line will be the method defined in the Square class.

There is, however, a more subtle point. To show this, suppose that we declare two variables in the following way:

Square sq =new square(4);

Rectangle r = sq;

Here variable r refers to sq as a Rectangle instance (possible because the Square class· derives from the Rectangle class). We can now raise the question: if we run the following code 

double area = r.getArea();

then which getArea method is actually called – the Square class method or the Rectangle class method.

The answer in this case is that the Square class method would still be called. Because the Square class’ getArea method ‘overrides’ the corresponding method in the Rectangle class, calls to this method· on a Square instance always ‘slide through’ to the overriding method.

Method Overriding

using system;

class demo {

   class A {

       publicvirtualvoid show(){

               console.writeLine(“I am A“);

       }

    }

   class B:A {

        publicoverridevoid show(){

               console.writeLine(“I am B“);

        }

    }

    class C:A {

        publicoverridevoid show(){

               console.writeLine(“I am c“);

        }

     }

        publicstaticvoid Main(){

              A o;

              o =new B();

              o.show();

              o =new c();

              o.show();

         }

}

OUTPUT:

I am B

I am c

Method hiding

Where one method ‘hides’ another, the hidden method does not need to be declared with any special keyword. Instead, the hiding method just declares itself as ‘new’, So, where the Square class hides the Rectangle class’ getArea method, the two methods might just be written thus:

publicdouble getArea()// in Rectangle {

     return length * width;

}}

publicnewdouble getArea()// in Square {

    return length.* length;

}}

Note that a method can ‘hide’ another one without the access modifiers of these methods being the same. So, for instance, the Square’s getArea method could be declared as private, viz:

Private newdouble getArea(){

      return length * length;

}

This leads us to an important point. A ‘new’ method only hides a super-class method with a scope defined by its access modifier. Specifically, where the access level of the hiding method is ‘private’, as in the method just described, this method only hides the super-class method for the particular class in which it is defined.

To make this point more concrete, suppose that we introduced a further class, SpecialSquare, which inherits from Square. Suppose further that SpecialSquare does not overwrite the getArea method. In this case, because Square’s getArea method is defined as private, SpecialSquare inherits its getArea method directly from the Rectangle class (where the getArea method is public).

The final point to note about method hiding is that method calls do not always ‘slide through’ in the way that they do with virtual methods. So, if we declare two variables thus:

Square sq =new square(4);

Rectangle r = sq;

then run the code double area = r.getArea();

the get Area method run will be that defined in the Rectangle class, class, not the Square class.

Method hiding

using System;

class demo {

    class A {

         public virtual void show() {

              console.writeLine(“1 am A”);

         }}

    class B:A {

         public new void show() {

               console.writeLine(“I am B“);

        }}

    class B:A {

        public new void show() {

               console.writeLine(“I am e“);

       }}

       public static void Main() {

           A o;

           o = new B();

           o.show();

           o = new e();

           o.show();

   }

}

OUTPUT:

I am A

I am A

You’ll also like:

  1. What is Polymorphism?
  2. Polymorphism in C++
  3. What is Polymorphism in java? With Example
  4. What is Overloading or Compile Time Polymorphism
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW