Junior — Middle
Can you explain the concept of constructor overloading in object-oriented programming?
sobes.tech AI
Answer from AI
Method overloading is the ability in object-oriented programming to create multiple constructors in a class with different parameters. This allows creating objects of the class in different ways depending on the arguments passed.
For example, in Dart (used in Flutter), you can define multiple constructors with different names:
class User {
String name;
int age;
User(this.name, this.age); // main constructor
User.withName(this.name) : age = 0; // constructor with one parameter
User.anonymous() : name = 'Guest', age = 0; // constructor without parameters
}
This approach simplifies creating objects with different initial data.