6.1 What are Classes & Objects in OOP | Learn Object Oriented Programming in Dart | Dart Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys assalamu alikum Welcome to our DOT programming language course I hope you all are fine our previous tutorial was about basic introduction of objectoriented programming now in today's tutorial we are going to discuss what are the classes and objects let us see what is class in objectoriented programming a class is a blueprint for creating objects a class defines the properties and methods that an object will have for example a class called person might have properties like name age and methods like talk walk run and sleep you can declare a class in dot using the class keyword followed by class name and braces it is good habit to write class name in Pascal case for example person car sh test Etc so we should follow Pascal case to write the name of class the class name should start with capital letter and for example there are two or more words in class name then first letter of each word should be Capital so this is called Pascal case now let me show you practically what is class here uh let me create a class before main function by using uh class keyword and here we can give a name uh for example we want to create a class student student so uh properties of student uh can be for example a name and uh role number and uh age for example age so these three are the properties of student here you can see all the variables are showing error let me hover it non-nullable instance field name must be initialized try adding an initializer expression or a generative Constructor that initializes it or mock it late because we did not assign value to any variable that is why it is showing error so if we use here late keyword it means we will assign its value later same like this we can use late keyword with all our variables late you can see error has been removed there is also another method uh if we don't want to use late keyword there is another way to use here question mark if we use question mark then our variable will become nullable now this uh name variable is nullable it means its value can be null because here you see we did not assign any value to this name that is why it can be nullable so there are two ways to use it is up to you uh to use any way uh but we will use late keyword so this is class student now here inside class we can create different functions for example we want to create a function uh display it will display the data of students uh for example print name will be name dollar sign name uh role number role will be dollar sign role number and age will be age dollar sign age same like traditional programming we can print any statement inside function this is the way same like this we can create multiple classes but if I run my program you will see uh nothing will printed you can see uh there is no output because we only make the class we did not use it in our program as you know our program always start with main function you can see inside main function there is nothing we did not use our class inside main function that is where there is no output in order to use class in our uh program we need to create it object so first of all let us see what is object in objectoriented programming an object is a self-contained unit of code and data objects are created from templates called classes an object is made up of properties variables and methods functions an object is an instance of a class for example bicycle object might have attributes like color size and current speed it might have methods like change gear pedal faser and break you can see it is saying that an object is a self-contained unit of code and data objects are created from templates called classes let me open vs code and show you practically you can see here this is student class we can make its object how let me show you first of all we will use her class name student and here we can create an object of student we can give any name to it for example uh obj object and again initialize it with class student now our object has been created Now by using object dot we can can access everything inside class you can see we can access age name role number and display function now first of all uh let us assign values to variables inside class name let me give a name this is sample name again uh role role number we can give role number again obj do age for example age 23 like this now we can simply display this data by using obj do display you can see here if I run my program here you can see it is printing so this is the way we can create any class and create its object and by using object we can access all the members and functions of that class we can also create another function here to assign values to variables for example set data set data and here we will pass three values uh for name we will choose n for RO number we will choose here R and for age we will choose integer a we will pass three values inside this function and assign these values to our class variables name n role number we will assign R and age we will assign a now we can uh use single line here object dot set data here inside set data we need to pass three parameters uh first of all name and second parameter is role number and age and pass age now it will do same thing here you can see it is printing when we will call set data function it will set values for all three variables inside class and then by using display function we can display all the values we can also create class inside a separate file here you can see this is our file uh 6.1 do dot this is is our file we can also create a separate file in order to store our student class or any class let me show you how uh here if you click file and here we can create a new file because our uh class name is student we will create here uh student dot dot inside same directory where our 6.1 do do file exist click create file it will create a separate file now we can uh cut and paste whole class inside this file here you can see now our student uh class is inside student do. file here you can see but you can see here it is showing error it is showing that student class undefined class undefined class student because inside uh this 6.1 file there is no class student that is why it is showing error so in order to remove this error we need to import this file in order to use it here we can use import student dot dot now you can see error has been removed because now we can access our student do do file if I run it here you can see there is no error it is working properly same like student we can create another class here uh let me create new file for example person person dot dot like this here we can create class person and inside person we can declare variables late string uh name and age int age simply we can uh set values for name and age by using function set data set data or set values here inside we need two parameters one for name and other for age uh name type is string and age type type is integer now here we can set values name is equal to n age is equal to a here we will create a new uh function display in order to display the data of person uh we can use print name dollar sign name comma H dollar sign H like this now here inside our main function we can use our uh person class if we remove it here we can chuse person class but before using it we need to import it import uh person dot dot you can see here here we will create object of person we can uh give a name obj person and by using object do set values you can see we can access set values function here we can set values name and here we need to pass H like this and then simply we can uh display it by using object. display here you can see now if I run it you will see you can see it is printed so by using same method we can create hundreds of files and store hundreds of classes inside files by using this method now you can see our program is uh very simple you can see our classes are inside different uh uh files inside different files you can see our person classes inside person uh file and our student classes inside student file you can can see now we can create another uh class uh for example we have a requirement to create a class area we will create file area dot dot here we will create class area so area will have two properties uh late num uh width width and height like this and here we need to assign values to width and height we will use a function white set values we need two values for width and height width and height W and H here we can use same name right same like width and height let me use width and height now this time you can see names are same same like width and height now how we will uh make difference between variables of CLA and variables of parameters how we can make difference we can difference uh by this keyword this dot if we use this dot width it means uh the width variable of class area this class inside this class this dot width means this width now we can initialize it with width if we use Simple width it means uh width of parameters that we passed in parameters so this mean uh this keyword means the variable of this class this means this class and again uh this dot height this dot height means the height variable of of class area I hope you understand this when we use this keyword it means this class for example we have a requirement to calculate area here we will create a variable calculate area we can give any name calculate area and inside this we can simply print area uh area and here you you know area formula height into width it will simply display area now we can use it inside our main function here let me remove it so first of all we need to import our uh file import area do do file here we need to make object of area area and its name area in small case again can initialize it with area now first of all we will call our uh set values function area do set values here we need to pass width and height for example width is 25 uh 35 and height is uh 56 again we will calculate area by calling area dot calculate area function it will simply display area if I run it you can see it is working fine area is equal to 9060 I hope you understand how to create a class and make it object and how to create a class inside separate files you can see here inside separate files and how to use it inside our uh main file you can see here we can choose any file here by importing it then we will import any file then we can access everything inside that file so you can see here uh these files are inside same location inside same location inside same folder that is why we only use their names if any file is inside another folder or another directory then we need to give their path here so always keep in mind so if you face any issue you can type in comment box or you can contact me directly on WhatsApp okay good luck
Info
Channel: Spread Coding
Views: 22
Rating: undefined out of 5
Keywords: OOP, Classes in OOP, Objects in OOP, Learn OOP in Dart, Learn Dart, Learn Flutter Dart, Dart Programming, Dart Course, OOP in Dart, Object Oriented Programming in Dart, Dart
Id: lG5fc0hpgtQ
Channel Id: undefined
Length: 17min 36sec (1056 seconds)
Published: Mon Mar 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.