views:

92

answers:

7

I have some code to implement interface in C#

public interface Intfc { void xyz();}

public class BaseClass : Intfc
{

    public virtual void xyz()
    {
        Console.WriteLine("In Base Class");
    }
}

public class Derived : BaseClass
{
    public override void xyz()
    {
        Console.WriteLine("In Derived Class");
    }
}

static void Main(string[] args)
    {
        Derived mc = new Derived();
        mc.xyz(); //In Derived Class
        ((BaseClass)mc).xyz(); //In Base Class
        ((Intfc)mc).xyz(); //In Derived Class

        Console.ReadKey();

    }

I need the console output as provided in comments in Main(). Actual results are

In Derived Class
In Derived Class
In Derived Class

How can I achieve the desired results.

A: 

You can't do this, mc is of type Derived and it will always call the xyz in this class. It's how inheritance works. You need an instance of BaseClass:

var bc = new BaseClass();
bc.xyz(); // In Base Class
Darin Dimitrov
Any reason for the downvote? Please leave a comment when downvoting.
Darin Dimitrov
A: 

You will need a var b = new BaseClass(). Then b.xyz() instead of ((BaseClass)mc).xyz().

Noel Abrahams
+1  A: 

You need to overwrite the method on the Derived class instead of using the virtual / override operators use the new operator.

http://msdn.microsoft.com/en-us/library/51y09td4(VS.71).aspx#vclrfnew_newmodifier

try

public interface Intfc { void xyz();}

public class BaseClass : Intfc
{
    public void xyz()
    {
        Console.WriteLine("In Base Class");
    }
}

public class Derived : BaseClass
{
    new public void xyz()
    {
        Console.WriteLine("In Derived Class");
    }
}

static void Main(string[] args)
{
    Derived mc = new Derived();
    mc.xyz(); //In Derived Class
    ((BaseClass)mc).xyz(); //In Base Class
    ((Intfc)mc).xyz(); //In Derived Class

    Console.ReadKey();

}
madsleejensen
This answers the question, but it's worth noting that this type of `new` should be used sporadically, if at all.
jdv
I agree, but im sure he has a reason for requesting this type of behavior :)
madsleejensen
Yep, the required behaviour is just plain weird, regardless of the implementation. As an aside, is it even possible to override a non-virtual method. I suspect this code won't compile.
spender
Hi Spender the code did compile, but you were absolutely right, the override should not have been there :) i have removed it from the example!
madsleejensen
+1  A: 

Don't use virtual methods with the override modifier, but reimplement the interface on the Derived class.

The code below exhibits the required behaviour, but I'd consider this approach slightly confusing and opaque to end users.

public interface Intfc { void xyz();}

public class BaseClass : Intfc
{

    public void xyz()
    {
        Console.WriteLine("In Base Class");
    }
}

public class Derived : BaseClass,Intfc
{
    public void xyz()
    {
        Console.WriteLine("In Derived Class");
    }
}

static void Main()
{
    Derived mc = new Derived();
    mc.xyz(); //In Derived Class
    ((BaseClass)mc).xyz(); //In Base Class
    ((Intfc)mc).xyz(); //In Derived Class



}
spender
A: 

You are miss understanding the behavior of inherit.

When You create a object of given type that override some virtual method You will alway get result from that override method. The casting do not change the object structure, once create object can change his state after cast operation.

static void Main(string[] args)
    {
        Intfc mc1 = new Derived();
        Intfc mc2 = new BaseClass();

          mc1.xyz();
          mc2.xyz();

        Console.ReadKey();

    }

This will product the

In Base Class
In Derived Class

But because there is always a but.

If You overwrite that method You will get the result what You are up to, to overwrite method just use the operator new

public class Derived : BaseClass
{
    new public void xyz()
    {
        Console.WriteLine("In Derived Class");
    }
}

public class BaseClass : Intfc
{

    public void xyz()
    {
        Console.WriteLine("In Base Class");
    }
}

This will use new implementation for DeriveClass and old one for BaseClass.

Links:

What override is ?

Modifier new in method signature

What modifier new is ?

Vash
A: 

When you added the modifier override to the inherited virtual method xyz in mc , you simply overrode the the inherited virtual method in the instance referred to by mc. Its as if you erased the Base.xyz and written Derived.xyz in its place in memory of the mc instance. So the instance referred to by mc has no record of the virtual xyz in its implementation and therefore no way of getting to it through the mc referrence. The only way to access the base's virtual method is through an instance of the base as demonstrated below:

 var b = new BaseClass();    
 b.xyz();//  instead of ((BaseClass)mc).xyz()
mumtaz
A: 

Try something like below

public interface Intfc { void xyz();}

public class BaseClass : Intfc
        {

            public virtual  void xyz()   
            {
                MessageBox.Show("In Base Class");
            }
        }

    public class Derived : BaseClass, Intfc //Implementation of interface
    {
        public new void xyz()             // new keyword to hide inherited member
        {
            MessageBox.Show("In Derived Class");
        }
    }  

this will print

Drive Base Drive

saurabh