0:02 hello welcome to preim Technologies I am
0:06 wer this is part 19 introduction to
0:09 classes in this session we will learn
0:11 what a class is purpose of a class
0:14 Constructor overloading class
0:16 Constructors understanding this keyword and
0:18 and
0:21 destructors so what is a class so far in
0:24 this video tutorial we have seen very
0:26 simple data types like integer float
0:29 double Boolean Etc now if you want to
0:32 create create a complex custom type then
0:34 we can make use of classes what do we
0:37 mean by a complex custom type let's say
0:39 for example if I want to store a number
0:42 I can do that using an integer variable
0:44 but if I want to store my customer
0:47 information then I can make use of
0:49 classes what kind of information does a
0:52 customer has he has first name last name
0:56 date of birth email Etc Now using the
1:00 built-in fields we can create a class
1:02 and also not only a customer class you
1:05 know it not only contains data it can
1:08 also do certain things like save the
1:10 customer to the database print the
1:13 customer full name so essentially a
1:16 class consists of data represented by
1:20 its fields and behavior represented by
1:23 the methods okay so a class has State
1:24 and behavior state is nothing but the
1:28 data behavior is nothing but what is the
1:31 class capable of doing for examp example
1:33 we can save a customer to a database we
1:37 can print his full name Etc let's look
1:39 at an example you know which will make things
1:44 clear okay so how do we create a class
1:47 to create a class we use the class
1:49 keyword followed by the name of the
1:52 class so class
1:55 customer and all of us know customer
1:57 will have first name and last name so
2:00 let's create them string
2:03 first name actually we will use an
2:07 underscore string first name
2:10 name
2:13 String last name so these two Fields
2:16 represent the state the data that this
2:19 class is having okay the first name and
2:23 last name okay now to initialize these
2:26 fields a class can also have a
2:28 Constructor so how does a Constructor
2:31 look like a con instructor will have the
2:34 same name as that of your class okay so
2:37 Public public is nothing but the access
2:39 modifier we will be talking about access
2:42 modifiers in a later session in a great
2:46 detail so access modifier and then the
2:49 name of the class public
2:52 customer and what is the purpose of
2:54 Constructors in life Constructors are
2:58 basically used to initialize your class
3:01 Fields okay we will we'll later
3:03 understand in a bit how we use this
3:06 Constructor okay so for now I'm passing
3:10 two parameters into this Constructor
3:12 string first name
3:14 name
3:16 String last
3:20 name okay so this is the Constructor
3:21 it's having the same name as that of
3:24 your class okay and it can take
3:27 parameters but a Constructor does not
3:29 return a value whereas a method can have
3:31 a return type and if your method doesn't
3:33 written anything you know the return
3:35 type is void otherwise you can have any
3:37 return return type like inlow double
3:40 Boolean Etc but a Constructor you know
3:42 the way you can identify this as a
3:44 Constructor is by looking at its name it
3:46 will have the same name as that of class
3:48 and it will not have a return return
3:51 type but a Constructor can take
3:55 parameters now I am actually using this
3:58 Constructor to initialize these two
4:00 class Fields first name
4:04 and last name so I can simply say
4:10 underscore so this field is equal
4:13 to the parameter that we are passing
4:16 into this Constructor similarly
4:20 underscore last name is equal to the
4:22 parameter that we are passing into this
4:25 Constructor okay now you can refer to
4:28 this this class field you know just by
4:33 its name or you can say this Dot and
4:37 look at this the moment I say this dot
4:40 the intellisense shows both the class
4:42 Fields underscore first name underscore
4:47 last name okay so now this keyword
4:50 actually refers to an instance of this
4:52 class to an object of this
4:55 class okay so you can either say you
4:57 know underscore first name or you know
5:00 for better readability you can use this
5:04 keyword this. last name okay so
5:06 basically we are using this Constructor
5:09 to initialize both of these class Fields
5:12 with these parameters that we are
5:15 passing into this Constructor and this
5:17 Constructor will get called
5:20 automatically when we create an instance
5:22 of this class we'll have a look at that
5:24 in a
5:27 bit okay so so far we have two fields
5:29 and a Constructor to initialize those
5:32 tool Fields so a class has some state
5:34 right now represented by these tool
5:36 Fields now let's say I want to add some
5:38 behavior in the sense the class should
5:40 be capable of doing something for
5:42 example I want this class to be in a
5:45 position to print the full name of our
5:47 customer okay so for that let's have a
5:50 function let's have a method and the
5:53 written type of the method is void
5:55 public void and I'm going to call this
6:00 method print full name so what is this
6:02 method going to do this method is going
6:06 to print the full name of this customer
6:15 line full
6:24 to underscore first
6:27 name have a space in between first name
6:31 and last name and _ last name again if
6:34 you want to refer to this class Fields
6:36 you can just you know type in the fields
6:39 just like this or for better readability
6:42 you can use the this keyword and as we
6:46 know this keyword refers to an instance
6:49 of this class to an in to an to an
6:51 object of this
6:53 class okay so if you look at our class
6:56 now it has got two Fields a Constructor
7:00 and a method apart from that a class can
7:04 also have destructors and destructors
7:06 will have the same name as that of your
7:10 class but they don't take parameters
7:13 they cannot have written type and they
7:17 have a tiled in front of them now
7:20 usually in C you don't require
7:22 destructors know the objects you know
7:25 basically we use destructors to clean up
7:27 the resources to clean up any resources
7:30 your class was holding on on to during
7:34 its lifetime okay so any code to clean
7:37 up those resources goes in this
7:39 distructor and these destructors we
7:41 don't have to call them these
7:44 destructors are called automatically by
7:46 the garbage collector you know when it
7:48 when it kind of tries to clean objects
7:50 from the memory we will be talking about
7:53 distractors and garbage collection in a
7:55 very great detail in a later session for
7:59 now just understand that a Destructor
8:00 will have have the same name as that of
8:03 your class it cannot take parameters and
8:05 it has a tilled symbol in front of it
8:08 and destructors are basically used to
8:10 clean up any resources that your class
8:13 was holding on to during its lifetime
8:15 and these are automatically called by
8:17 the garbage collector when it turns up
8:20 to clean your objects from the
8:25 memory okay so your cleanup code goes
8:28 here and this is your method you know
8:29 which represents the behavior
8:31 this is your Constructor you know
8:33 basically used to initialize your class
8:37 Fields now if I want to use this class I
8:41 can do that in our main method okay so
8:45 we can go ahead and create a an instance
8:49 of this class customer C1 is equal to
8:52 new customer now look at this I'm
8:54 creating C1 is nothing but an instance
8:56 of this class so I'm creating an
9:00 instance of customer class using the new
9:02 keyword the instance is also called as
9:05 object object and instance these terms
9:08 are used interchangeably so I'm creating
9:11 a new customer object now look at this
9:15 the moment I open up this parenthesis it
9:17 shows up our
9:20 Constructor this Constructor which takes
9:23 in first name and last name and let's
9:25 say for example I am passing in the
9:29 first name as Pim and the last name as Tech
9:34 Technologies so what's going to happen
9:38 now this preim will be passed into this
9:40 parameter technologies will be passed
9:42 into this parameter and these parameters
9:47 are then used to initialize these two
9:50 class Fields so understand that you know
9:52 this is the
9:54 Constructor which is getting called
9:56 automatically when we're creating an
9:59 instance of this customer class
10:01 okay so what are Constructors
10:03 Constructors are called automatically
10:05 when you create an instance of your
10:07 class and what is the purpose of this
10:10 Constructor it is basically used to
10:14 initialize your class Fields you know
10:16 this do first name this do last name
10:19 will now be initialized to first name to
10:22 regime and last name to Technologies so
10:24 that you know makes it clear the
10:26 definition of Constructor a Constructor
10:28 is called automatically when you create
10:30 an instance of this CL class and the
10:32 Constructor is basically used to
10:34 initialize those those class
10:37 members okay and then if you want to
10:39 print the customer full name you just
10:42 say C1 print full name and if you look
10:45 at this you know after we create this
10:47 object we are calling the print full
10:50 name so now in this customer object the
10:52 state which is the first name and last
10:54 name fields are initialized to piman
10:57 Technologies and your print full name is
10:59 actually concatenating them together
11:02 together and writing them out to the
11:05 console okay so your Constructor is
11:09 ensuring that this fields are populated
11:11 before we actually use those fields in this
11:12 this
11:15 method okay so that proves that a
11:17 Constructor is used to initialize your
11:19 class fields and it's automatically
11:20 called when you try to create an
11:23 instance with your class okay so if we
11:25 go ahead and run this program as you
11:28 might expect full name is equal to preim
11:31 Tech Technologies
11:35 now is it mandatory that we have to
11:37 provide a Constructor every time we
11:40 create a class not necessarily for
11:43 example let's comment this you know
11:45 Constructor and you can do that by
11:47 clicking that button so once I comment
11:50 that obviously you know since we don't
11:52 have that Constructor you know it gives
11:55 an error there is no you know if you
11:57 look at the error customer does not
11:59 contain a Constructor that takes two
12:01 arguments there is no Constructor which
12:05 takes two arguments so now we can't do
12:08 that but what I can do is look at this
12:10 when I say new
12:12 customer automatically I'm getting a
12:14 Constructor without any parameters if
12:16 you look at this customer open
12:18 parenthesis close parenthesis it doesn't
12:20 take any parameters so what does that
12:24 mean if you do not write a Constructor
12:26 if you do not provide a Constructor for
12:29 your class NET Framework will provide
12:33 one for you for free okay and it is a
12:35 default parameterless Constructor okay
12:38 there are no parameters the default
12:41 Constructor provided by NET Framework is
12:43 a parameterless default
12:46 Constructor okay and this default
12:48 Constructor what it does is basically
12:50 walks through all the fields in the
12:51 class and initialize them to their
12:54 defaults now look at this when I call
12:57 this default customer this first name
12:58 and last name they are not initialized
13:02 to anything okay they're basically empty
13:04 so now if I go ahead and run this
13:05 application look at what's going to
13:09 happen full name is equal to empty and
13:12 now somebody is able to use you know
13:14 call this method in spite of those
13:17 fields not being initialized that's why
13:19 you have that full name is equal to
13:22 empty okay so this default parameter
13:25 less Constructor is not really that
13:27 useful and if you want to make that a
13:31 little more useful what you can do is
13:33 basically you can you can provide you
13:35 know let's say I have this Constructor
13:37 but another thing to keep in mind look
13:39 at this the moment I provide a
13:41 Constructor you know I have provided a
13:43 Constructor for my own class and the
13:45 moment I have done that look at to look
13:47 at what happens to the default parameter
13:49 L Constructor now customer does not
13:51 contain a Constructor that takes zero
13:54 argument so what's happening here is
13:57 that once we provide our own Constructor
13:59 to this class you know net takes away
14:01 that default parameter less Constructor
14:03 which it has provided when your class
14:05 doesn't have any
14:08 Constructor okay so so it's taking away
14:11 that Constructor that's why it says the
14:13 AA message customer does not contain a
14:16 Constructor that takes zero
14:19 arguments okay so now let's say we want
14:21 to provide that capability you know
14:22 somebody should be able to create a
14:25 customer object without passing in any
14:27 parameters to the Constructor so how do
14:30 you do that okay you can actually write
14:33 a Constructor which does not take any
14:37 arguments something like this public
14:39 public
14:42 customer and then what you can basically
14:47 do is when you call this customer I mean
14:51 when somebody calls that Constructor I want
15:01 basically I want basically the
15:04 Constructor which takes two parameters
15:07 to be called but then I'm going to
15:10 initialize them to their defaults like
15:14 you know um
15:17 no first name
15:18 name
15:22 provided and similarly for the last name
15:40 basically okay if somebody calls this
15:42 default you know if somebody wants to
15:44 create a customer object without passing
15:46 in parameters that's fine they can do
15:49 that like this okay customer C1 is equal
15:52 to new customer now when this if you
15:54 right click on that and when you say go
15:56 to definition it comes to this
15:57 Constructor but if you look at this
16:00 Constructor it's called calling this
16:03 customer this Constructor this refers to
16:06 this instance of the class so this here
16:09 refers to this particular Constructor
16:11 and passing in no first name provided
16:13 for first name and no last name provided
16:16 for last name properties and these are
16:19 then used to initialize these class
16:21 Fields so obviously now if somebody is
16:24 creating a customer object like this and
16:26 if you go ahead and run that look at
16:27 that no first name provided is their
16:30 first name no no last name provided is
16:31 their last
16:34 name okay so if you want to make that
16:36 default parameter L Constructor a little
16:38 more useful and if you want to make sure
16:40 that your class fields are initialized
16:42 to something before you know somebody
16:44 calls methods and do something with that
16:47 class then you can provide an
16:49 implementation for that parameterless
16:52 Constructor and invoke any of the you
16:54 know parameter constru any of the
16:56 constructors with parameters to
16:58 initialize your class Fields the way you
17:01 want then okay but on the other hand you
17:04 know if somebody else wants to create a
17:07 customer another customer object they
17:09 can do customer C2 is equal to new
17:12 customer and now look at this there are
17:15 two Constructors now one Constructor
17:17 which does not if you look at this one
17:20 of two Constructors one Constructor if
17:22 you look on the right side no parameters
17:24 okay parameter less Constructor there
17:26 there is no need to pass parameters I
17:29 can do that or
17:31 there is another Constructor which takes
17:33 first name and last name so there are
17:36 two Constructors with the same name you
17:38 know differing by just the number of
17:41 parameters so this is called as
17:44 overloading Constructors so this class
17:46 has two Constructors one which does not
17:49 take parameters at all another one which
17:52 takes two parameters okay basically we
17:55 can overload a Constructor you know on
18:00 the number and type of parameters
18:03 okay say for example p is my first name
18:06 and maybe T is my last
18:11 name and if I say C2 do printful name so
18:16 for the second customer it will say PT p
18:28 slides so purpose of a class Constructor
18:29 the purpose of a CL class Constructor is
18:31 to initialize your class Fields a class
18:34 Constructor is automatically called when
18:36 an instance of a class is created
18:38 Constructors do not have written values
18:40 and always have the same name as that of
18:42 your class yeah that's that's one
18:43 important difference to keep between a
18:47 Constructor and a class methods I'm
18:48 sorry Constructor and methods methods
18:50 will have written type whereas your
18:52 Constructor doesn't have your doesn't
18:53 have any written type and they have the
18:55 same name as that of your class
18:58 Constructors are not mandatory if we do
19:00 not provide Constructor a default
19:02 parameter less Constructor is
19:04 automatically provided by NET Framework
19:07 but once we provide an implementation
19:09 you know of a Constructor the default
19:11 parameter less Constructor Constructor
19:14 provided by NET Framework is taken away
19:16 and Constructors can be overloaded by
19:18 the number and type of parameters and we
19:21 have seen an example of that as
19:24 well so what are destructors destructors
19:26 have the same name as that of your class
19:28 with a till symbol in front of them they
19:30 don't don't take any parameters and they
19:32 don't have written values as well
19:35 destructors are places where you put
19:37 code to release any resources your class
19:40 was holding on to during its lifetime we
19:43 will actually talk about destructors and
19:45 the process of garbage collection in a later
19:47 later
19:50 session that's it for today thank you