xxxxxxxxxx
public interface SomeInterface{
public int someMethod();
}
public class Independent implements SomeInterface{
@Override
public int someMethod(){
return 2; // Your logic
}
}
public class Dependent{
// "Independent" class will be replaced the interface it implements: "SomeInterface"
private SomeInterface someObj; // NOT private Independent someObj;
// Re-write constructor in a way that it takes an interface instead of a class
public Dependent(SomeInterface someObj){
this.someObj = someObj; // NOT "someObj = new Independent();"
}
}
class Main {
public static void main(String[] args) {
var independentObj = New Independent();
// Main class is acting as an injector class
var dependentObj = New Dependent(independentObj); // Injecting the "independentObj" inside Dependent's constructor
}
}