Constructors in Dart
Hello everyone!
Today I am here to share the concept of constructors in Object Oriented Programming (OOP) in dart. Constructors are very important. The constructors have the same name as the name of the class.

These are called when the object or instance of the class is created. There is no need to call them separately they are called as soon as the object of the class is created. Constructors are used to initialize the values.
There are different types of constructors normally we use simple constructor and parametric constructors. The parametric constructors accept parameters and we can pass the values as parameters. When the class is called in the main then those parameters are required to add there.

Here you can see I have created a class product. Then I have created the constructor of the class product. I have used a parametric constructor. I have passed productName, price, and quantity as parameter. In the body of the constructor I have used a print statement. Now it will be called right after creating the object of the class.

Here you can see in the main I have created the object p of the class product. It required me to add the parameters to the class as defined in the constructor.

Look at the output of the program. You can see as soon as I created the object of the class the constructor was called automatically and it completed its assigned task.

Here is another example of the parametric constructors. I created a Book class. Then I created the constructor of the class and I passed the parameters to the constructor. In the body of the constructor I defined some conditions to check whether the book is short or long.
In the main I am getting user input to dynamically get the values of all the parameters and then passing those values to the class.
Sp in this way we can play with the constructors in the object oriented programming. The concept of the constructors is widely used in the programming in all the languages.
🎉 Congratulations!
Your post has been upvoted by the SteemX Team! 🚀
SteemX is a modern, user-friendly and powerful platform built for the Steem community.
🔗 Visit us: www.steemx.org
✅ Support our work — Vote for our witness: bountyking5
🎉 Congratulations, @mohammadfaisal!
Your post Constructors in Dart was rewarded by CCS Curation Trail
“Home is where your heart is ❤️.”
👉 Join the CCS Curation Trail | Community Invitation
✅ Vote for visionaer3003 as witness
Visit: https://steemhop.org