Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have code
C#
namespace InheritanceExample
{
    class Program
    {
        static void Main(string[] args)
        {

            var animals = new IAnimal[] { new Monkey(), new Dog() };
            foreach (var animal in animals)
            {
                DisplayAnimalData(animal);
            }
            Console.ReadLine();
        }

        private static void DisplayAnimalData(IAnimal animal)
        {
            Console.WriteLine("Animal has {0} legs and makes this sound", animal.NumberOfLegs);
            animal.Vocalize();
            ISpanishAnimal spanishAnimal = (ISpanishAnimal)animal;

           if(animal == spanishAnimal)
               Console.WriteLine("The same");

            Console.WriteLine("Spanish Animal has {0} legs and makes this sound", spanishAnimal.NumberOfLegs);
            spanishAnimal.Vocalize();

        }
    }

    public interface IAnimal
    {
        int NumberOfLegs { get; }
        void Vocalize();
    }
    public interface ISpanishAnimal
    {
        int NumberOfLegs { get; }
        void Vocalize();
    }

    public abstract class baseAnimal : IAnimal, ISpanishAnimal
    {
        int _numberOfLegs;
        private readonly string _sound;
        public baseAnimal(int numberOfLegs, string sound)
        {
            _numberOfLegs = numberOfLegs;

            _sound = sound;
        }

        void ISpanishAnimal.Vocalize()
        {
            Console.WriteLine(_sound + "in Spanish");
        }

        int ISpanishAnimal.NumberOfLegs
        {
            get
            {
                return _numberOfLegs * 5;
            }          
        }
        void IAnimal.Vocalize()
        {
            Console.WriteLine(_sound);
        }

        int IAnimal.NumberOfLegs
        {
            get
            {
                return _numberOfLegs;
            }
        }


        public string Sound
        {
            get { return _sound; }       
        }

    }
    public class Monkey : baseAnimal
    {
        public Monkey() : base(2,"Monkey Sound!") { }
    }
    public class Dog : baseAnimal
    {
        public Dog() : base(4, "Woof!") { }
    }

}

Please look at the code:
C#
var animals = new IAnimal[] { new Monkey(), new Dog() };

The interface IAnimal doesn't have the methods Monkey and Dog.

So what kind of skills are used here? Downcast or upcast or implicit implement?
Posted
Comments
Sergey Alexandrovich Kryukov 23-Feb-15 10:20am    
Off-topic. Did you meet any expert here who looks like a psychologist (or a psychiatrist, for this matter)? Perhaps you just misuse the work "skill".

Even if you mean "what technique is used here", the question is counter productive and does not make sense. What do you really want to understand. You sound like you believe in verbal knowledge, as if words change something. (All your options are just gibberish, and the whole code sample is no good. At least explain what you want to achieve, what do you want to illustrate by this sample.)

—SA
[no name] 23-Feb-15 10:22am    
I mean what term is best to describe the implementation?
Sergey Alexandrovich Kryukov 23-Feb-15 10:24am    
You are going in wrong direction in principle, not in detail. The whole approach is wrong. Please see my updated comment.
—SA
[no name] 23-Feb-15 10:37am    
The code is not mine. I got it online. The purpose is to demonstrate the concepts of Inheritance.
Sergey Alexandrovich Kryukov 23-Feb-15 11:13am    
Then better don't waste time on it. Just read the manuals and reference and write your own code. Why should we even look at code written by who knows who?
—SA

1 solution

Monkey() and Dog() are not methods they are classes that implement the interface IAnimal.

Somewhere you will need something like
C#
public class Monkey : IAnimal
{
    public int getLegs()
    {
        return 2;
    }
}
public class Dog : IAnimal
{
    public int getLegs()
    {
        return 4;
    }
}

See this CP article for more information Interfaces in C# (For Beginners)[^]

You are not inheriting from the Interface, you are implementing the interface.

Another CP article that might be useful Abstract Class versus Interface[^]

[EDIT]
OP asked
Quote:
Yes, I know that. I meant new IAnimal[] is kind of array? Can we do that?


Yes you can. Every item in the array must be an object of a class that implements IAnimal.

Note that when you use the elements in the array you may need to cast them to their original class (or a base class) before you can access all of their properties or methods (the one's that are unique to each class)

Also note that you can't use the (new) style of declaration
var animals = new[] { new Monkey(), new Dog() };
as you will get an error
Quote:
No best type found for implicitly-typed array
In other words, Visual Studio needs a little extra help to work out what the elements in the array will be :)
 
Share this answer
 
v2
Comments
[no name] 23-Feb-15 10:54am    
Yes, I know that. I meant new IAnimal[] is kind of array? Can we do that?
CHill60 23-Feb-15 11:05am    
I've added more information to my solution
BillWoodruff 23-Feb-15 17:20pm    
+5 Another good one !
CHill60 23-Feb-15 17:51pm    
Cheers!

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