Why is there a private access modifier in an abstract class in Java, even though we cannot create an instance of an abstract class?

后端 未结 4 486
误落风尘
误落风尘 2020-12-08 11:25

I know it is not a good coding practice to declare a method as private in an abstract class. Even though we cannot create an instance of an

相关标签:
4条回答
  • 2020-12-08 11:32

    The method can be accessed only from within the abstract class. For example, you could have an abstract class with a public final method that makes use of a private helper method.

    0 讨论(0)
  • 2020-12-08 11:35

    It's the same as in a non-abstract class, there's no difference.

    Which means that if nothing in your abstract class calls the private method, then you can just as well remove it, as it won't be called (baring some evil reflection work).

    Usually, private methods are only used as internal utility methods that have a very specific task that the other methods in the class use to do their work.

    0 讨论(0)
  • 2020-12-08 11:38

    I know it is not a good coding practice to declare a method as private in an abstract class.

    I don't. Where did you get that idea?

    what is the scope of it within an abstract class?

    The abstract class.

    0 讨论(0)
  • 2020-12-08 11:55

    Since an abstract class can contain functionality (as opposed to an interface) it can have private variables or methods.

    In your example you might do something like

     public void startEngine(){
       injectFuel();
       igniteSpark();
       // etc. my understanding of engines is limited at best
       System.out.println("Start Engine");
     }
    
     private void injectFuel() {}
     private void igniteSpark() {}
    

    That way you can spread some of the work to other methods (so you don't have a 1000 line startEngine method), but you don't want the children to be able to call injectFuel separately since it doesn't make sense outside the context of startEngine (you want to make sure it's only used there).

    Or even more you might have a private method that gets called in several other public methods, with different parameters. This way you avoid writing the same code twice or more in each of the public methods, and grouping the common code in a private method makes sure the children don't access it (like they couldn't just call part of the public method before). Something like this:

     public void startEngine() {
       dishargeBattery(50);
       System.out.println("Start Engine");
     }
    
     public void startRadio() {
       dischargeBattery(20);
     }
    
     private void dischargeBattery(int value) {
       battery.energy -= value; //battery should probably be a private field.
     }
    

    This way your methods can have access to the battery, but the children shouldn't mess with it, and you don't write the same line (battery.energy -= value) in both of them. Take note though, that these are very simple examples, but if dischargeBattery was a 500 line method, writing it in both the other methods would be a hassle.

    0 讨论(0)
提交回复
热议问题