So far we have discussed the basics of dart programming language and now we will move to the more important concept which will be used in our app development process. Yes, it is Object-oriented programming concepts.
"The first and foremost important thing is that everything in dart is an object "
You have heard the terms Class and Objects and might be thinking what are they?
So let me explain you these terms using some analogy. Let us assume that you are working at a car manufacturing company and you have to manufacture a certain model of car. So the first thing that you have is the Blueprint of the car of that model, and then by using that model, you will manufacture as many cars as you want.
Here the Blueprint is analogous to the Class and the actual car that is made from that blueprint is analogous to Object.
As the blueprint can be used as many times as we wish to make the actual car so we can say that Classes are the templates for creating objects.
Now we will see what are the different elements present in a Class.
Class Contains:
Now we will see how to create a class.
Also to access these variables we can use the (.) dot operator.
Similarly, we can add methods to the class.
We can use the same dot operator to access methods also.
Now we will see how to create an object of given class
So I hope that you have now a clear understanding of Classes and Objects and In the next Blog I will focus mainly on Constructors and Setter and Getter.
"The first and foremost important thing is that everything in dart is an object "
You have heard the terms Class and Objects and might be thinking what are they?
So let me explain you these terms using some analogy. Let us assume that you are working at a car manufacturing company and you have to manufacture a certain model of car. So the first thing that you have is the Blueprint of the car of that model, and then by using that model, you will manufacture as many cars as you want.
Here the Blueprint is analogous to the Class and the actual car that is made from that blueprint is analogous to Object.
As the blueprint can be used as many times as we wish to make the actual car so we can say that Classes are the templates for creating objects.
Now we will see what are the different elements present in a Class.
Class Contains:
- properties or attributes: Ex: color, name, type etc
- Methods: Ex:move(), brake(),etc
Objects of a given class will have all the above properties.
class Car{
//Instance variables, member variables
String name;
String color;
int model;
}
Also to access these variables we can use the (.) dot operator.
Similarly, we can add methods to the class.
class Car{
//Instance variables, member variables
String name;
String color;
int model;
// Adding Methods
void applyBrake(){
print("Brake Applied");
}
}
We can use the same dot operator to access methods also.
Now we will see how to create an object of given class
main()
{
//Creating Object of class Car
var car = new Car();
car.name = "Bugatti";
car.color = "black";
car.model = 3456;
}
So I hope that you have now a clear understanding of Classes and Objects and In the next Blog I will focus mainly on Constructors and Setter and Getter.
Comments
Post a Comment