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, why there is private access specifier within an abstract class , what is the scope of it within an abstract class? In which scenario does private access specifier are 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 private access specifier within an abstract class ?? even though below method 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
}
}
Thanks!