Programming Example of C# Polymorphism

Qu 1. Write a program to save data to database. There should be a function Save() that accepts one, two or three values as parameter and then save that to database.
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgrammingExample1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.save();
            p.save("Jack");
            DateTime dt = DateTime.Now;
            p.save("Jack", 22, dt.ToShortDateString());
            Console.ReadKey();

        }
        public void save()
        {
            Console.WriteLine("No Data to Saved");
        }
        public void save(string name)
        {
            Console.WriteLine("{0} saved", name);
        }
        public void save(string name, int age)
        {
            Console.WriteLine("{0} and {1} - Saved", name, age);
        }
        public void save(string name, int age, string date)
        {
            Console.WriteLine("{0} and {1} are saved on {2}", name, age, date);
        }
    }
}

Output

No Data to Saved
Jack saved
Jack and 22 are saved on 19-11-2016
_
 
Qu 2: Write a program using virtual and override keyword in which base class should have a function TyreSize() that should be implemented in derived class using override keyword.
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example2
{
    class Program
    {
        static void Main(string[] args)
        {
            Ferrari f = new Ferrari();
            f.TyreSize();
            Console.ReadKey();
        }
    }
    public class Tyre
    {
        public virtual void TyreSize()
        {
            Console.WriteLine("Default Tyre Size is 13 Inches");
        }
    }
    public class Ferrari:Tyre
    {
        public override void TyreSize()
        {
            base.TyreSize();
            Console.WriteLine("Tyre Size is Overriden and new Tyre Size is : 14 Inches");
        }
    }
}

Output

Default Tyre Size is 13 Inches
Tyre Size is Overriden and new Tyre Size is : 14 Inches
_

Summary

In this chapter you learned Polymorphism using some programming example. In the next chapter there are some programming questions. You must solve that question using programming techniques.

More Articles

 

Share your thought