Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 abstract class, why is the private access modifier available within an abstract class, and what is the scope of it within an abstract class? In which scenario is the private access specifier used in an abstract class?

check out this code where Vehicle class is abstract and Car extends Vehicle.

package com.vehicle;  abstract class Vehicle {   // What is the scope of the private access modifier within an abstract class, even though  method below cannot be accessed??   private void onLights(){    System.out.println("Switch on Lights");   }    public void startEngine(){    System.out.println("Start Engine");   }  } 

Within is the same package creating a Car class

package com.vehicle; /*  * Car class extends the abstract class Vehicle  */ public class Car extends Vehicle {   public static void main(String args[]){   Car c =  new Car();   c.startEngine();   // Only startEngine() can be accessed   }  } 
like image 258
Srinivas M.V. Avatar asked Jul 26 '10 11:07

Srinivas M.V.


People also ask

Why does abstract class can have private method?

If a method of a class is private, you cannot access it outside the current class, not even from the child classes of it. But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use. Therefore, the abstract method cannot be private.

Why you couldn't create an instance from an abstract class?

We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.

What is the purpose of private access modifier?

Private Access ModifierAllows a variable or method to only be accessed in the class in which it was created. No other class beyond the class that created the variable or method can access it. This is closely similar to your internal organs. They are only accessible to the owner.


1 Answers

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.

like image 97
Andrei Fierbinteanu Avatar answered Sep 17 '22 20:09

Andrei Fierbinteanu