Monday, 25 February 2013

Method Overriding

If at all a parent classes method is re-implemented under a child class exactly with the same signature we call it as Method  Overriding.

How to override a parent classes method under child class ?
  • If we want to override any parent classes method under child class first under the parent the method must be defined using virtual modifier. Declaring a method as virtual under a class is giving a permission for its child classes to override.
  • The methods that are declared as virtual can be overwritten as child classes using a override modifier.
      class1
      public virtual void Show()  // overridable
      class 2:class1
      public override void Show() // overriding

Add a class LoadParent.cs and write the following code
using System;
namespace CSharpConsole
{
    class LoadParent
    {
        public void Test()
        {
            Console.WriteLine("Parent's test method");
        }
        public virtual void  Show() //  overridable
        {
            Console.WriteLine("Parent's show method");
        }
        public void Display()
        {
            Console.WriteLine("Parent's display method");
        }
    }
}
// Add a class loadchild.cs and write the following code
using System;
namespace CSharpConsole
{
    class loadchild: LoadParent
    {
       // overloading parent's test method
        public void Test(int x)
        {
            Console.WriteLine("Child's test method ");
        }
       public override  void Show()
        {
            Console.WriteLine("Child's show method ");
        }
       public new void Display()
       {
            Console.WriteLine("Child's display method");
       }
       static void Main()
       {
           loadchild c = new loadchild();
           c.Test();
           c.Test(10);
           c.Show();
           c.Display();
           Console.ReadLine();
       }
   }
}

Types of Polymorphism

Polymorphism is divided into 2 categories
  • Compiletime Polymorphism (also called as Static Polymorphism or Early Binding)
  • Runtime Polymorphism (also called as Non-Static Polymorphism or Late Binding)
        In the first case the object of the class will identify for a particular method call which polymorphic method has to be executed and binds the method call with its method definition and executes this method in runtime but only the method will be bound. This type of binding takes place in Overloading because in overloading we have multiple methods with same name but different signature so each method is unique to itself so while binding the method definition we can identify the unique keyword.
       In the second case ie Runtime Polymorphism the object of a class recognizes which polymorphic method it has to call in runtime and this kind of identification takes pace in Overriding and Hiding as there are multiple methods with the same name and same signature with both the cases so that  the object will identify which exact method has to be executed in runtime only being on the hierarchy of the class.

Friday, 22 February 2013

Constructor Overloading

Just like we can overload the methods of a class in the same way we can also overload the constructors of a class i.e it is possible to define multiple constructors under a class changing their signatures.
    If a constructor of a class are overloaded we can create object of that class by making use of any constructor available or we can create a separate object by using a separate constructor.

Add a class LoadCon.cs and write the following code.

// Constructor Overloading

using System;
namespace CSharpConsole
{
    class LoadCon
    {
        int x;
        public LoadCon()     //default constructor
        {
            x = 10;
        }
        public LoadCon(int x)
        {
            this.x = x;

        }
        public void Display()
        {
            Console.WriteLine("The value of x is:{0}",+ x);
        }
        static  void Main()
        {
            LoadCon c1 = new LoadCon();
            LoadCon c2 = new LoadCon(20);
            c1.Display();
            c2.Display();
            Console.ReadLine();
        }
    }
}

Polymorphism

Its an approach of definning different methods with a different behaviour i.e whenever the input changes the output or behaviour also changes accordingly which can be implemented under a language using different approaches like Overloading, Overriding and Hiding.

Overloading
This is again of three types:
  • Method Overloading
  • Constructor Overloadding
  • Operator Overloading 
Method Overloading: It is an approach of definnig multiple methods under a class with the same name, by changing the signature of those methods. Changing the signature of those method in the sense we can change the number of parameters being passed to the method or order of parameters being passed to method.

//Method overloading

using System;

namespace CSharpConsole
{
    class LoadDemo
    {
        public void Show()
        {
            Console.WriteLine(1);
        }
        public void Show( int x)
        {
            Console.WriteLine(2);
        }
        public void Show(string s)
        {
            Console.WriteLine(3);
        }
        public void Show(int x, string s)
        {
            Console.WriteLine(4);
        }
        public void Show(string s, int x)
        {
            Console.WriteLine(5);
        }

        static  void Main()
        {
            LoadDemo d = new LoadDemo();
            d.Show();
            d.Show(10);
            d.Show("hello");
            d.Show(5,"leena");
            d.Show("bansiwal", 15);
            Console.ReadLine();
        }
    }
}

Thursday, 21 February 2013

Axure Tooltip

         Today, Sudeep showed us how to use a tool called 'Axure'. It is a very powerful wire-framing tool, that lets you create clickable wire-frames very quickly. The Business Analyst acts as a middle-ware between the Software developer and the Client. When a Client places an requests the BA analyses those requests and proceeds the work accordingly.
         We also created 2-3 pages using this tool  under the guidance of Sudeep and it was nice to learn something new.

Extension Methods in CSharp

This is a new feature that has been inter choose in C#.Net 3.0 that allows us to add new methods into an existing class without editing the source code of the class.

Defining Extension:
  •  Extension methods must be defined under static class only so every extension method is static by default but once the method is bound with another class it gets converted into non static and it can be called by using old classes object.
  • The first parameter of an extension method  is to specify to which class the method has to be bound with and we call it as a binding parameter which should be defined as following. 
         this<class name><obj>
  • If required we can also pass normal parameters to an extension method which should start from the second place of parameter list and all this parameter will be considered while calling the method.
  • If an extension method is defined with n parameters while calling the methods we will be having n-1 parameters only because binding parameters is excluded.
Add a class oldclass.cs and code the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
 class oldclass
 {
  public int a;
  public oldclass (int a)
  {
   this.a=a;
  }
   public void show1()
  {
   Console.WriteLine("Method One:  " + this.a);
  }
   public void show2()
  {
   Console.WriteLine("Method Two:  " + this.a);
  }
 }
}
// Now add a newclass.cs and code the following
using System;
namespace CSharpConsole
{
  static  class newclass
 {
  public static void show3(this oldclass obj)
   {
    Console.WriteLine("Method three ");
   }
    public static void show4(this oldclass obj, int x)
   {
    Console.WriteLine("Method four: "  + x);
   }
    public static void show5(this oldclass obj)
   {
    Console.WriteLine("Method five: " + obj.a);
   }
 }
}
// Again add a new classTestold.cs to test the above code

using System;

namespace CSharpConsole
{
 class Testold
 {
   static void Main()
   {
    oldclass obj = new oldclass(10);
    obj.show1();
    obj.show2();
            
    // Calling Extension Methods
     obj.show3();
     obj.show4(50); obj.show5();
     Console.ReadLine();
   }
 }
}

Tuesday, 19 February 2013

     Today we practiced the concept of Inheritance, Some SQL queries based on joins, indexes, set operators etc. We also learnt the concept of security in SQL Server, and that how certain permissions or privileges can be granted to other users by the users who own the database etc...
     It was a good day... Learning more and more every day and more important enjoying while learning.