Types3:14 with Jeremy McLain
Every class defines a new type. Objects are just instances of a type.
We can't talk about objects without talking about types. 0:00 You see every object is of a certain type. 0:02 Remember when we use strings, ints, doubles before? 0:06 They're called types because they form the template for 0:10 the values that are of that type. 0:12 In C sharp every value that a variable can take on is an object, so 0:15 we've already used a bunch of objects, five, ten and 0:20 23 are all objects of type int. 0:24 3.14 10.5 100.2 are all objects of type double. 0:28 Hello Bob and We the People are all objects of type string. 0:34 These are all types that are built into the C Sharp language and .net framework. 0:39 We can create our own types though, that's what classes are for. 0:43 A class is the template for making individual objects of a particular type. 0:47 Each individual object that a class makes is called an instance of that class. 0:53 Remember we can use the words type and class interchangeably. 0:58 So we could also say that an object is an instance of a type. 1:02 You can think of classes like a cookie cutter. 1:07 And the cookies are instances of that class. 1:10 This cookie cutter makes heart shaped cookies. 1:14 So all the cookies that this cookie cutter makes are of that type. 1:16 Each of these cookies is a distinct object, 1:21 even though they're all shaped like hearts. 1:25 Also, just because we're using the same cookie cutter, 1:27 doesn't mean all of the cookies have to look the same. 1:31 We can make each heart shaped cookie a little different. 1:34 For example, we could add food coloring to the dough, and 1:37 have pink heart shaped cookies. 1:41 The color is one of the attributes of the cookie. 1:43 Likewise, we can create different objects from a class 1:46 by changing the attributes of that class. 1:50 Let's take a look at a graphical user interface of a piece of software to get 1:53 an idea of how it's composed of classes and objects. 1:56 Take a look at the Chrome browser for example. 1:59 You're probably very familiar with what a web browser looks like. 2:02 But have you ever thought about how it's put together? 2:06 In the code for the Chrome browser there's a class that models a tab. 2:08 This tab that I'm pointing to right here is just one instance of that class. 2:13 I can make many tab instances. 2:17 Each of these tabs is a separate object and 2:20 they're all created from the tab class. 2:23 Each tab has a title attribute that is different for each tab. 2:26 They also have a behavior so 2:31 that when they're clicked, their content is shown in the Window. 2:33 There's a menu class with that many menu objects created from that class. 2:37 There's a button class and many unique objects created from the button class. 2:42 I hope you're starting to get an idea of what object-oriented programming is and 2:48 how it's useful. 2:52 In the next videos,we'll get to see how this is all done in code. 2:54 And hopefully things will become even more clear. 2:57 The concept of objects in code can be a little difficult to understand at first. 3:01 If after working through some code, things still aren't making sense. 3:05 I suggest watching this video again while thinking about what we've done in code. 3:09
You need to sign up for Treehouse in order to download course files.Sign up