Usually in interviews where you have to compile and run a code, you do so in a single file. But in cases of solving Low Level Design problems (especially in Java) you can’t have all the classes and interface in a single file because Java doesn’t allow it.
So a possible solution of this problem is to have an instance of Main class and use that instance to call a function which creates object for all the various subclasses.
public class Main {
public interface Logistics {
void planDelivery();
}
public class RoadLogistics implements Logistics {
@Override
public void planDelivery() {
System.out.println("hsjf");
}
}
public static void main(String[] args) {
Main main = new Main();
String transportType = "road";
Logistics logistics = main.rL();
logistics.planDelivery();
}
public RoadLogistics rL(){
return new RoadLogistics();
}}
Here in the main function I have created a main object and then used that to call another member function which just creates a new object of a subclass of main.
So my question is that whether in interviews is this the only method to solve LLD problems where we have to create multiple interfaces and classes? Or is there something which I am unaware of (in terms of Java implementation).
Also I have never had an interview where I had to solve a Low Level Design problem in Java, so I don’t know if I will be allowed to use multiple files for various Java classes, so any experiences of interviews relevant to this will also be helpful.