Click here to Skip to main content
15,885,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have four classes as follow

C#
class Base
    {
        public void printf()
        {
            Console.WriteLine("I am fro base !!!");
        }
    }

class DerivedA : Base
    {
        public void printf()
        {
            Console.WriteLine("I am fro derived A !!!");
        }
    }

class DerivedB : Base
    {
        public void printf()
        {
            Console.WriteLine("I am fro derived B !!!");
        }
    }

class testProgram
    {
        public void print(Base baseClass)
        {
            baseClass.printf();
        }
    }


my aim is when I execute following code in line 4 it must give me error.

C#
testProgram tP = new testProgram();
 tP.print(baseClass);
 tP.print(derivedA);
 tP.print(derivedB);


What changes I need to do ????
thanks in advance
Posted
Updated 25-Sep-14 1:56am
v2

Your test program 'printf method gets a single parameter of Type Base: so, even if a valid instance of Class A, or Class B, is passed to it, it will always call the 'printf method defined in Base.

By declaring the parameter of Type Base, you essentially down-cast the instance of A, or B, to Type Base.

One way you could avoid this, with minimum code, would be to make the parameter Type 'dynamic and let that "do the work" for you of calling the right method for each Class Type:
C#
// assuming you have these instances created:
public Base baseClass = new Base();
public DerivedA derivedA = new DerivedA();
public DerivedB derivedB = new DerivedB();

public class testProgram
{
    public void print(dynamic baseClass)
    {
        baseClass.printf();
    }
}
However, you pay a price for using 'dynamic, and, until you are more advanced in C# programming, you may want to wait until you fully understand how 'dynamic (late binding) works before using it in your code.

Another way you can make your code use the "right Type" is to evaluate the Type:
C#
public class testProgram
{
    public void print(Base baseClass)
    {
        if (baseClass is DerivedA)
        {
            (baseClass as DerivedA).printf();
        }
        else if (baseClass is DerivedB)
        {
            // uncomment this to call the printf method in DerivedB
            //(baseClass as DerivedB).printf();

            // throw an error ? ... but, why ?
            // comment this out to not throw the error
            throw new InvalidProgramException("DerivedB don't print"); 
        }
        else
        {
            baseClass.printf();
        }
    }
}
But, why have a 'printf Method in Class DerivedB if you don't want to use it ?

I suggest you consider using the 'new method modifier when you are creating methods in derived classes when you wish to "hide" the method defined in the base Class:
C#
public class DerivedA : Base
{
    public new void printf()
    {
        Console.WriteLine("I am from derived A !!!");
    }
}

public class DerivedB : Base
{
    public new void printf()
    {
        Console.WriteLine("I am from derived B !!!");
    }
}
Using 'new (even though not necessary here: you'll get a compiler warning, but, not an error) makes the intent of your code clear. See: [^].
 
Share this answer
 
Hi Member,

You can't stop assigning a derived object to it's base class pointer. That's the nature of OOP...

So your question doesn't make sense.

If BillWoodruf was right in guessing what you really want, I'd like to add that you could use code like this (maybe also have a look at http://msdn.microsoft.com/en-us/library/ms173153.aspx[^] )

C#
using System;

namespace ConsoleApplication4
{
    class Base
    {
        public virtual void printf()
        {
            Console.WriteLine("I am fro base !!!");
        }
    }

    class DerivedA : Base
    {
        public override void printf()
        {
            Console.WriteLine("I am fro derived A !!!");
        }
    }

    class DerivedB : Base
    {
        public override void  printf()
        {
            Console.WriteLine("I am fro derived B !!!");
        }
    }

    class testProgram
    {
        public  void print(Base baseClass)
        {
            baseClass.printf();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Base baseClass = new Base();
            DerivedA derivedA = new DerivedA();
            DerivedB derivedB = new DerivedB();

            testProgram tP = new testProgram();
            tP.print(baseClass);
            tP.print(derivedA);
            tP.print(derivedB);

            Console.ReadKey();
        }
    }
}


Happy learning!
 
Share this answer
 
Comments
BillWoodruff 25-Sep-14 11:05am    
+5 I think your solution is exactly what the OP needs, and I need :)

It would be (academically) interesting to know what the "penalty" (memory, performance) for using 'dynamic here (without defining the method in the base class as virtual void) would be.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900