Say we have a class called Maze. Now suppose that we have another class called MazeSolver. So in order to create the Maze object in MazeSolver for the reason of using the methods from Maze, we create private Maze maze. Then within the constructor of MazeSolver, we also write public MazeSolver(Maze maze). My question is, why do we have to do both? What is the philosophy behind the idea? Why can't we do one or the other opposed to doing both is I guess where I am confused.
Add a comment
|
2 Answers
Here you have two options first one is you can use like this
public MazeSolver(Maze maze) {
this.maze = maze;
}
second option is
public MazeSolver() {
maze = new maza();;
}
Actually your requirement is you need to use maza behavior so in that case you need one instance of maza class( if those method are non-static). that's why you need to initialize or you assign object to maza
I hope this will help you to understand your requirement