0

I'M reading the book that's in the title. Let me show you what's going on.

I'M given the following code, an example of Inheritance.

using System;

namespace CritClone
{
    class CritViewer
    {
        static void Main(string[] args)
        {
            CritViewer cv = new CritViewer();
        }// end main

        public CritViewer()
        {
            Clone myClone = new Clone();
            myClone.name = "Dolly";
            Console.WriteLine(myClone.talk());

            Console.WriteLine("Please press Enter to continue");
            Console.ReadLine();
        }// end constructor
    }// end critviewer
}// end namespace

And then the next page gives me this part.

using System;

namespace CritClone
{
    public class Clone : Critter
    {
        // nothing here
    }
}

But it looks to me like it's two different files that have no connection. Also, did I miss the creating the talk() method? I don't understand what's going on here, the book seems to be going all over the place and I'M unable to follow. Am I doing something wrong?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by apegram
2

One recent thread you might want to look at is this one on instantiating on object, because it deals with inheritance, as well.

As for this particular example, it looks like the code is missing the definition of the Critter class. From the code example, it looks like it should have a public name property along with a public Talk method.

So let's assume that exists.

public class Critter 
{
      public string name { get; set; }
      public string Talk() { return "Hi, my name is " + this.name; }
}

(Forgive any errors, I'm typing this directly into the page without validating it in the VS IDE.)

In this case, your Clone class inherits from Critter, so all of the public members of Critter are accessible when using an object of type Clone. A Clone is a Critter, so a Clone has a name and can Talk. It is not true, however, that a Critter is a Clone. So if you defined additional properties or methods within Clone, they would only be accessible to Clone and any object that derives from Clone.

On that note, you cannot set up an inheritance cycle such that Clone : Critter and Critter : Clone. That's not permitted and, frankly, would probably disrupt the space-time continuum and destroy the universe as we know it.

Edited by apegram: n/a

Votes + Comments
informative and amusing :) +rep
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.