Monday, December 29, 2014
Sunday, December 28, 2014
Method Overloading
public static void Calculate(int a, double b)
{
Console.WriteLine("int,double");
}
public static void Calculate(double a, int b)
{
Console.WriteLine("double,int");
}
static void Main(string[] args)
{
/// Error-The best overloaded method match for
'.Calculate(double, int)' has some invalid arguments.
/// Error-Argument 2: cannot convert from 'double' to 'float'
///Calculate(2.5, 2.5);/// Invalid
///Error-The call is ambiguous between the following methods
or properties: 'Calculate(int, double)' and 'Calculate(double, int)'
///Calculate(2, 2);///
Invalid
Calculate(2, 2.5);/// Valid
Calculate(2, 2.5f);/// Valid
Calculate(2.5, 2);/// Valid
Calculate(2.5f, 2);/// Valid
}Output
int,double
int,double
double,int
double,int
Overriding Vs Shadowing
Overriding
Method overriding is an important feature of OOP that allows us to re-write a base class function or method with a different definition. Overriding is also known as “Dynamic polymorphism” because overriding is resolved at runtime. Here the signature of the method or function must be the same. In other words both methods (base class method and child class method) have the same name, same number and same type of parameter in the same order with the same return type. The overridden base method must be virtual, abstract or override.
Example
A method cannot be overriden if:
A method or function of the base class is available to the child (derived) class without the use of the "overriding" keyword. The compiler hides the function or method of the base class. This concept is known as shadowing or method hiding. In the shadowing or method hiding, the child (derived) class has its own version of the function, the same function is also available in the base class.
Example
Test Code
Output

If we do not use the new keyword the compiler generates the warning:

Mixing Method (Overriding and shadowing (Method Hiding))
We can also use shadowing and method overriding together using the virtual and new keywords. This is useful when we want to further override a method of the child (derived) class.
Example
Shadowing Vs Overriding
Shadowing Example
Output

In overriding, the base class can be accessed using the child object's overridden method.
Overriding Example
Output

We cannot use the new and override keywords together. If you do then the compiler throws a compilation error.
Output
http://www.c-sharpcorner.com/UploadFile/ff2f08/overriding-vs-shadowing-in-C-Sharp/
http://msdn.microsoft.com/en-us/library/ms172785.aspx
Method overriding is an important feature of OOP that allows us to re-write a base class function or method with a different definition. Overriding is also known as “Dynamic polymorphism” because overriding is resolved at runtime. Here the signature of the method or function must be the same. In other words both methods (base class method and child class method) have the same name, same number and same type of parameter in the same order with the same return type. The overridden base method must be virtual, abstract or override.
Example
- public class BaseClass
- {
- public virtual string GetMethodOwnerName()
- {
- return “Base Class”;
- }
- }
- public class ChildClass : BaseClass
- {
- public override string GetMethodOwnerName()
- {
- return “Child Class”;
- }
- }
- Methods have a different return type
- Methods have a different access modifier
- Methods have a different parameter type or order
- Methods are non virtual or static
A method or function of the base class is available to the child (derived) class without the use of the "overriding" keyword. The compiler hides the function or method of the base class. This concept is known as shadowing or method hiding. In the shadowing or method hiding, the child (derived) class has its own version of the function, the same function is also available in the base class.
Example
- Public class BaseClass
- {
- public string GetMethodOwnerName()
- {
- return "Base Class";
- }
- }
- public class ChildClass : BaseClass
- {
- public new string GetMethodOwnerName()
- {
- return "ChildClass";
- }
- }
- static void Main(string[] args)
- {
- ChildClass c = new ChildClass();
- Console.WriteLine(c.GetMethodOwnerName());
- }

If we do not use the new keyword the compiler generates the warning:

Mixing Method (Overriding and shadowing (Method Hiding))
We can also use shadowing and method overriding together using the virtual and new keywords. This is useful when we want to further override a method of the child (derived) class.
Example
- public class BaseClass
- {
- public virtual string GetMethodOwnerName()
- {
- return "Base Class";
- }
- }
- public class ChildClass : BaseClass
- {
- public new virtual string GetMethodOwnerName()
- {
- return "ChildClass";
- }
- }
- public class SecondChild : ChildClass
- {
- public override virtual string GetMethodOwnerName()
- {
- return "Second level Child";
- }
- }
| Shadowing | Overriding |
| Shadowing is a VB.Net concept. It also known as method hiding in C#. Using this concept we can provide a new implementation for the base class method without overriding it. | Overriding allows us to re-write a base class function with a different definition. |
| Using the “new” keyword we can do the shadowing or method hiding. | C# uses the virtual/abstract and override keyword for method overriding. |
| Shadowing redefines an entire method or function. | Overriding redefines only the implementation of a method or function. |
| Showing is used to protect against subsequent base class modification. | Overriding does polymorphism by defining a different implementation. |
| We can change the access modifier. | We cannot change the access modifier. The access modifier must be the same as in the base class method or function. |
| There is no control of a base class on shadowing. In other words, a base class element cannot enforce or stop shadowing. | The base class has some control over the overriding. Using the keyword abstract, the base class forces the child (derived) class to implement the function or method. |
| Shadowing an element (function method or property) can be inherited further in a child (derived) class. The shadowed element is still hidden. | The same as shadowing, overriding an element is inherited further in a derived class and the overridden element is still overridden. |
| In shadowing, the signature of an element could be different. | In overriding, the signature of the element must be the same. |
| In shadowing, the base class cannot access the newly created child (derived) class method. This is because the base class has the same name of the element. |
In concept, the base class can be accessed using the child object's overridden method.
|
- static void Main(string[] args)
- {
- BaseClass c = new ChildClass();
- Console.WriteLine(c.GetMethodOwnerName());
- }

In overriding, the base class can be accessed using the child object's overridden method.
Overriding Example
- static void Main(string[] args)
- {
- BaseClass c = new ChildClass();
- Console.WriteLine(c.GetMethodOwnerName());
- }

We cannot use the new and override keywords together. If you do then the compiler throws a compilation error.
Output
http://www.c-sharpcorner.com/UploadFile/ff2f08/overriding-vs-shadowing-in-C-Sharp/
http://msdn.microsoft.com/en-us/library/ms172785.aspx
Difference between normal Class and Abstract Class
Base classes :
1.
Have their own implementations for methods.but we cannot declare method
definition without implementation.
2.
Any class can inherit the class and it can be used/added/override to
in an inherited class.
3.
You can instantiate a base class
Abstract classes :
1.
Have
declarations for class methods and have implementation of concrete method.
2.
Any class inheriting the abstract class must implement its abstract methods or becomes too abstract
itself. Otherwise, it will get error.
3.
You cannot instantiate an abstract class
Example:-
public class A
{
public void Method1()
{
//method code
}
///Error-'TestConsoleApplication.AAA.Method2()'
is abstract but it is contained in non-abstract class 'A'
///public abstract void Method2();
public virtual void Method3()
{
//method code for class A, when
class A calls Method3, this code is executed
}
}
public class B : A
{
//public override void Method2()
//{
// //this must be implemented here to use it
//}
//{
// //this must be implemented here to use it
//}
public override void Method3()
{
//method code for class B, when
class B calls Method3, this code is executed
//or, if you choose not to
override this method, the compiler will use the code in class A
}
}
class B can still use Method1 from class A because it's inherited.
And if class A is to be abstract, then all of the methods would be declared
like Method2 and would have to be implemented in class B so that it can be
used.
Subscribe to:
Posts (Atom)