CoDebate

Debating Code - [ASP.NET, C#, Sql Server, VB.NET]

Saturday, December 20, 2008

Inheritance, Polymorphism and Method Hiding

Whenever I meet .NET people in the code-debate mode, I encounter a common question which is the good example for the concepts like inheritance, polymorphism and method hiding.

Check the code snippet below:
Snippet I:



public class A
{
public virtual methodA()
{
Console.WriteLine("From Class A");
}
}
public class B : A
{
public override methodA()
{
Console.WriteLine("From Class B");
}
}
public class classMain
{
public static void Main(string[] args) {
A objA = new A();
B objB = new B();

// This calls A's methodA()
objA.methodA();


// This calls B's methodA()
objB.methodA();

objA = new B();


// This calls B's methodA()
objA.methodA();
}
}


Consider the change in the Classes B and classMain, as follows:
Snippet II:



public class A
{
public virtual methodA()
{
Console.WriteLine("From Class A");
}
}


public class B : A
{
public new methodA()
{
Console.WriteLine("From Class B");
}
}


public class classMain
{
public static void Main(string[] args)
{
A objA = new A();
B objB = new B();


// This calls A's methodA()
// From the object of class A
objA.methodA();


// This calls B's methodA()
// From the object of class B
objB.methodA();


objA = new B();


// This calls A's methodA()
// From the object of class B
((A)objB).methodA();
}
}


Hope you got the execution flow and output of the above snippets.
I place some questions here:

1) In the snippet I, from the main class, if I want to call the method methodA() of class A by the object of class B, I will get compile time error. Whats the purpose of inheritance here?

2) In the snippet II, from the main class, if I am able to call methodA() of class A by the object of class B, whats the purpose of method overriding then?. (Whenever I see ILDASM, it shows two methodA() in that) .

3) Whats the significant performance improvement in using method overriding over method hiding?

Shoot your answers here...

Labels: , , , , , ,

posted by Karthikeyan @ 6:45 AM 3 Comments

Tuesday, December 16, 2008

Yet another .NET Blog - CODE4ASP.NET

Code For ASP.NET - yet another site for .NET Technologies.

Srinath Gnath is the author blogger compiles .NET/C# code digests, who is also a friend and colleague of mine. According to him, this is a kind of knowledge sharing. Like me, he is also a consistent learner for advanced techniques in C# and ASP.NET.

For regular Updates, readers can check out his articles here

Labels: , , , , , ,

posted by Karthikeyan @ 8:02 AM 0 Comments

Welcome CoDebaters !!!

" CoDebate "

This term is coined from 2 words ... Code & Debate...

Debating What ??

Of Course...

.NET Code..

Well, its a great pleasure to write down here for the purpose of debating .NET code.

For your note...

Technology in CoDebate:
ASP.NET, C#, Sql Server, VB.NET, AJAX, jQuery,....

Possible Topics can be covered in CoDebate:
Advanced Programming Techniques,
Discussion on conventional codes,
Tips & Tricks,
Interview Questions,
Book Reviews,
General Technology Updates,...

Suitable contents for:
.NET Developers (Advanced/Intermediate/Beginners),
Job Seekers(.NET),...

Happy Programming...!!!

Labels: , , , ,

posted by Karthikeyan @ 6:47 AM 2 Comments