1

I am running into the trouble of extending/altering private methods in subclasses. For instance, I am creating a mock object that inherits from a super class in order to be used in testing.

Here is an example of the code from the parent class that I would like to alter:

 private void build(int skill) {
        // switch screen
        state = Constants.STATE_GENERATING;
        percentdone = 0;
        notifyViewerRedraw() ;
        // select generation method
        switch(method){
        case 1 : mazebuilder = new MazeBuilderPrim(); // generate with Prim's algorithm
        break ;
        case 0: // generate with Falstad's original algorithm (0 and default), note the missing break statement
        default : mazebuilder = new MazeBuilder(); 
        break ;
        }
}

I know that private methods cannot be overwritten in subclasses. So therefore I should create a new method of the same signature. Would I also have to recreate all the private variables in this class too? But if I do that, I am unsure if that would change the behavior to be different from the parent class since the I know that space is actually reserved for private variables from the parent class in the subclass. Therefore, I would have duplicate private variables. I don't know what the best way is to approach this.

1
  • I may be misunderstanding but in the parent couldn't you make the methods and variables in question protected instead of private? Commented Sep 27, 2014 at 0:57

4 Answers 4

2

If you find the need to override a method in a subclass, perhaps the logic that method is responsible for is abstract enough to warrant a public or protected method.

Consider the design of the Strategy Pattern. Basically, there shouldn't be a need to override a private method, because those methods should be reserved for things outside your interface and only specific to that particular concrete class.

Something as integral and specific as build() to me sounds like it belongs as a protected method which your highest superclass may use at some point during construction but which shouldn't be called externally. Of course, if it's safe to call build() as many times as necessary (idempotent), like a render() method might be in a game character class, then it should be safe to make it public and document what your expectations are of its implementation.

Sign up to request clarification or add additional context in comments.

Comments

1

Try using mocking API, for example Jmockit .

Using it, will save you a lot of trouble doing hand mocks, like in your case extending class with private methods! Good luck.

Comments

0

Im not sure how your program works overall but it might be worth you looking into abstract classes and methods if you want all of the base classes to share the same methods(?)

Comments

0

You can call super() in the subclasses method which will call the superclasses method and then make the extra changes you need in the subclasses method.

Edit: Read the original question wrong. If a method has functionality in the superclass and you want to extend or alter it, you shouldn't set it to private. Protected is probably the best bet in your case, as it allows subclasses to extend it using super() or just override it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.