Clicky

Hi everyone,

I have a small problem in understanding the below oops code, i am using c# 2008

i have created 2 interfaces,
interface 1 has a method a()
interface 2 has a method b()

i hava a class aa which implements the interface1 and class bb which implements the interface 2
now in my mainclass i have to call both the interfaces with which i have to write some methods. how can i go about this can i call those 2 interfaces here.

Regards
Rajeeva

asked 12/15/2011 02:21

rajeeva_nagaraj's gravatar image

rajeeva_nagaraj ♦♦


12 Answers:
It is very simple. For example:

Interface A:
1:
2:
3:
4:
interface IA
{
    void methodA();
}


Interface B:
1:
2:
3:
4:
interface IB
{
    void methodB();
}


Class AA that implements Interface A:
1:
2:
3:
4:
5:
6:
7:
class AA : IA
{
    public void methodA()
    {
        Console.WriteLine("Method A is called.");
    }
}


Class BB that implements Interface B:
1:
2:
3:
4:
5:
6:
7:
class BB : IB
{
    public void methodB()
    {
        Console.WriteLine("Method B is called.");
    }
}


Then, your main program:
1:
2:
3:
4:
5:
IA a = new AA();
a.methodA();

IB b = new BB();
b.methodB();
link

answered

yeelokhk's gravatar image

yeelokhk

No its not working
link

answered 2011-12-15 at 22:37:20

rajeeva_nagaraj's gravatar image

rajeeva_nagaraj

Please ensure that all classes and interfaces are in the same namespace.

I have attached my source for your reference.

If the problems still exist, please provide the error message or any screenshots.  Thanks
 
Source.zip
  • 1 KB
  • Source
