Every class requires a constructor to be present init if we want to create the instance of that class.
Every class contains an implicit constructor if not defined explicitly and with the help of that implicit constructor instance of class can be created.
What is the need of defining a constructor explicitly again ?
Implicit constructor of a class will initialize variables of a class with the same value even if we create multiple instance of that class.
If we define constructor explicitly with parameters then we get a change of initializing the fields or variables of the class with a new value every time we are going to create instance of that class.
When ever we define a class identify whether if the class variables requires some values to execute and if they are required then define a constructor explicitly and pass values thru that constructor , so that every time the instance of the class is created we get a chance of passing new values.
Note : Generally every class requires some values for execution and the values that is required for a class to execute are always sent to that class by using the constructor only.
0 Comments:
Post a Comment