e enjte, 10 janar 2008

.NET basics revisited

I am on 'bench' these days.....well if thats not in your dictionary - 'its that condition of software engineer when he/she is not assigned to any projects / tasks'.

So thought of checking out tips, questions, tweaks, forums,interview questions etc in .NET....my next posts will be ones i have come across...so look out :)

Question: Which one among the following is illegal

a. Base b = new Derived();
b. Dervied b = new Base();

Where Derived is class inherited from Base...

Answer: b, now u can try and tell me

Question :
class A
{
public virtual void f()
{
Console.WriteLine("A.F");
}
}
class B : A
{
public override void f()
{
Console.WriteLine("B.F");
}
}
class C : B
{
new public virtual void f()
{
Console.WriteLine("C.F");
}
}
class D : C
{
public override void f()
{
Console.WriteLine("D.F");
}
}
class Program
{
static void Main(string[] args)
{
D d = new D();
A a = d;
B b = d;
C c = d;
a.f();
b.f();
c.f();
d.f();
}
}

What will this print

Answer: B.F B.F D.F D.F

If derived class overrides a method base class, objects of the derived class will call that method rather than the base class method.

Nuk ka komente: