tags:

views:

108

answers:

6

I want to do this, yet I can't. Here is my scenario and rational. I have an abstract class for test cases that has an abstract method called test(). The test() method is to be defined by the subclass; it is to be implemented with logic for a certain application, such as CRMAppTestCase extends CompanyTestCase. I don't want the test() method to be invoked directly, I want the super class to call the test() method while the sub class can call a method which calls this (and does other work too, such as setting a current date-time right before the test is executed for example). Example code:

public abstract class CompanyTestCase {
    //I wish this would compile, but it cannot be declared private
    private abstract void test();

    public TestCaseResult performTest() {
        //do some work which must be done and should be invoked whenever 
        //this method is called (it would be improper to expect the caller
        // to perform initialization)
       TestCaseResult result = new TestCaseResult();
       result.setBeginTime(new Date());
       long time = System.currentTimeMillis();
       test(); //invoke test logic
       result.setDuration(System.currentTimeMillis() - time);
       return result;
    }
}

Then to extend this....

public class CRMAppTestCase extends CompanyTestCase {

    public void test() {
        //test logic here
    }

}

Then to call it....

TestCaseResult result = new CRMAppTestCase().performTest();
+15  A: 

Private methods are not polymorphic (you cannot inherit them), so it makes no sense to make a private method abstract. Making a method abstract means you'd have to override and implement it in a subclass, but since you can't override private methods, you can't make them abstract either.

You should make it protected instead of private.

Private really means private to the class you've defined the method in; even subclasses don't see private methods.

Jesper
+3  A: 

'private' means that the method can only be called by the class. It must therefore be defined on the class, and so can't be abstract. If you want to define the method elsewhere you will have to make it something other than private.

DJClayworth
+2  A: 

I want to do this, yet I can't

Changing this will require rewriting the compiler, which isn't likely.

You realize why this can never work, right? A subclass can't override a private method, yet, it's abstract which says they must. Catch-22.

duffymo
A: 

abstract means you'll want to define the method in a child class

private means the child classes won't see the method

How could it compile ?

If the method should end up public, declare it public abstract, that's all.

Maxime ARNSTAMM
A: 

You should declare your "test" method as "protected" to achieve your goal. But it still will be accessible from the package of the class.

Vitaly Bogdanov
+1  A: 

You cannot have a private abstract method because subclasses can't see private members of a superclass. You need to make your test method protected.

dbyrne