views:

68

answers:

1

Possible Duplicate:
Is there any way to forbid the son class to call the public method of super class in java?

some days ago, when I develop the plugin for hudson(a Continue integrate tool), I met the this

problem . I created the class which extends the SubversionSCM(the offical class). I just

wanted to override the public method of super class, then call super's public method back like

example. but the compile gave error.I don't konw why, how could it do?

the real situation is like following example.

public abstract class TObject{
    abstract public void quark();
}


public class Animal extends TObject{

       public void quark(){
           System.out.println("this is the animal");
       }

}

public class Dog extends Animal{
       @overide
       public void quark(){
           System.out.println("this is the animal");
           **super.quark();**
       }
} 

In this example, The Dog call the super.quark(); in it's quark method.

But I don't want the Dog could call super.quark();the Animal class is written by

others, so can't be changed.....

anyone who can help me?

A: 

As to your first question: if you were overriding something marked final (either the class or the method) the compiler would yield an error.

As to the second you could meddle with reflection. For instance if you were the author of TObject and did not want anybody to extend your class more than one node you could do something like this:

public class BaseClass {
  public void apiMethod() {
    if(check(0, getClass()) > 1) {
      throw new IllegalStateException("Too much inheritance");
    }
  }
  private int check(int prev, Class c) {
    if(c.equals( BaseClass.class)) {
      return prev; 
    }
    else { 
      return check(++prev, c.getSuperClass());
    }
  }
}

But that would be sneaky...

may be this is a good solution.. Thanks
tangjinou