How to access overridden methods of superclass of a superclass in Java?

Let us consider an example:

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();

It produces expected output


The point is that there is no way to call test() method of the TestClass1 class!


won’t compile but


will produce java.lang.StackOverflowError.

Despite that we can access superclass members of any level by casting this to appropriate class (see The Java Language Specification at, polymorphism allows us to access immediate superclass only, not overridden methods of higher levels (see The Java Language Specification at

Author: Artem's Blog

Working on software and more...

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s