Q. What is Difference between Overriding and Overloading ?
========================================================Ans:
Overloading
Method overloading happens in the same class shares the same method name but each method should have different number of parameters or parameters having different types and order.
Class myClass {
int Func(int x)
{
}
int Func(string s)
{
}
int Func(int x,string s) { }
}
Output
========================================================Ans:
Overloading
Method overloading happens in the same class shares the same method name but each method should have different number of parameters or parameters having different types and order.
Class myClass {
int Func(int x)
{
}
int Func(string s)
{
}
int Func(int x,string s) { }
}
Overriding
Method Overloading happens at compile time while Overriding happens at runtime. In method overloading, method call to its definition has happens at compile time while in method overriding, method call to its definition happens at runtime.
Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.
In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.
In base class if we declare methods with virtual keyword then only we can override those methods in derived class using override keyword
Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.
In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.
In base class if we declare methods with virtual keyword then only we can override those methods in derived class using override keyword
//Our Base Class
public class Baseclass
{
public virtual void Sample1()
{
Console.WriteLine(" My Base
Class");
}
}
// Our Derived Class
public class DerivedClass : Baseclass
{
public override void Sample1()
{
Console.WriteLine("My Derived
Class");
}
}
// Using base and derived class
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DerivedClass objDc = new DerivedClass ();
objDc.Sample1();
// calling the base class method
Baseclass objBc = new DerivedClass ();
objBc.Sample1();
}
}
Output
My Derived Class
My Derived Class
- In the case of performance, method overloading gives better performance when compared to overriding because the binding of overridden methods is being done at runtime.
- Static binding is happens when method overloaded while dynamic binding happens when method overriding.
- Method overloading add or extend more to the method functionality while method overloading is to change the existing functionality of the method.
- Static methods can be overloaded, that means a class can have more than one static method of same name. But static methods cannot be overridden, even if you declare a same static method in derived class it has nothing to do with the same method of base class.
No comments:
Post a Comment