Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I am getting output as swim, quack and fly. I don't know how to get output from my derived class Mallard, RubberDuck and DecoyDuck. Am I doing anything wrong while implementing derived class from my base class? Can anyone help me.

C#
{
    interface IFlyBehavior
    {
        void Action1();
    }
    interface IQuackBehavior:IFlyBehavior
    {
        void Action2();
    }
    interface ISwimBehavior:IQuackBehavior
    {
        void Action3();
    }
    public class Duck:IQuackBehavior
    {
        public void Action1()
        {
           Console.WriteLine("fly");
        }
        public void Action2()
        {
            Console.WriteLine("quack");
        }
        public void Action3()
        {
            Console.WriteLine("Swim");
        }
    }
    public class Mallard: Duck, IFlyBehavior, IQuackBehavior, ISwimBehavior
    {
       public void Action1(string fly, string quack, string swim){
        Console.WriteLine("This duck can {0}, {1}, and {2}", fly, quack, swim);}
    }
    public class RubberDuck: Duck, IFlyBehavior, IQuackBehavior, ISwimBehavior
    {
         public void Action2(string fly, string quack, string swim)
         {
       Console.WriteLine ("This is rubberduck so it might float on water but cannot {0}, {1} or {2}",fly,quack,swim);
       }
    }
    public class DecoyDuck: Duck, IFlyBehavior, IQuackBehavior, ISwimBehavior
    {
         public void Action3(string fly, string swim, string quack) {
        Console.WriteLine("This duck cannot {0}, but {1}, and may be able to {2}",fly,swim,quack);}
    }
    class Program
    {
        static void Main()
        {
            Duck M = new Mallard();
            Duck R = new RubberDuck();
            Duck D = new DecoyDuck();
            M.Action1();
            R.Action2();
            D.Action3();


        }
    }
}
Posted
Comments
[no name] 4-May-14 14:39pm    
Well... what output do you get? What do you see happening when you step through your code?
Sergey Alexandrovich Kryukov 4-May-14 14:41pm    
Swim behavior inheriting from quack behavior is of course a string design move... :-)
—SA
Sergey Alexandrovich Kryukov 4-May-14 14:43pm    
Swim behavior inheriting from quack behavior is of course a string design move... :-)

You see, the question per se makes no sense unless you explain what do you want to achieve.

—SA
Sergey Alexandrovich Kryukov 4-May-14 15:03pm    
Well, does it all compiles and run? If it does, the question is what are those classes supposed to do and what do you thing goes wrong?
—SA
Sergey Alexandrovich Kryukov 4-May-14 16:22pm    
You should not include derived class from base class. Why? what it is supposed to mean? Do you really understand what inheritance does? Inheritance from class? from interface?
—SA

1 solution

Your code has a few problems: if you want to override a method in a derived class, you ought to use the virtual keyword in the base class (public virtual void Action1), and the override keyword in the derived class (public override void Action1).
But note: the function signature (i.e. return type and parameter types) must be the same for base and derived methods.
 
Share this answer
 

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