Can't declare an abstract method private....

Posted by Zombies on Stack Overflow See other posts from Stack Overflow or by Zombies
Published on 2010-05-20T13:54:41Z Indexed on 2010/05/20 14:00 UTC
Read the original article Hit count: 166

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();

© Stack Overflow or respective owner

Related posts about java

Related posts about object-oriented-design