Can you please help me with Abstract and Interface...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alireza6485
    New Member
    • Jan 2009
    • 19

    Can you please help me with Abstract and Interface...

    Hi,
    Can you please help me with my assignment.I coded it and it runs I just need someone to help me a bit.
    The behaviour of monekys and human beings are Similar (Breeding and breathing are Identical and walking ,communication and eatng are common but non-identical)here is my code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Project1
    {
        public abstract class abstractAnimalsComonBehaviour:Animals
        {
            public abstract void breeding();
            public abstract void breathing();
            public abstract void walking();
            public abstract void eating();
            public abstract void communicating();
    
    
    
    
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Project1
    {
        public interface Animals
        {
          void breathing();
          void breeding();
          void walking();
          void communicating();
          void eating();
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Project1
    {
        class HumanBeings:abstractAnimalsComonBehaviour
        {
            public override  void walking()
            {
                Console.WriteLine("walking");
            }
            public override  void eating()
            {
                Console.WriteLine("fork and knife");
            }
            public override  void communicating()
            {
                Console.WriteLine("talking");
            }
            public override void breeding()
            {
                Console.WriteLine("Breed");
            }
            public override void breathing()
            {
                Console.WriteLine("Breath");
            }
    
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Project1
    {
        class Monkey : abstractAnimalsComonBehaviour
        {
            public override void walking()
            {
                Console.WriteLine("Jumping");
            }
            public override  void eating()
            {
                Console.WriteLine("using hands");
            }
            public override  void communicating()
            {
                Console.WriteLine("scream");
            }
            public override void breeding()
            {
                Console.WriteLine("Breed");
            }
            public override void breathing()
            {
                Console.WriteLine("Breath");
            }
    
    
        }
    }
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Project1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Animals Bobo = new Monkey();
                Bobo.eating();
                Bobo.breathing();
                Animals John = new HumanBeings();
                John.eating();
                John.breeding();
            }
        }
    }
    as you can see breething and breeding have the same output when their method is called.Is there any way not to re-type the breed and breath in one of the HumanBeings or Moneky???Can you please provide me the code for that?
    Last edited by PRR; Apr 10 '09, 04:34 AM. Reason: Please post code in [code] [/code] tags.
  • Bassem
    Contributor
    • Dec 2008
    • 344

    #2
    I think a base class for both Monkey and HumanBeings that implement the details of the common methods and declare the others as virtual.

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      Look into How to Override, Abstact class inheritance Abstract vs interface

      Comment

      Working...