Connecting Tech Pros Worldwide Forums | Help | Site Map

General question about inheritance

GeneralCody
Guest
 
Posts: n/a
#1: Mar 8 '06
Hi!

I'm just starting out in C# and I am wondering why the Cow objects gets a
"inaccessible due to protection level" in the following code:

namespace ClassesAndInheritance
{
class Program
{
static void Main(string[] args)
{
Cow myCow = new Cow();
myCow.EatFood();
Console.ReadKey();
}

}

class Animal
{
protected void EatFood()
{
Console.WriteLine("Mmmm... Yummy! I am an animal...");
}
protected void Breed()
{
Console.WriteLine("Hey! Here comes another!");
}

}
class Cow : Animal
{
protected void Moo()
{
Console.WriteLine("Moooo!");
}
protected void SupplyMilk()
{
Console.WriteLine("I am a cow... I give you milk...");
}

}
}


--
Life is never boring.

GeneralCody

Michael Groeger
Guest
 
Posts: n/a
#2: Mar 8 '06

re: General question about inheritance


Because you declared EatFood() as protected. Protected members are only
accessible from inherited classes, but not from outside. You have to declare
EatFood() as either public (make it accessible for everyone) or as internal
(public for everyone within the same assembly).

Michael

"GeneralCody" <GeneralCody@discussions.microsoft.com> schrieb im Newsbeitrag
news:97025B5B-A517-4BFD-8F19-883BE247301C@microsoft.com...[color=blue]
> Hi!
>
> I'm just starting out in C# and I am wondering why the Cow objects gets a
> "inaccessible due to protection level" in the following code:
>
> namespace ClassesAndInheritance
> {
> class Program
> {
> static void Main(string[] args)
> {
> Cow myCow = new Cow();
> myCow.EatFood();
> Console.ReadKey();
> }
>
> }
>
> class Animal
> {
> protected void EatFood()
> {
> Console.WriteLine("Mmmm... Yummy! I am an animal...");
> }
> protected void Breed()
> {
> Console.WriteLine("Hey! Here comes another!");
> }
>
> }
> class Cow : Animal
> {
> protected void Moo()
> {
> Console.WriteLine("Moooo!");
> }
> protected void SupplyMilk()
> {
> Console.WriteLine("I am a cow... I give you milk...");
> }
>
> }
> }
>
>
> --
> Life is never boring.
>
> GeneralCody[/color]


Truong Hong Thi
Guest
 
Posts: n/a
#3: Mar 8 '06

re: General question about inheritance


Method EatFood is declared as protected. You cannot access it from
Program which is not a subclass of Animal.

I think that Console class does not have ReadKey method.

JDC
Guest
 
Posts: n/a
#4: Mar 8 '06

re: General question about inheritance


Hi,

You get that error because EatFood() is only visible to the Animal
class, and classes derived from it (i.e. Cow).

Perhaps if you never intend to create an Animal directly, you could
think about making it abstract, and using virtual methods:

abstract public class Animal
{
public virtual void EatFood()
{
Console.WriteLine("Mmmm... Yummy! I am an animal...");
}
}

public class Cow : Animal
{
public override void EatFood()
{
base.EatFood();
Console.WriteLine("...and I'm eating cow feed!");
}

}

Notice how EatFood() is now publicly accessible to your Main() method.

Cheers, J

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Mar 8 '06

re: General question about inheritance


Truong Hong Thi <thi1981@gmail.com> wrote:

<snip>
[color=blue]
> I think that Console class does not have ReadKey method.[/color]

Just FYI really: ReadKey is a new method in 2.0.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
GeneralCody
Guest
 
Posts: n/a
#6: Mar 8 '06

re: General question about inheritance


Thanks! This newsgroup is great.
--
Life is never boring.

GeneralCody


"GeneralCody" wrote:
[color=blue]
> Hi!
>
> I'm just starting out in C# and I am wondering why the Cow objects gets a
> "inaccessible due to protection level" in the following code:
>
> namespace ClassesAndInheritance
> {
> class Program
> {
> static void Main(string[] args)
> {
> Cow myCow = new Cow();
> myCow.EatFood();
> Console.ReadKey();
> }
>
> }
>
> class Animal
> {
> protected void EatFood()
> {
> Console.WriteLine("Mmmm... Yummy! I am an animal...");
> }
> protected void Breed()
> {
> Console.WriteLine("Hey! Here comes another!");
> }
>
> }
> class Cow : Animal
> {
> protected void Moo()
> {
> Console.WriteLine("Moooo!");
> }
> protected void SupplyMilk()
> {
> Console.WriteLine("I am a cow... I give you milk...");
> }
>
> }
> }
>
>
> --
> Life is never boring.
>
> GeneralCody[/color]
Closed Thread