Classes and Objects
classes are blueprint . Name of Classes starts with capital letter.
classes have i)properties
ii)Methods - functions in Class
Example
Object of class
classes and objects
void main(){
Human jenny = Human(15); // here we created jenny as object in class of Human
print(jenny.height);
jenny.talk('Why gods create this universe');
}
class Human {
double height;
int age = 0;
Human(double startingHeight){
height = startingHeight;
}
void talk(String whatTosay){ // talk is method and call of method as shown above
print(whatTosay);
}
}
Inheritance
we can create more classes based on another class or we simply say that this we can create class which has inherited property and method from other classes OR PARENT CLASSES
void main(){
Car myCar = Car();
myCar.drive();
print(myCar.number_of_seats);
ElectricCar myPhoton = ElectricCar();
myPhoton.drive();
myPhoton.batterylevel;
}
class Car {
int number_of_seats = 5;
void drive(){
print('wheels turn');
}
}
class ElectricCar extends Car { // ElectricCar Class takes all property and method inside class car
int batterylevel = 100;
// And we can write more prop. and methods
void recharge(){
batterylevel = 100;
}
}
Comments
Post a Comment