What is the Difference between Overriding and overloading?
Answer Posted / aparna murthy
overloading is the ability to define several methods with
the same name ,provided each method has a different signature.
example:--
class shape
{
public void area(double r)
{
console.writeline("Area of circle :"+(3.14*r*r));
}
public void area(double l,double b)
{
console.writeline("Area of rectangle :"+(l*b));
}
public void area(int b,int h)
{
console.writeline("Area of rectangle :"+(0.5*b*h));
}
}
class program
{
static void main(string[]args)
{
shape s=new shape()
s.area(7,6)
s.area(22,11);
s.area(12,20);
console.read();
}
}
try this example.....
signature of a method is the datatype of the
parameters,number of parameters and the sequence in which
they are passed to the function .
overriding:---
In method overriding method have same names same signatures
same return types but in different scopes(classes) in
hierarchy .c# uses virtual and override keywords to define
method overriding .Method overriding enables object to
resolve method invocation at runtime.we call it late binding
behavior of object.
1.An override declaration cannot change the accesibility of
vitual method .Both the override method and the virtual
method must have the same access level modifier.
2.Methods which are to be overriden in the subsequent
derived class are made virtual or abstract in base class.
example:--
class employee
{
public virtual double calculatesalary()
{
return basic+hra+da;
}
}
class manager:employee
{
public override double calculatesalary()
{
return basic+hra+da+allowances;
}
}
static void main(string[] args)
{
employee mgr=new manager();
double salary=mgr.calculatesalary();
console.writeline(salary);
console.readline();
}
| Is This Answer Correct ? | 2 Yes | 2 No |
Post New Answer View All Answers
How to execute VB.NET PROJECTS,VB6.0 PROJECTS AND write their test cases.Need Reply Urgently
What are the advantages of vb.net and c#?
Name some different types of control?
Write program in VB.Net with SQL Server and Crystal Reports to develop a small windows application to add,edit,save, search and print Employee Information and send sourcecode as zip file. empcode : .............. empname : .............. dateofjoin : dd/mm/yyyy dateofbirth : dd/mm/yyyy TableName: EmpMaster EmpCode EmpName DOB DOJ TableName: EmpDocs EmpCode DocNo DocName ExpDate
Explain how the .net framework performs automatic memory management.
Explain some of the exclusive features which are present in vb?
Write a program that would find the nth term of a geometric progression, given the value of first term and common ratio. Any inbuilt function that the language provides for calculating exponent should not be used.
What is stack used for in vb. Net?
Explain the use of option explicit?
How can I extract the formated word(bold,italic,underline,font,color etc) from the msword file.?
What do you mean by Redim in VB.NET?
how to get dynamic control array position or its index position?
Explain strong name in .net assembly?
Can you please explain the difference between dataset and datareader?
What is the differences between dataset.clone and dataset.copy?