<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx</link><description>A question I got recently was about access to protected methods from a derived class. Clearly that's what "protected" means – that you can access it from a derived class. In that case, why doesn't this work? 
 class Ungulate { protected void Eat() {</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#9936863</link><pubDate>Tue, 15 Dec 2009 02:40:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9936863</guid><dc:creator>Jesse</dc:creator><description>&lt;p&gt;But the compiler's static type analysis can't detect this situation:&lt;/p&gt;
&lt;p&gt;class RoboGiraffe : Giraffe {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;protected override void Eat() { /* use dangerous lasers that only RoboGiraffe is trained for */ }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Now when we say g1.Eat(), we may be calling RoboGiraffe.Eat, which should only be accessible from RoboGiraffe and its subclasses, right? Shouldn't that be prohibited under the very same logic that prevents us from calling Zebra.Eat?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9936863" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#8344110</link><pubDate>Sun, 30 Mar 2008 11:23:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8344110</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;That's not a bug. I'll be writing about that feature in my blog next week. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8344110" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#8344101</link><pubDate>Sun, 30 Mar 2008 11:02:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8344101</guid><dc:creator>Frederik Siekmann</dc:creator><description>&lt;p&gt;Well.&lt;/p&gt;
&lt;p&gt;While this doesn't compile:&lt;/p&gt;
&lt;p&gt;class Ungulate {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;protected void Eat() { /* whatever */ }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;class Giraffe : Ungulate {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public static void FeedThem() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Giraffe g1 = new Giraffe();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Ungulate g2 = new Giraffe();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;g1.Eat(); // fine&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;g2.Eat(); // compile-time error &amp;quot;Cannot access protected member&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Making a subtile change (adding internal):&lt;/p&gt;
&lt;p&gt;class Ungulate {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;protected internal void Eat() { /* whatever */ }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;class Giraffe : Ungulate {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public static void FeedThem() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Giraffe g1 = new Giraffe();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Ungulate g2 = new Giraffe();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;g1.Eat(); // fine&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;g2.Eat(); // compile-time error &amp;quot;Cannot access protected member&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Tadaa: this will compile ... and I am really happy about that ... please don't fix this bug ;)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8344101" width="1" height="1"&gt;</description></item><item><title>Why Can't I Access A Protected Member From A Derived Class, Part Two: Why Can I?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#8342570</link><pubDate>Sat, 29 Mar 2008 02:30:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8342570</guid><dc:creator>Fabulous Adventures In Coding</dc:creator><description>&lt;p&gt;This is a follow-up to my 2005 post on the same subject which I believe sets a personal record for the&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8342570" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#496472</link><pubDate>Thu, 24 Nov 2005 01:14:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:496472</guid><dc:creator>Eric Lippert</dc:creator><description>I've only ever written one program in Java and that was about ten years ago, so I'm a bad person to ask about Java semantics.  &lt;br&gt;&lt;br&gt;However, my reading of section 6.6.2 of my 1996 copy of the JLS is that in your example, the semantics of &amp;quot;protected&amp;quot; do not apply because the two classes are in the same package.  &lt;br&gt;&lt;br&gt;If you read section 6.6.7, it describes the semantics of cross-package access to protected members via subclasses as being the same as I describe above.  &lt;br&gt;&lt;br&gt;Apparently in Java, &amp;quot;protected&amp;quot; means roughly what &amp;quot;Protected Friend&amp;quot; means in Visual Basic.&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=496472" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#493765</link><pubDate>Thu, 17 Nov 2005 09:04:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:493765</guid><dc:creator>Abhijit</dc:creator><description>I am not sure how this figures but I tried your example on JVM out of curiosity and it worked without any errors. This is what I used - &lt;br&gt;public class Mammal {&lt;br&gt;	protected void Eat() { &lt;br&gt;		System.out.println(&amp;quot;In Mammal.eat()&amp;quot;); &lt;br&gt;	}&lt;br&gt;}&lt;br&gt;public class Giraffe extends Mammal {&lt;br&gt;	public static void FeedThem() {&lt;br&gt;		Giraffe g1 = new Giraffe();&lt;br&gt;		Mammal g2 = new Giraffe();&lt;br&gt;		g1.Eat(); // fine&lt;br&gt;		g2.Eat(); // fine&lt;br&gt;	  }&lt;br&gt;	public static void main(String[] args) {&lt;br&gt;		Giraffe.FeedThem();&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;&lt;br&gt;This might sound like a dumb question but if what you have shown is specific to C# then what is the rationale behind having such checking?&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=493765" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#491289</link><pubDate>Thu, 10 Nov 2005 17:26:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491289</guid><dc:creator>Eric Lippert</dc:creator><description>But being in an ungulate-derived class is in this example not enough information to guarantee that you are in a derived class of the RUN TIME type of the callee. &lt;br&gt;&lt;br&gt;Really what you're saying is that &amp;quot;protected&amp;quot; could be redefined to mean something weaker, such as &amp;quot;callable from any class derived from the base class which declares the protected method&amp;quot;. &lt;br&gt;&lt;br&gt;But that's not the kind of protection we've chosen as interesting or valuable.  &amp;quot;Sibling&amp;quot; classes do _not_ get to be friendly with each other because otherwise protection is very little protection.&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=491289" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#491216</link><pubDate>Thu, 10 Nov 2005 12:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491216</guid><dc:creator>Erno</dc:creator><description>I think that the real problem is that the FeedThem method is in the wrong class. As soon as you put FeedThem in the right place you will not need the construction you just created.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=491216" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx#491207</link><pubDate>Thu, 10 Nov 2005 11:42:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:491207</guid><dc:creator>Paulo Morgado</dc:creator><description>The compiler surelly knows that it's in a Ungulate derived class. So, the rule could be another one.&lt;br&gt;Nevertheless, the rule makes sense to me.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=491207" width="1" height="1"&gt;</description></item></channel></rss>