Overloading: is the mechanism to have more than one
method with same name but with different signature (parameters). A
method can be overloaded on the basis of following properties
Overriding: Overriding can be done in derived class, an override method provides a new implementation of a method inherited from parent class.
To override a method in base (parent) class it must be
- Have different number of parameter
- Having same number of parameters but of different type
- Having same number and type of parameters but in different orders
- Different return type
- Different access modifier
- Normal and optional parameters
public List<Customer> FindCustomer(String customer_name)
{
// your code
}
public List<Customer> FindCustomer(Int32 customer_Id)
{
// your code
}
public List<Customer> FindCustomer(String customer_name, String city)
{
// your code
}
public List<Customer> FindCustomer(String customer_name, Int32 customer_Id)
{
// your code
}
public List<Customer> FindCustomer(String customer_name, String city, String zip)
{
// your code
}
Following are invalid overloading because we can not overload on the basis of different return typepublic List<Customer> FindCustomer(String customer_name)
{
// your code
}
public Customer FindCustomer(String customer_name)
{
// your code
}
Following are also invalid overloading, because we cannot overload on the basis of access modifierpublic List<Customer> FindCustomer(String customer_name)
{
// your code
}
private List<Customer> FindCustomer(String customer_name)
{
// your code
}
Following are also not valid, because we cannot overload normal and optional parameterpublic List<Customer> FindCustomer(String customer_name)
{
// your code
}
private List<Customer> FindCustomer(String customer_name = String.Empty)
{
// your code
}
In the same way we can overload constructor of a class by defining
more than one constructor with different parameters which is known as
constructor overloading.Overriding: Overriding can be done in derived class, an override method provides a new implementation of a method inherited from parent class.
To override a method in base (parent) class it must be
- virtual
- abstract
- override
- non-virtual
- static
- new
- static
- virtual
- abstract
public class Square {
public double side;
// Constructor:
public Square(double side)
{ this.side = x; }
public virtual double Area()
{ return side * side ; }
}
Note Area method is defined with virtual so it can be overridden in
derived class, now lets derive a class cube from square and override
it's Area methodclass Cube: Square {
// Constructor:
public Cube(double side): base(x) { }
// Calling the Area base method:
public override double Area()
{ return (6*(base.Area())); }
}
Conclusion: - Overloading can be done in same class
- Overriding can be done in parent and derived class
- Overloading in used when we need same method in same class with different parameters
- Overriding is used when we need to redefine a method that has already been defined in parent class (using the exact same signature
- Overloading is resolved at compile time
- Overriding is resolved at run time
No comments:
Post a Comment