Friday, 27 April 2018

Method Overriding in C#


Method overriding is the most powerful feature of object oriented programming. Method Overriding can be defined as changing the definition or behavior of the Methods of the same Inheritance/Hierarchy in order to override their logic. Method Overriding is used to achieve runtime polymorphism. Method Overriding is accomplished by using the three keywords.

  • Virtual

  • override

  • Base

  • new


  • Using the keywords virtual , override and Base we can extend or override the logic of the methods derived in Base class. But the keyword new keyword we can hide the method derived in the base class.

    Let's see the use of each keyword in detail.

  • Virtual
  • - This keyword is used with the method which can be overridden.
  • override
  • - This keyword is used with the method which is overriding the same method derived in the base class.
  • Base
  • - This keyword is used to call the base class method in the derived class.
  • new
  • - This keyword is used to hide the base class implementation of method.

    using System;
    namespace Method_overriding
    {
        class Override
        {
         public virtual void show()
         {
          Console.WriteLine("Baseclass Show");
         }
        }
        class override2:Override
        {
         public override void show()
         {
         base.show(); Calling base class method
         Console.WriteLine("Sub show");
         }
        }
        class Program
        {
         static void Main(string[] args)
         {
          Override over = new Override();
          over.show();
          Console.ReadLine();
         }
        }
    }


    Output
    Baseclass Show


    Explanation

  • We created a class override and a method show() with the keyword virtual.
  • Then we created another class override2 inherited from override and defined same method show() with the keyword override in order to override parent class method show().
  • In the subclass show() method, we called base class show() method using keyword base.
  • in the main method, we created Object of override class and called show() method.
  • Let's see another example

    using System;
    namespace Method_overriding
    {
        class Override
        {
         public virtual void show()
         {
          Console.WriteLine("Baseclass Show");
         }
        }
        class override2:Override
        {
         public override void show()
         {
         base.show(); Calling base class method
         Console.WriteLine("Sub show");
         }
        }
        class Program
        {
         static void Main(string[] args)
         {
          Override over = new Override2();
          over.show();
          Console.ReadLine();
         }
        }
    }


    Output
    Baseclass Show
    Sub show


    Explanation

  • We created a class override and a method show() with the keyword virtual.
  • Then we created another class override2 inherited from override and defined same method show() with the keyword override in order to override parent class method show().
  • In the subclass show() method, we called base class show() method using keyword base.
  • in the main method, we created Object of override class and gave the reference of override2 class and called show() method.


  • Now we will remove the Base.show() from the subclass show() method.

    using System;
    namespace Method_overriding
    {
        class Override
        {
         public virtual void show()
         {
          Console.WriteLine("Baseclass Show");
         }
        }
        class override2:Override
        {
         public override void show()
         {
         base.show() removed
         Console.WriteLine("Sub show");
         }
        }
        class Program
        {
         static void Main(string[] args)
         {
          Override over = new Override2();
          over.show();
          Console.ReadLine();
         }
        }
    }


    In the above program , note we have created object of parent class but given the reference of subclass.

    Output
    Sub show


    Use of new keyword

    As you see above , with the virtual, override keywords we override the parent class method with subclass method. But if you see , if we created object of parent class and gave reference of subclass , then the subclass method will be called not base class method.
    But in case of new keyword, there is slight difference. If you create object of Parent class and gave the reference of subclass , then the parent class method will be called, because the new keyword hides the implementation of base class method in derived class. see the below example.

    using System;
    namespace Method_overriding
    {
        class Override
        {
         public virtual void show()
         {
          Console.WriteLine("Baseclass Show");
         }
        }
        class override2:Override
        {
         public new void show()
         {
              Console.WriteLine("Sub show");
         }
        }
        class Program
        {
         static void Main(string[] args)
         {
          Override over = new Override2();
          over.show();
          Console.ReadLine();
         }
        }
    }


    Output
    Baseclass Show


    Please comment below if you have any questions.

    Sunday, 15 April 2018

    Function/Method Overloading in C#



    In previous post , we learned what are the classes and methods and objects. If you have not seen that, here is the link to it.
    Classes in C#

    What is Function Overloading

    Function Overloading is nothing but a function/Method with the same name but multiple function definition. The function are different from each other by Function Definition and the number of parameters passed to the function.
    Function Overloading is one of the way we can implement static Polymorphism. Polymorphism stands for the term having multiple forms. The Polymorphism have two types .
    1)Static .
    2)Dynamic .
    The function overloading is a Static Polymorphism.

    Example

    using System;
    namespace FunctionOverloading
    {
          class PrintData
          {
                 public void show(int a)
                {
                        Console.WriteLine("Age of Raju is {0}", a);
                }
                 public void show(double amount)
                {
                        Console.WriteLine("Amount he has taken is {0}",amount);
                 }
                 public void show(string s)
                {
                         Console.WriteLine("Name is {0}", s);
                         Console.ReadLine();
                 }
          }
    }
          class Program
          {
             static void Main(string[] args)
             {
               PrintData p = new PrintData();
               p.show(20);
               p.show(500.45);
               p.show("Raju");
             }
          }


    Outout
    Age of Raju is 20
    Amount he has taken is 500.45
    Name is Raju


    Explanation

  • In the above program, We wrote the function public void show for three times to print three different values of different data type.int,double and string respectively.

  • But the difference is in each function we provided different data type of parameters to the function.

  • Next we created object of PrintData class in the main method.
  • in the main method , we called the all the three methods using object of PrintData class.

  • So it printed all the parameters passed to method on the console as seen in the output


  • Example 2

    using System;
    namespace Overloading_2
    {
          class Calculate
          {
             public void calculation(int a ,int b)
             {
               Console.WriteLine("Addition of two numbers is {0} ",a+b);
             }
             public void calculation(int a,int b,int c)
             {
               Console.WriteLine("Addition of three numbers is {0} ", a + b + c);
               Console.ReadLine();
             }
            }
    }
    class Program
    {
        static void Main(string[] args)
        {
           Calculate c = new Calculate();
           c.calculation(3, 3);
           c.calculation(2, 4, 3);
        }
    }


    Output

    Addition of two numbers is 6
    Addition of three numbers is 9


    Explanation
  • In the above program , we created class named calculate
  • We defined and implemented two functions named CALCULATION with different number of parameter passed.
  • i.e two parameters for first function and three parameters for second function.
  • Despite having the same name , the program was executed successfully because we passed different number of parameters to both the functions implementing method overloading.

  • This is how method overloading is implemented.

    Sunday, 1 April 2018

    SQL IN Operator


    Using IN operator in SQL, We can SELECT, UPDATE or DELETE operations providing multiple values in the WHERE clause.

    That means SQL query will be executed for those provided values in the IN clause. This IN clause works like OR condition. If one value supplied in the IN clause is not found the query will look for other values.
    You can also provide SELECT Query in the IN clause.

    Syntax (supply values)

    SELECT column_name1,column_name2...column_name_n
    FROM table_name WHERE
    column_name IN (value1,value2...valuen)


    Example

    ID Name State JoinDate Experience
    1 Anoop Sharma Delhi 2017-06-12 12:04:30.233 3
    2 Tarun Sharma Maharashtra 2017-06-15 01:03:34.247 5
    3 Gaurav Kapoor Pune 2017-08-21 07:30:00.543 3
    4 Arjit Singh Punjab 2017-07-11 10:03:30.577 7


    Query_1

    Select those employees from above table who's experience is either 7 or 5 years or both.

    SELECT * FROM Employee_details
    WHERE Experience in (7,5)


    Output

    ID Name State JoinDate Experience
    2 Tarun Sharma Maharashtra 2017-06-15 01:03:34.247 5
    4 Arjit Singh Punjab 2017-07-11 10:03:30.577 7


    Query_2

    Select those employees from above table who belongs to state either punjab or Pune or both.

    SELECT * FROM tbl_Record WHERE
    State in ('Punjab','Pune')


    Output
    ID Name State JoinDate Experience
    4 Arjit Singh Punjab 2017-07-11 10:03:30.577 7
    3 Gaurav Kapoor Pune 2017-08-21 07:30:00.543 3


    Apply NOT to IN clause

    Query_3

    Select those employees from above table who's experience not in 3 or 5 years.

    SELECT * FROM tbl_Record
    WHERE Experience NOT IN (3,5)


    Output
    ID Name State JoinDate Experience
    4 Arjit Singh Punjab 2017-07-11 10:03:30.577 7


    Using SELECT query in IN clause

    Syntax

    SELECT column_name1,column_name2...column_name_n
    FROM table_name WHERE
    column_name IN (SELECT Query)


    Consider below tables

    Example

    ID Name State JoinDate Experience
    1 Anoop Sharma Delhi 2017-06-12 12:04:30.233 3
    2 Tarun Sharma Maharashtra 2017-06-15 01:03:34.247 5
    3 Gaurav Kapoor Pune 2017-08-21 07:30:00.543 3
    4 Arjit Singh Punjab 2017-07-11 10:03:30.577 7


    ID CustomerName Customer_Type OrderID Date
    1 Bob Prime 334 2018-02-11 05:45:54.000
    2 Raj Non-Prime 364 2018-01-04 04:30:54.000


    Query_4

    SELECT * FROM tbl_Record
    WHERE ID in (SELECT ID FROM customers)


    Output

    ID Name State JoinDate Experience
    1 Anoop Sharma Delhi 2017-06-12 12:04:30.233 3