0

Why constructor name is similar to the class name?

2nd Oct 2016, 5:42 AM
Saharsh Pandey
Saharsh Pandey - avatar
3 Antworten
+ 1
constructor is controlled by class. class a{} class b{ b(){} } when a class a is calls class b before starting execution of any function in class b constructors are first excuted. since constructors are used for calling private functions of other class or to reassign variable. so they should get excuted 1st. for this reason and to differentiate from other functions . class name n constructors name are same
2nd Oct 2016, 10:25 AM
Vipra Chudasama
Vipra Chudasama - avatar
0
when constructor is not defined manually then default constructor is created automatically to initialize variables. Every class has constructor. How would you know which constructor is for which class when you'll be working with multiple classes.. ? For the sake of simplicity constructors are of same name as class... !
2nd Oct 2016, 11:37 AM
chetan acharya
chetan acharya - avatar
0
If, by convention, the compiler insists that all methods with the same name as the class are constructors of that class, it means you don't have to explicitly identify methods as being constructors. In other OOP languages, there is a "constructor' keyword in the method signature and the constructor method can have any name. This can be useful because you can then name constructors more meaningfully, instead of relying on the different parameter lists (of overloaded constructors) to identify a constructor's purpose.
4th Oct 2016, 11:02 PM
Bazza Cuda
Bazza Cuda - avatar