0

I want to remove a method from a class that is present in it's super class. I can deprecate the superclass method using the @Deprecated annotation, but it is still accessible in the subclass.

Eg:

public class Sample {

    void one() {}

    void two() {}

    @Deprecated
    void three() {}
}

class Sample2 extends Sample {
    @Override
    void one() {}

    public static void main() {
        Sample2 obj = new Sample2();
        obj.one();
        obj.two();
        obj.three();// I do not want to access this method through the sample 2 object.
    }
}

While using the Sample2 object I only want methods one and two to be available. Please advice on how to do this.

Thanks a lot.

Perception
  • 79,279
  • 19
  • 185
  • 195
John Christy
  • 371
  • 3
  • 6
  • 22
  • Deprecated Doesn't mean that you cant access them. Read this http://stackoverflow.com/questions/2941900/is-it-wrong-to-use-deprecated-methods-or-classes-in-java @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. – Nikhil Agrawal Apr 19 '13 at 04:47
  • @ Nikhil Ok. How to make this scenario possible ? – John Christy Apr 19 '13 at 04:49
  • Is `Sample.three()` currently being called from outside `Sample`, or can you make it private? Currently it is only visible to other classes in the same package – Brad Aug 22 '14 at 12:53

4 Answers4

2

Override three() in Sample2 and throw an exception if that method is accessed.

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
1

There is nothing you can do at compile-time. You cannot have a subclass with less methods than a superclass. Best you can do is make a runtime error like @Sudhanshu proposes, and maybe some tooling (like custom FindBugs rules) to flag it an error in your IDE.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • If so How a method is removed in some version of Java release. – John Christy Apr 19 '13 at 05:02
  • 1
    So far, no method has ever been removed in Java releases. And even if they did, that would remove the method in a new version of the class. The type hierarchy would still remain consistent, i.e. you cannot have "incomplete" subclasses (provided you don't mix releases). – Thilo Apr 19 '13 at 05:54
0

Use the private access level modifier in front of methods that should only be accessed in their own classes.

public class Sample {

    void one() {}

    void two() {}

    @Deprecated
    private void three() {}
}
ZEuS
  • 171
  • 1
  • 6
  • This answer assumes you have the ability to edit the source for `Sample .java`, and that `Sample.three()` can be made private without breaking existing code. – Brad Aug 22 '14 at 12:50
0

One idea for hiding the interface of another class while still using it is to wrap it with your own object (i.e. don't subclass).

class MySample {
    private Sample sample;
    //maybe other stuff

    public MySample(){ 
        sample = new Sample();
    }

    void one(){
        return sample.one();
    }
}

This is potentially unsatisfactory: not wanting to use Sample in the entire way it was intended while simultaneously wanting to hijack and extend its behavior. It solves the problem of ever calling three() on your backing Sample.

clwhisk
  • 1,805
  • 1
  • 18
  • 17