Source.zip
    link

    answered 2011-12-15 at 22:52:33

    yeelokhk's gravatar image

    yeelokhk

    The code is here:

     
    1:
    2:
    3:
    4:
    5:
    6:
    7:
    8:
    9:
    10:
    11:
    12:
    13:
    14:
    15:
    16:
    17:
    18:
    19:
    20:
    21:
    22:
    23:
    24:
    25:
    26:
    27:
    28:
    29:
    30:
    31:
    32:
    33:
    34:
    35:
    36:
    37:
    38:
    39:
    40:
    41:
    42:
    43:
    44:
    45:
    46:
    47:
    48:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        interface IInterface1
        {
            void A();
        }
    
        interface IInterface2
        {
            void B();
        }
    
        class Aa : IInterface1
        {
    
            public void A()
            {
                Console.WriteLine("Method A");
            }
        }
    
        class Bb : IInterface2
        {
    
            public void B()
            {
                Console.WriteLine("Method B");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                IInterface1 ii1= new Aa();
                ii1.A();
    
                IInterface2 ii2=new Bb();
                ii2.B();
            }
        }
    
       
    }


    Just create an instance of IInterface1 and assign with object of class Aa, in similar way you c
    an create an instance of IInterface2 and assign it with object of class Bb.
    link

    answered 2011-12-15 at 22:56:57

    naman_goel's gravatar image

    naman_goel

    >>Please ensure that all classes and interfaces are in the same namespace.

    It don't have to be. You can add public key word before interfaces and you can use using keyword to import them to the classes. However can't find a problem in the "yeelokhk's" code. Whats the problem you got?
    link

    answered 2011-12-15 at 23:10:48

    wdfdo1986's gravatar image

    wdfdo1986

    naman_goel's way is also possible. but that doesn't means that the above way isn't working. So I would like to know what is the error.
    link

    answered 2011-12-15 at 23:16:30

    wdfdo1986's gravatar image

    wdfdo1986

    yes public modifier for those method should work.
    link

    answered 2011-12-15 at 23:19:23

    naman_goel's gravatar image

    naman_goel

    Hi every one, reason i asked u the above question is the below written code, here i am facing problem in the birdmain class it is giving me some error. i am using c# please find the code

    Sir i have a small doubt in writing some oops code, please can you help me in fixing that.


    -->Step1 i have created a interface called "Fly Behaviour" that will have a Fly() method

    Interface FlyBehaviour
    {
         string Fly()
    }

    --> Step 2 i have 2 class which will implement that interface
    Public class CanFly:FlyBehaviour
    {
          public string Fly()
          {
               return "I can fly";
          }
    }

    Public class CannotFly:FlyBehaviour
    {
          public string Fly()
          {
              return "I cannot fly";
          }
    }

    --> Step 3 I have 2nd interface which
    Interface SoundBehaviour
    {
         string Sound();
    }

    --> Step 4 I have 2 more classes which implement this interface
    Public class MakeSound:SoundBehaviour
    {
            Public string Sound()
            {
                   return "I can make sound";
            }
    }

    Public class NoSound:SoundBehaviour
    {
             public string Sound()
             {
                  return "I cannot make sound";
             }
    }

    --> i have a bird class
    public class BirdMain
    {
           FlyBehaviour = flybeh;
           SoundBehaviour = soundbeh;

           public string PerformFly
           {
                 return flybeh.Fly();
           }

           public string PerformSound()
           {
                 return soundbeh.Sound();
           }
    }

    -- Finally i have 2 classes which inherits the BirdMain class
    class RealBird:BirdMain
    {
         FlyBehaviour fly = new CanFly();
         SoundBehaviour snd = new MakeSound();
    }

    Class PlasticDuck:BirdMain
    {
          FlyBehaviour fly = new CannotFly();
          SoundBehaviour snd = new NoSound();
    }

    -- i have 2 buttons on my form button 1  = "Real bird" and the button 2 = "Plastic bird"
    on click of realbird button
    {
          BirdMain brd = new RealBird()

          print brd.PerformFly()
          print brd.PerformSound()
    }

    on click of plasticbird button
    {
         BirdMain brd = new PlasticBird()

          Print brd.PerformFly();
          Print brd.PerformSound();
    }

    Now the problem is that i am unable to create a reference for 2 interfaces in the bird main class, i dont know whether i am doing it right or not please advise me and let me know how can i proceed with this.

    Regards
    Rajeeva
    link

    answered 2011-12-15 at 23:20:51

    rajeeva_nagaraj's gravatar image

    rajeeva_nagaraj

    what is :

    public class BirdMain
    {
           FlyBehaviour = flybeh;//what is flybeh, where is it's object
           SoundBehaviour = soundbeh;//what is soundbeh, where is it's object

           public string PerformFly
           {
                 return flybeh.Fly();
           }

           public string PerformSound()
           {
                 return soundbeh.Sound();
           }
    }
    link

    answered 2011-12-16 at 01:38:51

    naman_goel's gravatar image

    naman_goel

    It should be :


     
    1:
    2:
    3:
    4:
    5:
    6:
    7:
    8:
    9:
    10:
    11:
    12:
    13:
    14:
    15:
    16:
    17:
    18:
    19:
    20:
    21:
    22:
    23:
    24:
    25:
    26:
    27:
    28:
    29:
    30:
    public class BirdMain
    {
           FlyBehaviour flybeh=null;
           SoundBehaviour soundbeh=null;
           
           //Intializing default values as CanFly and CanSound Instance
           BirdMain()
           {
              flybeh= new CanFly();
              soundbeh = new MakeSound();
           }
    
            BirdMain(FlyBehaviour flybeh, SoundBehaviour soundbeh)
           {
              this.flybeh= flybeh;
              this.soundbeh = soundbeh;
           }
    
    
    
           public string PerformFly
           {
                 return flybeh.Fly();
           }
    
           public string PerformSound()
           {
                 return soundbeh.Sound();
           }
    }


    now where ever you are using this you should pass instance of classes implementing these interfaces, something like this:

    BirdMain(new CanFly(), new MakeSound());
    link

    answered 2011-12-16 at 02:31:59

    naman_goel's gravatar image

    naman_goel

    >FlyBehaviour = flybeh;//what is flybeh, where is it's object
    >SoundBehaviour = soundbeh;//what is soundbeh, where is it's object

    And how can a type can assign an value
    link

    answered 2011-12-16 at 02:41:42

    wdfdo1986's gravatar image

    wdfdo1986

    I agree with naman_goel's code again
    link

    answered 2011-12-16 at 02:44:14

    wdfdo1986's gravatar image

    wdfdo1986

    Your answer
    [hide preview]

    Follow this question

    By Email:

    Once you sign in you will be able to subscribe for any updates here

    By RSS:

    Answers

    Answers and Comments

    Tags:

    Asked: 12/15/2011 02:21

    Seen: 186 times

    Last updated: 12/16/2011 03:02