tags:

views:

179

answers:

3

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

For 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(); and I also don't want to change the

modifier of quark method in Animal to private. Is there any way to prevent this in compile?

I have be confused couple of days, who can help me........

The reason I do that is I met the similar problem in developing hudson scm plugin.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? Dose java have

something like *reflect way*s to prevent this?

+2  A: 

No, there's no way to have a public method that subclasses can't call. The best you can do is document this recommendation.

As a note, it's called a subclass or child class.

Matthew Flaschen
@Downvoter, what part of this answer is incorrect?
Matthew Flaschen
Dose java havesomething like *reflect way*s to prevent this?
tangjinou
@tangjinou - No.
Stephen C
+5  A: 

No, by definition of public you can't stop the method from being called (whether from a derived class or anywhere else). You can of course stop it from being overridden (and thereby ensure that the syntax used to call it won't use super;-) by making it final.

Alex Martelli
You can call final methods with `super`. There's no real reason to do so, but it's allowed.
Matthew Flaschen
Firstly Thanks for your reply.
tangjinou
In real situation, I can't change anything in class Animal because it is written by others.
tangjinou
A: 

The other 2 answers (Alex & Matthew) are basically right.

  1. you can prevent the subclass from calling a public method from the super class
  2. there is probably something wrong with your design if you do this.

The below Father class has two public methods named fatherMethod. Subclasses can call the fatherMethod using reflection (Thank You Matthew for pointing that out.) Without using reflection, subclasses can probably not call either fatherMethod method. Thus it is very similar to a private method.

class Father
{
    private class Alpha { }

    private class Beta { }

    public void fatherMethod ( Alpha param )
    {
    }

    public void fatherMethod ( Beta param )
    {
    }
}
emory
The reason why I do that is that I met the similar problem in developing hudson scm plugin.Icreate the class extends the SubversionSCM(the offical class). I just want to override thepublic method of super class, then call super's public method back like example. but thecompile gave error.I don't konw why, how could it do?Dose java have something like reflect ways to prevent this?
tangjinou
Is the superclass method final? R U overriding it? Is it static? Is it generic? Have you double checked the parameter types? Double checked the names? Does it throw an exception?
emory
the subperclass is not final and static , I could override the public method but can't call the super method in my override method..
tangjinou
Do u put the @Override tag on the method? I suspect u r not really overriding the public method.
emory
If you have the @Override tag on the method and it is not really overidding the compiler will tell you.
emory
no @Override tag
tangjinou
http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why -- "Use it every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that you method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten."
emory
@emory, all of the classes in the question have a single method taking no parameters. Yet your class has two methods, each taking an instance of a private class. So it seems obvious you're ignoring the question in order to make a philosophical point. In that regard you've failed. It only requires trivial reflection to call one of the methods: `Father.class.getDeclaredMethods()[0].invoke(new Father(), new Object[]{null});`
Matthew Flaschen