Method Overriding
Method Overriding
Method Overriding
If we create two or more members having same name but different in number or
type of parameter, it is known as member overloading. In C#, we can overload:
o methods,
o constructors, and
o indexed properties
C# Method Overloading
Having two or more methods with same name but different in parameters, is known
as method overloading in C#.
1. using System;
2. public class Cal{
3. public static int add(int a,int b){
4. return a + b;
5. }
6. public static int add(int a, int b, int c)
7. {
8. return a + b + c;
9. }
10. }
11. public class TestMemberOverloading
12. {
13. public static void Main()
14. {
15. Console.WriteLine(Cal.add(12, 23));
16. Console.WriteLine(Cal.add(12, 23, 25));
17. }
18. }
Output:
35
60
1. using System;
2. public class Cal{
3. public static int add(int a, int b){
4. return a + b;
5. }
6. public static float add(float a, float b)
7. {
8. return a + b;
9. }
10. }
11. public class TestMemberOverloading
12. {
13. public static void Main()
14. {
15. Console.WriteLine(Cal.add(12, 23));
16. Console.WriteLine(Cal.add(12.4f,21.3f));
17. }
18. }
Output:
35
33.7
C# Method Overriding
If derived class defines same method as defined in its base class, it is known as
method overriding in C#. It is used to achieve runtime polymorphism. It enables you
to provide specific implementation of the method which is already provided by its
base class.
1. using System;
2. public class Animal{
3. public virtual void eat(){
4. Console.WriteLine("Eating...");
5. }
6. }
7. public class Dog: Animal
8. {
9. public override void eat()
10. {
11. Console.WriteLine("Eating bread...");
12. }
13. }
14. public class TestOverriding
15. {
16. public static void Main()
17. {
18. Dog d = new Dog();
19. d.eat();
20. }
21. }
Output:
Eating bread...
C# Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which means
many forms. It is a greek word. In object-oriented programming, we use 3 main
concepts: inheritance, encapsulation and polymorphism.
There are two types of polymorphism in C#: compile time polymorphism and
runtime polymorphism. Compile time polymorphism is achieved by method
overloading and operator overloading in C#. It is also known as static binding or
early binding. Runtime polymorphism in achieved by method overriding which is also
known as dynamic binding or late binding.
1. using System;
2. public class Animal{
3. public virtual void eat(){
4. Console.WriteLine("eating...");
5. }
6. }
7. public class Dog: Animal
8. {
9. public override void eat()
10. {
11. Console.WriteLine("eating bread...");
12. }
13.
14. }
15. public class TestPolymorphism
16. {
17. public static void Main()
18. {
19. Animal a= new Dog();
20. a.eat();
21. }
22. }
Output:
eating bread...
1. using System;
2. public class Shape{
3. public virtual void draw(){
4. Console.WriteLine("drawing...");
5. }
6. }
7. public class Rectangle: Shape
8. {
9. public override void draw()
10. {
11. Console.WriteLine("drawing rectangle...");
12. }
13.
14. }
15. public class Circle : Shape
16. {
17. public override void draw()
18. {
19. Console.WriteLine("drawing circle...");
20. }
21.
22. }
23. public class TestPolymorphism
24. {
25. public static void Main()
26. {
27. Shape s;
28. s = new Shape();
29. s.draw();
30. s = new Rectangle();
31. s.draw();
32. s = new Circle();
33. s.draw();
34.
35. }
36. }
Output:
drawing...
drawing rectangle...
drawing circle...
1. using System;
2. public class Animal{
3. public string color = "white";
4.
5. }
6. public class Dog: Animal
7. {
8. public string color = "black";
9. }
10. public class TestSealed
11. {
12. public static void Main()
13. {
14. Animal d = new Dog();
15. Console.WriteLine(d.color);
16.
17. }
18. }
Output:
white