×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Java question: it that possible to refer to the method of super super class?

本文发表在 rolia.net 枫下论坛For some reason, I need to make a little customization on a Java open source API.

There are 2 classes in this API.

public class Grandfather
{
public add( Object a)
{
......

}
}

public class Father extends Grandfather
{

public add( Object a )
{
super.add(a);
specific process in Father.....
}

}

In my case, the "specific process" in class Father doesn't meet my requirement. I need it to have the special behaviors
under some certain condition. However, those conditions cannot be distinguished from inside
this class's perspective or by the parameter passed in. Therefore, adding in a new processing flow wouldn't help solve this problem

Then I tried to create a class named Uncle that extends Grandfather and overrode the method "add" with my customized code.
Unfortunately, there's cast operation somewhere else in this API. So when I ran my program, a ClassCastException
was thrown out.

It means I'd better use the children classes of Father if I don't want to get more complex work.
I guess the easiest way to work this around is to have my class inherited from the class Father and override the
method.

My question is, is that possible to refer to the Grandfather's method "add" from Self? If yes, how?

Thank you.更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Java question: it that possible to refer to the method of super super class?
    本文发表在 rolia.net 枫下论坛For some reason, I need to make a little customization on a Java open source API.

    There are 2 classes in this API.

    public class Grandfather
    {
    public add( Object a)
    {
    ......

    }
    }

    public class Father extends Grandfather
    {

    public add( Object a )
    {
    super.add(a);
    specific process in Father.....
    }

    }

    In my case, the "specific process" in class Father doesn't meet my requirement. I need it to have the special behaviors
    under some certain condition. However, those conditions cannot be distinguished from inside
    this class's perspective or by the parameter passed in. Therefore, adding in a new processing flow wouldn't help solve this problem

    Then I tried to create a class named Uncle that extends Grandfather and overrode the method "add" with my customized code.
    Unfortunately, there's cast operation somewhere else in this API. So when I ran my program, a ClassCastException
    was thrown out.

    It means I'd better use the children classes of Father if I don't want to get more complex work.
    I guess the easiest way to work this around is to have my class inherited from the class Father and override the
    method.

    My question is, is that possible to refer to the Grandfather's method "add" from Self? If yes, how?

    Thank you.更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • try this
      You can try. I can't guarantee it works.

      public class Son extends Father {
      public add(Object a) {
      ((GrandFather)this).add(a);
      ......
      }
      }
      • This way should work.
        • 可能吧
          • 都是你,害得我连偷偷改回来的机会都没有了。呵呵!
        • 哎,我想写的是This way should not work. 可真是的。
    • how about create a special method, say superAdd() in Father to call GrandFather's add(), then in your grandson object, use super.superAdd()?
    • 1. I don't think smallwhale's would work. 2. tongcd's no good either :) remember, father is not your code, next time the api get upgrade, you have to modify father again. 3. imho, getting your uncle is the best way.