Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't declare an abstract method private

Tags:

java

oop

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();
like image 318
Zombies Avatar asked May 20 '10 13:05

Zombies


People also ask

Can we have private abstract method in Java?

You can't have private abstract methods in Java. When a method is private , the sub classes can't access it, hence they can't override it. If you want a similar behavior you'll need protected abstract method.

Can an interface have private abstract methods?

Rules For using Private Methods in InterfacesPrivate interface method cannot be abstract and no private and abstract modifiers together.

Can abstract class have private fields?

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.


3 Answers

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.

like image 73
Jesper Avatar answered Oct 05 '22 14:10

Jesper


I'd like to point out that it is the choice of Java not to let abstract private methods implemented (e.g. in C++ you can and is advisable). Saying that private methods are not polymorphic in general is just ignorant of the fact that calling and defining a method is two different mechanism - although subclasses can never call a private method of a superclass, they could be able to define it. Mixing the two is a common misconception, please be aware.

I don't have enough reputation to comment, that's why I'm writing an answer.

like image 11
Viktor Tóth Avatar answered Oct 05 '22 14:10

Viktor Tóth


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.

like image 4
duffymo Avatar answered Oct 05 '22 15:10

duffymo