jsmith
jsmith

Reputation: 976

Using Inheritance in C#

So I just had a job interview and they gave me a list of tasks that they wanted me to work on beforehand. I finished all of the tasks except for one and was wondering if anyone could shed some light on the question.

The question went something like this

  1. Create a class called Human, give the class a public property HairColor and a public method Talk() that, when invoked, returns "I am a human."
  2. Create another class called child that inherits from Human and overrides the talk method and returns "I am a child."
  3. Create a male and female class that override talk and output "I am a Male." and "I am a Female."
  4. Change the child and human hierarchy to include male and female appropriately and output the proper string.

I completed tasks 1-3 but was stumped on question 4. I haven't really done much in my past work experience with inheritance and the question seemed kind of vague to me. I explained this to the employer and they seemed ok with it, but didn't tell me how it should have been done. Its been bugging me now and I'm wondering how it is done.

Upvotes: 2

Views: 563

Answers (3)

user153923
user153923

Reputation:

I don't do this kind of stuff often, but perhaps this would work:

(If nothing else, it gives us a code base to start with)

class Human {
  public enum SexType { Unknown, Male, Female };
  public Human() {
    HairColor = Color.Brown;
    Sex = SexType.Unknown;
  }
  public bool IsAdam { get { return true; } }
  public Color HairColor { get; set; }
  public SexType Sex { get; set; }
  public virtual string Talk() {
    switch (Sex) {
      case SexType.Male:
        return "I am a Male.";
      case SexType.Female:
        return "I am a Female.";
      default:
        return "I am a human.";
    }
  }
}

class Male : Human {
  public Male() {
    Sex = SexType.Male;
  }
}

class Female : Human {
  public Female() {
    Sex = SexType.Female;
  }
}

class Child : Human {
  public Child() {
  }
  public override string Talk() {
    switch (Sex) {
     case SexType.Male:
       return "I am a boy.";
     case SexType.Female:
       return "I am a girl.";
     default:
       return "I am a child.";
    }
  }
}

Upvotes: 2

Tom W
Tom W

Reputation: 5403

It sounds as though they intend you to demonstrate that you understand the limitations of simple inheritance in this exercise. You can't model Humans with both age and sex represented by inheritors in the most simple model of inheritance. Consider the following:

  • Abstract Class Human inherited by both Child and Adult;
  • Abstract Class Human inherited by Male and Female

It becomes obvious that in this model, a human cannot be both Child and Female - or any other combination. Therefore the answer is that this simple model is inadequate and instead you should suggest alternative implementations which fulfil the requirements while using the established model as best you can.

I shall repeat what I've found myself saying often:

Objects should not attempt to represent reality. A good object model only need solve the problem at hand.

What I mean by this is that the Child/Adult and Male/Female exclusivity relationships should be modelled in the most appropriate way that solves the problem at hand. It doesn't matter if they aren't representative of reality. In your position I would attempt to suggest a variety of alternatives that would suit varying problems and explain why each is beneficial in that situation.

Upvotes: 5

Anthony Pegram
Anthony Pegram

Reputation: 126804

Just to fit into their world and desired class hierarchy, which is not particularly indicative of how you might treat a person in a real application, you might have

class Human
{
     public string HairColor { get; set; }

     public virtual void Talk()
     {
         Console.WriteLine("I am a human");
     }
}

class Female : Human
{
     public override void Talk()
     {
         Console.WriteLine("I am a woman");
     }
}

class Male : Human
{
     public override void Talk()
     {
         Console.WriteLine("I am a man");
     }
}

class Boy : Male
{
     public override void Talk()
     {
         Console.WriteLine("I am a boy");
     }
}

class Girl : Female
{
     public override void Talk()
     {
         Console.WriteLine("I am a girl");
     }
}

This expresses the inheritance hierachy that a boy is a male is a human, and a girl is a female is a human. So you could write any of the below valid statements

Human human = new Human();
Human human = new Female();
Human human = new Girl();

Female female = new Female();
Female female = new Girl();

Girl girl = new Girl();

And anywhere that takes a Human could be passed a Female or a Girl.

Things such as this would not be valid

Girl girl = new Human();
Girl girl = new Female();
Female female = new Human();
Female female = new Male();
Female female = new Boy();

And naturally, anywhere expecting a Girl could not be passed a Human or Female, and certainly not a Male or Boy.

In the real world, as commenters have stated, you would typically not use inheritance in this scenario, but instead use properties to differentiate based on age or gender. And, for that matter. If using actual inheritance, you'd probably not opt to use Male or Female, as those descriptors are hardly unique to humans.

Upvotes: 3

Related Questions