Inheritance - Daily Learning
Tuesday, June 8, 2010I always wonder that most of the people don't realize the importance of virtual and override words in the function. Everytime I ask, many (8 out of 10) of them tend to answer that virtual function can be overridden by child class. When you don't define base class function as virtual and child class function as override then also it overrides. So here I am going to show the difference with the following example in Console App.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
CA ca = new CA();
CB cb = new CB();
CA cab = new CB();
ca.fun1();
ca.fun2();
ca.fun3();
ca.fun4();
cb.fun1();
cb.fun2();
cb.fun3();
cb.fun4();
cab.fun1();
cab.fun2();
cab.fun3();
cab.fun4();
Console.ReadLine();
}
}
public class CA
{
public void fun1()
{
Type type = GetType();
Console.WriteLine("CA: fun1");
}
public virtual void fun2()
{
Type type = GetType();
Console.WriteLine("CA: fun2");
}
public virtual void fun3()
{
Type type = GetType();
Console.WriteLine("CA: fun3");
}
public virtual void fun4()
{
Type type = GetType();
Console.WriteLine("CA: fun4");
}
}
public class CB:CA
{
public void fun1()
{
Type type = GetType();
Console.WriteLine("CB: fun1");
}
public void fun2()
{
Type type = GetType();
Console.WriteLine("CB: fun2");
}
public override void fun3()
{
Type type = GetType();
Console.WriteLine("CB: fun3");
}
public new void fun4()
{
Type type = GetType();
Console.WriteLine("CB: fun4");
}
}
}
What I learnt Today
Monday, November 30, 2009
I thought of creating a series of what I learnt today. I feel almost everyday I learn something new and it gets slipped somewhere at the back of mind if I don't recall it. So its better to post it so that it will be a sort of revision for me and would be helpful to others. So I'll post my day to day learning under this post.
Today when I was working on Merging 2 databases and making them one, we had to define new constraints and keys . While defining new keys we found the following type of error coming.
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Grid__Page_ID__523BA32C". The conflict occurred in database "STATSONLINE", table "dbo.Page", column 'Page_ID'.
This error was caused when I ran the following query to give foreign key constraint.
ALTER TABLE Grid ADD FOREIGN KEY (Page_id ) REFERENCES Page(Page_Id)
After looking at the error one would guess that query is trying to define some foriegn key on the table 'Grid' which is already there and hence not allowing to overwrite that. At least I did think that way and started serching for all the Keys defined on the table. To my surprise there wasn't any such constraint define. Finally I concluded that It happens when this constraint can not be defined on the table because of inconsistent data. I had many grid records defined with the pages that are not there in page table. So it violates the basic requirement of foreign key.
About FK__Grid__Page_ID__523BA32C I feel it is the in memory name that server has given temporarily and then it checks all the records against this constraint and if it doesn't meet the criteria it throws aforesaid exception.