Overloading | Overriding |
---|---|
Having same method name with different signatures. | Methods name and signatures must be same. |
Overloading is the concept of compile time polymorphism. | Overriding is the concept of runtime polymorphism. |
Two functions having same name and return type, but with different type and/or number of arguments is called Overloading. | When a function of base class is re-defined in the derived class called as Overriding. |
It doesn't need inheritance. | It needs inheritance. |
Method can have different data types. | Method should have same data type. |
Method can have different access specifier. | Method should be public. |
class abc { public void Disp(int a) { Console.WriteLine(a); } public void Disp(int a, int b) { Console.WriteLine("{0}{1}",a,b); } } class Program { static void Main(string[] args) { abc a1 = new abc(); a1.Disp(10) Disp(10, 20); } } |
public class MyBaseClass { public virtual void print1() { Console.WriteLine("Base imp"); } } public class MyDerivedClass: MyBaseClass { public override void print1() { Console.WriteLine("Derived imp"); } } class Program { static void Main(string[] args) { MyDerivedClass myobj = new MyDerivedClass(); MyBaseClass myBaseObj = myobj; myBaseObj.print1(); Console.ReadKey(); } } |
Differentiate between overloading and overriding in C#.
About Vipul Singh
Editor-in-Chief and Owner of TechBlogMU. Computer Engineer, Data Science Enthusiast and Tech Blogger. Catch him on Facebook and Twitter
Become a Patron: https://www.patreon.com/techblogmu
Subjects
- Artificial Intelligence (AI)
- ASP.NET with C#
- Big Data Analytics
- Computer Networks
- Cryptography and System Security
- Data Structures
- Data Warehouse and Mining
- Database Management System
- Digital Signal Processing
- Human Machine Interaction
- Machine Learning
- Microprocessor
- Object Oriented Programming Methodolgy (OOPM)
- Operating System
- Parallel and Distributed Database
- Software Architecture (SA)
- Statistics
- Structured and Object Oriented Analysis and Design (SOOAD)
- System Programming and Compiler Construction (SPCC)
- Theory of Computation