<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Artem&#039;s blog &#187; method</title>
	<atom:link href="http://artemgolubev.com/tag/method/feed/" rel="self" type="application/rss+xml" />
	<link>http://artemgolubev.com</link>
	<description>Thoughts on software</description>
	<lastBuildDate>Sat, 17 Dec 2011 01:52:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to access overridden methods of superclass of a superclass in Java?</title>
		<link>http://artemgolubev.com/no-way-to-access-overridden-methods-of-superclass-of-superclass-in-java/</link>
		<comments>http://artemgolubev.com/no-way-to-access-overridden-methods-of-superclass-of-superclass-in-java/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 03:42:28 +0000</pubDate>
		<dc:creator>Artem</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[override]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[super]]></category>
		<category><![CDATA[superclass]]></category>

		<guid isPermaLink="false">http://artemgolubev.com/no-way-to-access-overridden-methods-of-superclass-of-superclass-in-java/</guid>
		<description><![CDATA[Let us consider an example:

public class Test2 {
  static class TestClass1 {
      int x = 1;
      String test() {
          return "1";
      }
  }

  static class TestClass2 extends TestClass1 [...]]]></description>
			<content:encoded><![CDATA[<p>Let us consider an example:</p>
<pre>
public class Test2 {
  static class TestClass1 {
      int x = 1;
      String test() {
          return "1";
      }
  }

  static class TestClass2 extends TestClass1 {
      int x = 2;
      String test() {
          return "2";
      }
  }

  static class TestClass3 extends TestClass2 {
      int x = 3;
      int testX() {
          return ((TestClass1)this).x;
      }
      String test() {
          return super.test();
      }
  }

  public static void main(String[] args) {
      TestClass3 obj = new TestClass3();
      System.out.println(obj.testX());
      System.out.println(obj.test());
  }
}</pre>
<p>It produces expected output<br />
<code><br />
1<br />
2<br />
</code></p>
<p>The point is that there is no way to call test() method of the TestClass1 class!<br />
<code><br />
super.super.test()<br />
</code><br />
won&#8217;t compile but<br />
<code><br />
((TestClass1)this).test()<br />
</code><br />
will produce java.lang.StackOverflowError.</p>
<p>Despite that we can access superclass members of any level by casting this to appropriate class (see The Java Language Specification at <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.11.2">http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.11.2</a>), polymorphism allows us to access immediate superclass only, not overridden methods of higher levels (see The Java Language Specification at <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.4.9">http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.4.9</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://artemgolubev.com/no-way-to-access-overridden-methods-of-superclass-of-superclass-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

