WEBVTT 1 00:00:05.713 --> 00:00:09.913 Game Basics Class 2 00:00:09.913 --> 00:00:12.091 GCC Academy 3 00:00:28.251 --> 00:00:29.831 Hello everyone 4 00:00:30.213 --> 00:00:35.033 I am Lee Yeong-hoon, in charge of this C#-based Object-Oriented Programming Lecture 5 00:00:35.033 --> 00:00:38.251 This unit covers object-oriented programming 6 00:00:38.251 --> 00:00:42.552 and the core topic of classes 7 00:00:42.811 --> 00:00:46.991 We will explore the concept of classes and objects, 8 00:00:46.992 --> 00:00:52.048 learning how to initialize them using constructors 9 00:00:52.048 --> 00:00:55.050 Additionally, we’ll examine the principles of object-oriented programming, 10 00:00:55.051 --> 00:01:00.411 abstraction, inheritance, encapsulation, and polymorphism, 11 00:01:00.411 --> 00:01:03.023 and learn how to implement these through coding 12 00:01:03.481 --> 00:01:07.582 Understanding Object-Oriented Programming and Classes 13 00:01:07.782 --> 00:01:12.183 A class, 14 00:01:12.603 --> 00:01:16.163 also referred to as a user-defined data type, 15 00:01:23.964 --> 00:01:25.325 in a more detailed way, 16 00:01:25.785 --> 00:01:29.735 represents a space containing attributes and functions 17 00:01:31.058 --> 00:01:34.928 It contains things like 18 00:01:34.929 --> 00:01:38.248 attributes and functions 19 00:01:38.483 --> 00:01:41.583 Last time, 20 00:01:41.962 --> 00:01:44.582 we learned about variables and functions 21 00:01:44.892 --> 00:01:47.652 Attributes function like variables, 22 00:01:50.212 --> 00:01:51.952 while functions represent actions or methods 23 00:01:54.091 --> 00:01:59.861 And function literally means a function 24 00:02:01.719 --> 00:02:06.400 So we say a class functions as a function 25 00:02:06.400 --> 00:02:10.780 or a method 26 00:02:10.780 --> 00:02:11.844 That's another way of saying it 27 00:02:12.299 --> 00:02:14.979 In there, we have attributes or functions 28 00:02:14.979 --> 00:02:18.817 that are also called fields 29 00:02:18.817 --> 00:02:22.457 In C#, attributes inside a class are 30 00:02:22.457 --> 00:02:24.444 specifically referred to as fields 31 00:02:25.140 --> 00:02:27.061 In other words, 32 00:02:27.061 --> 00:02:30.394 we can say a member variable or a member function 33 00:02:30.844 --> 00:02:36.584 In Unity development using C#, 34 00:02:37.759 --> 00:02:40.979 the naming conventions follow specific rules 35 00:02:41.415 --> 00:02:46.420 Variables are often nouns, 36 00:02:46.420 --> 00:02:50.840 and functions, verbs 37 00:02:51.240 --> 00:02:54.600 For instance, a Human class 38 00:02:54.601 --> 00:02:57.261 could have fields like age, 39 00:02:59.091 --> 00:02:59.671 name, 40 00:03:00.919 --> 00:03:03.299 and gender, 41 00:03:03.300 --> 00:03:06.348 and maybe height too 42 00:03:06.690 --> 00:03:14.641 A Human class can have attributes such as these 43 00:03:15.058 --> 00:03:18.098 It can also include functionalities 44 00:03:20.169 --> 00:03:21.469 like walking 45 00:03:22.258 --> 00:03:23.688 and sitting 46 00:03:24.349 --> 00:03:28.189 This is essentially how a class is structured 47 00:03:28.405 --> 00:03:31.845 Within a class, you can include constructs 48 00:03:31.846 --> 00:03:39.639 like if statements like so 49 00:03:39.639 --> 00:03:43.396 or for loops to define the logic of its methods 50 00:03:43.510 --> 00:03:46.570 That's how classes are composed of 51 00:03:46.871 --> 00:03:50.411 Classes are described as user-defined data types 52 00:03:51.994 --> 00:03:55.954 With these user-defined data types, you can create variables, 53 00:03:56.103 --> 00:03:56.663 or objects 54 00:03:56.823 --> 00:03:58.923 Variables created using a class are referred 55 00:03:59.883 --> 00:04:04.492 to as objects in programming 56 00:04:04.572 --> 00:04:09.752 Objects, 57 00:04:10.167 --> 00:04:12.827 in English 58 00:04:16.904 --> 00:04:18.144 An object 59 00:04:18.145 --> 00:04:24.733 Sometimes, it is also referred to as an instance 60 00:04:25.024 --> 00:04:28.924 While object and instance are related, 61 00:04:29.023 --> 00:04:31.323 not exactly the same, 62 00:04:31.582 --> 00:04:36.002 as in they refer to a variable that exists 63 00:04:36.023 --> 00:04:39.783 But they carry slightly different meanings 64 00:04:40.092 --> 00:04:44.873 An instance refers to a specific existence, 65 00:04:44.873 --> 00:04:48.033 whereas an object refers to a role or functionality within the system 66 00:04:48.399 --> 00:04:52.159 For example, a Human class 67 00:04:52.160 --> 00:04:59.480 can be used to create specific objects 68 00:05:00.009 --> 00:05:02.309 like Yeonghee or Cheolsu 69 00:05:02.611 --> 00:05:03.171 Right 70 00:05:03.897 --> 00:05:06.007 Let’s say we create an object called Cheolsu 71 00:05:06.711 --> 00:05:11.071 Cheolsu is an instance of the Human class 72 00:05:11.072 --> 00:05:14.346 Inheritance is a concept within a class 73 00:05:14.579 --> 00:05:20.079 that allows it to transform into a different class 74 00:05:20.657 --> 00:05:23.236 This transformation can change the roles associated with the class, 75 00:05:23.237 --> 00:05:25.935 which is the essence of an object 76 00:05:25.935 --> 00:05:30.515 For example, Cheolsu is his parents’ son 77 00:05:30.857 --> 00:05:35.717 At school, he is a friend to his classmates, 78 00:05:35.718 --> 00:05:37.074 so his role is a friend, 79 00:05:37.579 --> 00:05:42.359 and in his relationship with his teacher, he is a student 80 00:05:42.537 --> 00:05:44.597 If Cheolsu has a job, 81 00:05:44.598 --> 00:05:47.518 such as being a department manager in a company, 82 00:05:47.679 --> 00:05:51.879 he assumes the role of a manager in that context 83 00:05:51.937 --> 00:05:53.958 Although Cheolsu is the same entity, 84 00:05:53.958 --> 00:05:57.178 he performs different roles based on the situation, 85 00:05:57.178 --> 00:06:00.657 which illustrates the concept of objects as adaptable entities 86 00:06:00.657 --> 00:06:02.817 Instances, in contrast, refer to the actual existence 87 00:06:02.818 --> 00:06:06.238 of something in memory 88 00:06:06.547 --> 00:06:09.187 In this context, it is referred to as an instance 89 00:06:11.147 --> 00:06:15.307 For class, 90 00:06:15.451 --> 00:06:20.172 a class also has special concepts, 91 00:06:20.172 --> 00:06:22.872 which includes 92 00:06:23.327 --> 00:06:30.327 things such as inside and outside the class 93 00:06:30.327 --> 00:06:34.246 For instance, a class can contain another class 94 00:06:38.286 --> 00:06:38.836 Right 95 00:06:39.135 --> 00:06:44.035 This creates the distinction between the inner part and the outer part of the class 96 00:06:44.287 --> 00:06:47.127 This distinction involves concepts 97 00:06:47.128 --> 00:06:50.987 like granting access permissions to the information 98 00:06:50.987 --> 00:06:52.143 inside the class from other classes outside it 99 00:06:52.143 --> 00:06:57.863 This is called access modifiers or access specifiers 100 00:06:57.959 --> 00:06:59.899 I’ll use the term access modifier 101 00:07:03.217 --> 00:07:09.277 For example, a class may have attributes like age, name, and gender 102 00:07:09.278 --> 00:07:14.675 Let’s say you want gender to be accessible internally 103 00:07:14.935 --> 00:07:18.657 but hidden externally 104 00:07:19.297 --> 00:07:19.879 Right 105 00:07:20.051 --> 00:07:23.371 In such cases, you can make it public, 106 00:07:23.951 --> 00:07:25.731 private, 107 00:07:27.173 --> 00:07:29.357 or conditionally accessible 108 00:07:29.595 --> 00:07:32.755 within inheritance relationships, 109 00:07:32.855 --> 00:07:34.137 but hidden externally 110 00:07:34.137 --> 00:07:38.656 Visible or invisible to 111 00:07:38.657 --> 00:07:40.197 the parent-child relationships 112 00:07:40.198 --> 00:07:44.403 Public means fully accessible, 113 00:07:44.403 --> 00:07:48.437 private means completely hidden, 114 00:07:48.437 --> 00:07:51.576 and protected means accessible 115 00:07:51.577 --> 00:07:57.079 within inheritance relationships but hidden elsewhere 116 00:07:57.603 --> 00:08:00.343 These keywords 117 00:08:00.344 --> 00:08:06.864 allow you to control access to attributes 118 00:08:06.864 --> 00:08:10.843 or functions using access modifiers 119 00:08:11.163 --> 00:08:12.303 Like this 120 00:08:12.566 --> 00:08:14.066 Additionally, 121 00:08:14.306 --> 00:08:18.686 When we learned about functions, 122 00:08:18.686 --> 00:08:21.523 we discussed internal and external concepts 123 00:08:21.523 --> 00:08:25.711 And when there’s a variable, accessing it is also part of this 124 00:08:25.962 --> 00:08:27.763 Remember? 125 00:08:28.326 --> 00:08:31.126 A variable outside the function 126 00:08:31.126 --> 00:08:33.593 is typically called a field 127 00:08:33.803 --> 00:08:35.343 For instance, 128 00:08:38.963 --> 00:08:40.683 let’s say there’s a variable called money 129 00:08:41.234 --> 00:08:43.134 A variable called money 130 00:08:44.014 --> 00:08:48.094 This variable money can also exist inside the walk function, 131 00:08:49.842 --> 00:08:52.283 and the sit function can also have its own money variable 132 00:08:53.610 --> 00:08:56.970 When accessing these, 133 00:08:57.240 --> 00:09:01.500 the money inside the walk function is only accessible within walk 134 00:09:01.843 --> 00:09:08.403 This is called a local variable 135 00:09:08.906 --> 00:09:11.247 This is a member variable 136 00:09:11.247 --> 00:09:12.639 or a field 137 00:09:12.759 --> 00:09:14.439 I'll stick to field 138 00:09:17.758 --> 00:09:19.858 So this is a local variable too 139 00:09:24.284 --> 00:09:28.984 So, if sit refers to money, it refers to its own money variable 140 00:09:28.984 --> 00:09:32.990 Similarly, walk’s money refers to its own money 141 00:09:33.545 --> 00:09:34.195 Right 142 00:09:34.196 --> 00:09:38.356 Therefore, internal variables 143 00:09:38.357 --> 00:09:41.777 are prioritized when accessed 144 00:09:41.898 --> 00:09:44.878 But sometimes, you may want to access the external field instead 145 00:09:45.020 --> 00:09:50.030 To do this, you use 146 00:09:50.031 --> 00:09:52.094 something before this name, money 147 00:09:52.239 --> 00:09:58.438 for a field, it's this.money 148 00:09:58.439 --> 00:09:59.876 This means 149 00:10:00.132 --> 00:10:02.592 that the field is outside 150 00:10:02.592 --> 00:10:07.874 By clearly indicating whose property it is, 151 00:10:07.875 --> 00:10:10.746 access becomes possible 152 00:10:11.146 --> 00:10:14.411 Using these characteristics of classes, 153 00:10:14.411 --> 00:10:18.790 we can perform object-oriented programming 154 00:10:18.930 --> 00:10:20.036 Next 155 00:10:23.686 --> 00:10:28.626 is object-oriented programming, 156 00:10:31.576 --> 00:10:34.346 which is object, 157 00:10:36.096 --> 00:10:38.066 and oriented, 158 00:10:40.614 --> 00:10:42.584 and now programming 159 00:10:48.934 --> 00:10:52.814 It involves creating everything as objects 160 00:10:52.815 --> 00:10:57.367 and programming through their interactions 161 00:10:57.367 --> 00:11:01.112 That's the object oriented programming, 162 00:11:01.404 --> 00:11:07.084 or OOP when abbreviated 163 00:11:07.720 --> 00:11:11.220 OOP has four main features, 164 00:11:11.220 --> 00:11:14.460 inheritance, 165 00:11:14.460 --> 00:11:16.900 the ability to inherit, 166 00:11:16.900 --> 00:11:23.984 abstraction, 167 00:11:23.984 --> 00:11:28.734 encapsulation, 168 00:11:28.784 --> 00:11:35.474 or hiding, 169 00:11:35.474 --> 00:11:40.144 and polymorphism 170 00:11:40.886 --> 00:11:42.866 Let me explicate 171 00:11:43.165 --> 00:11:47.465 Inheritance allows a parent-child relationship between classes 172 00:11:47.466 --> 00:11:49.766 For example, 173 00:11:49.766 --> 00:11:53.966 let's say we have class A and B 174 00:11:54.713 --> 00:12:06.053 Class A with a name variable and a walk function can be inherited by class B, 175 00:12:06.053 --> 00:12:13.493 which then gains A’s features and can add its own 176 00:12:13.493 --> 00:12:21.533 When class B inherits class A, this is what it looks like 177 00:12:21.573 --> 00:12:25.332 This is class B, 178 00:12:26.373 --> 00:12:38.773 inheriting class A, like this Of course, the child B is bigger than its parent A 179 00:12:38.773 --> 00:12:45.173 So we can say name, walk, and everything 180 00:12:45.173 --> 00:12:54.613 We can also add member variables and functions in class B here 181 00:12:54.613 --> 00:13:04.493 So again, inheritance allows a parent-child relationship between classes 182 00:13:04.493 --> 00:13:14.732 Now, abstraction is something we're familiar with unclear things 183 00:13:14.732 --> 00:13:24.412 Abstraction simplifies complex structures by only implementing necessary features 184 00:13:24.412 --> 00:13:30.453 For instance, let's create a human class 185 00:13:30.453 --> 00:13:36.692 For instance, a human class might include a lot of attributes 186 00:13:36.692 --> 00:13:49.893 When programming, creating a human involves reducing and simplifying what a human can do 187 00:13:49.893 --> 00:14:00.173 or what information it holds, for example, a human class might only need the ability to walk 188 00:14:00.173 --> 00:14:06.773 and speak to serve its purpose 189 00:14:06.773 --> 00:14:14.973 Instead of implementing everything a human can do, 190 00:14:14.973 --> 00:14:22.092 abstraction involves designing and implementing only the essential features required for the task 191 00:14:22.173 --> 00:14:31.453 Encapsulation, as the term suggests, means hiding information 192 00:14:38.533 --> 00:14:45.732 Earlier I mentioned access modifiers, including public, 193 00:14:46.292 --> 00:14:49.092 private, 194 00:14:49.453 --> 00:14:52.453 and protected 195 00:14:53.732 --> 00:15:03.033 These are the three concepts 196 00:15:04.372 --> 00:15:05.940 Public, private, and protected 197 00:15:05.940 --> 00:15:07.064 Partial access 198 00:15:07.065 --> 00:15:11.700 means making something public in a parent-child relationship 199 00:15:11.700 --> 00:15:14.291 but private to external classes 200 00:15:14.291 --> 00:15:17.629 In this case, we use private or protected 201 00:15:18.646 --> 00:15:22.480 protected is used to limit access 202 00:15:22.733 --> 00:15:27.114 to specific relationships 203 00:15:27.114 --> 00:15:29.268 while keeping it hidden from others, 204 00:15:29.269 --> 00:15:31.849 for instance the name 205 00:15:31.851 --> 00:15:35.245 So external classes 206 00:15:35.246 --> 00:15:39.043 cannot have access to this member, or this class 207 00:15:39.044 --> 00:15:42.059 Preventing access to certain properties 208 00:15:42.059 --> 00:15:43.585 or functionalities is called encapsulation 209 00:15:43.729 --> 00:15:46.211 Blocking access entirely is not always ideal 210 00:15:46.211 --> 00:15:51.239 Encapsulation is often used instead 211 00:15:51.413 --> 00:15:53.421 Imagine receiving a capsule pill 212 00:15:53.719 --> 00:15:56.337 from a pharmacy 213 00:15:58.040 --> 00:15:59.847 labeled as a cold medicine 214 00:16:00.076 --> 00:16:02.342 It's a capsule, 215 00:16:02.786 --> 00:16:05.276 a pill that looks like this 216 00:16:05.693 --> 00:16:08.637 Let's say this is our cold pill 217 00:16:11.533 --> 00:16:15.158 When you take the pill, 218 00:16:15.159 --> 00:16:20.177 you expect it to cure your cold, 219 00:16:20.177 --> 00:16:23.813 but you don’t necessarily know 220 00:16:24.234 --> 00:16:27.371 or care about the exact ingredients inside 221 00:16:27.371 --> 00:16:28.642 You focus on 222 00:16:29.324 --> 00:16:33.780 its functionality rather than its internal composition 223 00:16:33.780 --> 00:16:36.289 Similarly, encapsulation 224 00:16:36.402 --> 00:16:40.067 in a class means using its functions 225 00:16:40.068 --> 00:16:43.534 without needing to understand 226 00:16:43.798 --> 00:16:46.829 the detailed implementation inside, 227 00:16:46.894 --> 00:16:50.082 trusting that it’s properly set up 228 00:16:50.338 --> 00:16:52.446 That's encapsulation 229 00:16:53.121 --> 00:16:56.354 To enable encapsulation, access specifiers are used 230 00:16:57.218 --> 00:17:00.116 Lastly, there’s polymorphism, 231 00:17:00.681 --> 00:17:03.427 so polymorphism in English 232 00:17:03.744 --> 00:17:05.416 It comes from polymorph 233 00:17:05.675 --> 00:17:08.802 For those familiar with fantasy novels, 234 00:17:08.853 --> 00:17:14.046 you might recognize the term polymorph 235 00:17:14.046 --> 00:17:15.700 use to transform into human shapes 236 00:17:16.040 --> 00:17:19.794 and secretly live among people 237 00:17:19.795 --> 00:17:21.890 In that case, 238 00:17:21.891 --> 00:17:27.072 polymorph is the magic spell to change that body 239 00:17:27.221 --> 00:17:28.786 In programming, 240 00:17:28.787 --> 00:17:31.924 polymorphism refers to having multiple forms 241 00:17:32.309 --> 00:17:35.672 or appearances in general 242 00:17:35.851 --> 00:17:40.193 When a class is inherited, 243 00:17:40.264 --> 00:17:41.905 for that inheritance, 244 00:17:41.906 --> 00:17:44.371 an object, 245 00:17:45.773 --> 00:17:47.773 or an instance, 246 00:17:49.037 --> 00:17:51.812 looks like this 247 00:17:52.196 --> 00:17:55.278 For instance, if I create 248 00:17:55.279 --> 00:17:59.967 an instance using class B, 249 00:17:59.967 --> 00:18:01.878 like this, 250 00:18:02.050 --> 00:18:03.872 it follows 251 00:18:03.985 --> 00:18:10.087 the structure of class B if I want it to 252 00:18:10.990 --> 00:18:14.007 However, I can also cast this instance into the form of class A 253 00:18:14.258 --> 00:18:17.510 his transformation is called casting 254 00:18:17.743 --> 00:18:20.744 Type conversion, 255 00:18:20.744 --> 00:18:22.791 also called type casting, 256 00:18:22.791 --> 00:18:26.270 is possible 257 00:18:27.597 --> 00:18:29.535 but only so under the condition of 258 00:18:29.536 --> 00:18:32.767 when classes are in an inheritance relationship 259 00:18:32.767 --> 00:18:33.864 For classes, 260 00:18:34.326 --> 00:18:36.605 the unrelated classes 261 00:18:36.606 --> 00:18:39.253 without an inheritance structure, 262 00:18:39.383 --> 00:18:41.014 type conversion is not allowed 263 00:18:41.451 --> 00:18:45.335 So it can only happen when it has inherited it 264 00:18:45.815 --> 00:18:48.102 When type casting is performed, 265 00:18:48.334 --> 00:18:50.460 with the conversion, 266 00:18:50.757 --> 00:18:54.392 the actual memory size of the object remains unchanged, 267 00:18:54.393 --> 00:19:01.644 but access to members is restricted by the converted type 268 00:19:01.644 --> 00:19:04.391 For example, if an instance created as type B 269 00:19:04.392 --> 00:19:07.039 is forcibly cast to type A, 270 00:19:07.309 --> 00:19:09.838 you can only access members of type A 271 00:19:10.517 --> 00:19:13.654 This allows the object’s form to change like so 272 00:19:14.736 --> 00:19:16.223 Although this example only involves two types, 273 00:19:16.224 --> 00:19:18.368 you can continue inheriting and transforming forms 274 00:19:19.718 --> 00:19:24.511 This ability to change forms through type casting 275 00:19:24.512 --> 00:19:26.833 is referred to 276 00:19:26.833 --> 00:19:29.200 as polymorphism 277 00:19:29.678 --> 00:19:30.548 An object, 278 00:19:36.050 --> 00:19:37.190 through type casting, 279 00:19:37.190 --> 00:19:38.594 can take on different appearances, 280 00:19:41.372 --> 00:19:44.015 which is what polymorphism signifies 281 00:19:44.016 --> 00:19:47.607 So that's what polymorphism does 282 00:19:48.951 --> 00:19:53.181 Constructors, Inheritance, Encapsulation, and Polymorphism 283 00:19:54.672 --> 00:19:58.891 Now, let’s create a project 284 00:19:58.892 --> 00:20:00.382 to practice coding 285 00:20:00.952 --> 00:20:03.790 Right-click on the solution, choose Add, 286 00:20:03.791 --> 00:20:07.087 and then New Project 287 00:20:07.392 --> 00:20:11.168 Select ‘Console App .NET Framework, 288 00:20:11.521 --> 00:20:12.951 click Next, 289 00:20:13.292 --> 00:20:16.146 and name it Class 290 00:20:20.703 --> 00:20:23.081 Like so, Class 291 00:20:27.977 --> 00:20:29.331 Now, Create 292 00:20:30.163 --> 00:20:34.504 n the initial setup, you’ll see the default structure 293 00:20:34.505 --> 00:20:39.735 includes a class Program declaration 294 00:20:41.943 --> 00:20:44.192 It may also include the internal keyword, 295 00:20:44.661 --> 00:20:47.217 which might seem complex, 296 00:20:47.218 --> 00:20:48.426 but simply put, 297 00:20:49.679 --> 00:20:54.503 it restricts access to within the same program 298 00:20:54.503 --> 00:20:56.412 It’s somewhat similar to protected 299 00:20:57.159 --> 00:20:59.409 When developing an application, 300 00:20:59.410 --> 00:21:01.833 other applications could potentially 301 00:21:02.343 --> 00:21:06.584 access certain functions or members 302 00:21:06.585 --> 00:21:08.677 by traversing memory 303 00:21:08.813 --> 00:21:11.068 The internal keyword 304 00:21:11.262 --> 00:21:13.566 ensures that only components 305 00:21:13.567 --> 00:21:17.361 within the same application can access those members, 306 00:21:17.361 --> 00:21:20.301 or within the same binary, 307 00:21:20.301 --> 00:21:25.552 preventing external access 308 00:21:25.553 --> 00:21:29.926 The internal keyword is used to handle access 309 00:21:29.926 --> 00:21:31.639 protection in this way 310 00:21:31.639 --> 00:21:37.804 It’s a concept I believe is specific to C# 311 00:21:37.863 --> 00:21:39.886 So now, 312 00:21:39.887 --> 00:21:41.070 while many might think 313 00:21:41.532 --> 00:21:45.451 C# only has public, private, and protected, 314 00:21:45.452 --> 00:21:46.566 there’s one more 315 00:21:46.743 --> 00:21:47.990 in C# 316 00:21:48.091 --> 00:21:50.519 So this is, 317 00:21:50.519 --> 00:21:51.990 it's like protected, 318 00:21:51.991 --> 00:21:54.825 or even public in a way 319 00:21:57.441 --> 00:22:01.605 Now, let’s create a class 320 00:22:02.117 --> 00:22:04.243 Outside the main program, 321 00:22:04.608 --> 00:22:07.019 we’ll add a new class declaration 322 00:22:08.052 --> 00:22:12.475 ll call it Animal. Start with class Animal 323 00:22:14.569 --> 00:22:18.613 and add curly braces to define its structure. 324 00:22:20.529 --> 00:22:21.792 Now, 325 00:22:22.541 --> 00:22:24.278 this creates the class, 326 00:22:24.278 --> 00:22:26.304 but if you want it 327 00:22:26.305 --> 00:22:28.386 to be used anywhere publicly, 328 00:22:29.129 --> 00:22:32.399 such as creating an object like Class.Animal 329 00:22:32.892 --> 00:22:37.969 In making an object like this, I recommend 330 00:22:38.414 --> 00:22:40.391 that you make it public 331 00:22:40.391 --> 00:22:43.220 Add the public keyword 332 00:22:43.221 --> 00:22:45.852 to allow access from anywhere 333 00:22:46.289 --> 00:22:48.165 So it looks like this 334 00:22:50.489 --> 00:22:54.353 Now, let’s add 335 00:22:54.886 --> 00:22:55.979 some members to this Animal class 336 00:22:56.433 --> 00:22:58.550 First, we’ll define a member variable 337 00:22:59.583 --> 00:23:02.206 and a function 338 00:23:02.716 --> 00:23:05.802 I'll make these two now 339 00:23:05.802 --> 00:23:07.684 Let’s create 340 00:23:08.048 --> 00:23:11.294 a public member variable called name 341 00:23:11.295 --> 00:23:14.043 Public 342 00:23:14.624 --> 00:23:18.343 We’ll write it as public string name 343 00:23:22.986 --> 00:23:26.441 Then, we’ll create an object from this class 344 00:23:26.442 --> 00:23:29.581 In the Main method, we’ll declare an object of the Animal class 345 00:23:30.786 --> 00:23:33.471 Since a class acts as a data type, 346 00:23:33.472 --> 00:23:34.522 a data type, 347 00:23:34.522 --> 00:23:37.935 we write the class name followed by the variable name, 348 00:23:38.106 --> 00:23:41.714 such as Animal animal 349 00:23:42.026 --> 00:23:45.396 So notice that this class 350 00:23:45.397 --> 00:23:46.733 is what we just created 351 00:23:46.970 --> 00:23:52.123 So, a class variable created with a user-defined data type 352 00:23:52.356 --> 00:23:54.201 is called an object 353 00:23:54.346 --> 00:23:58.396 In C#, you can’t just create an object like this 354 00:23:58.711 --> 00:24:00.161 So how do you create one? 355 00:24:00.162 --> 00:24:02.857 You leave a space, use the = operator, 356 00:24:02.861 --> 00:24:07.305 and then use the new keyword to create it 357 00:24:07.506 --> 00:24:08.878 Then, you leave a space, 358 00:24:08.879 --> 00:24:10.934 write the name of the class, 359 00:24:12.706 --> 00:24:16.020 and finally add parentheses at the end 360 00:24:20.425 --> 00:24:23.452 By doing this, the class is created 361 00:24:23.453 --> 00:24:27.153 Now, I’ll try printing the name inside this class 362 00:24:28.374 --> 00:24:31.390 I’ll write it like this and use 363 00:24:35.105 --> 00:24:36.199 Console.WriteLine 364 00:24:36.200 --> 00:24:42.116 to output the name of the animal 365 00:24:42.385 --> 00:24:46.678 However, since the name currently has no value, 366 00:24:46.803 --> 00:24:49.543 it will simply print an empty space 367 00:24:51.186 --> 00:24:52.467 Ah, I forgot to set it 368 00:24:52.468 --> 00:24:55.709 After selecting the class 369 00:24:55.709 --> 00:24:59.186 and setting it as the startup project, I’ll run it 370 00:24:59.186 --> 00:25:03.021 When executed, nothing will be displayed, 371 00:25:03.021 --> 00:25:06.586 just a blank line, as you can see 372 00:25:06.590 --> 00:25:08.251 Now, I want to assign a name 373 00:25:08.606 --> 00:25:09.403 Look here 374 00:25:09.403 --> 00:25:13.220 I’ll assign ‘animal’ to the name of animal, 375 00:25:15.186 --> 00:25:17.406 like animal 376 00:25:19.946 --> 00:25:24.480 So this prints animal correctly 377 00:25:25.546 --> 00:25:29.583 But creating an object like this 378 00:25:29.584 --> 00:25:35.126 and manually assigning a value every time feels inconvenient 379 00:25:35.129 --> 00:25:39.062 So, I want the name field to automatically have animal 380 00:25:39.274 --> 00:25:43.966 when an animal object is created 381 00:25:43.966 --> 00:25:46.605 For this, we use a special function 382 00:25:46.605 --> 00:25:50.725 called a constructor 383 00:25:50.746 --> 00:25:52.697 A constructor function 384 00:25:52.698 --> 00:25:55.975 is a function automatically called 385 00:25:56.435 --> 00:26:00.591 when an object is created from a class 386 00:26:00.625 --> 00:26:04.665 When an object is instantiated from a class, 387 00:26:04.665 --> 00:26:07.031 the constructor is automatically triggered 388 00:26:07.032 --> 00:26:08.164 That's how you can think of it 389 00:26:08.468 --> 00:26:09.905 So, here, 390 00:26:09.905 --> 00:26:12.079 there's a hint in this line 391 00:26:12.400 --> 00:26:17.152 When you initialize an Animal object, 392 00:26:17.322 --> 00:26:21.031 Initialization simply means 393 00:26:21.185 --> 00:26:26.211 assigning a value to a variable at the moment it is created 394 00:26:26.705 --> 00:26:28.415 Looking at this example, 395 00:26:28.416 --> 00:26:31.717 after using new followed by the class name, 396 00:26:31.718 --> 00:26:34.236 you can see parentheses 397 00:26:34.243 --> 00:26:38.811 The presence of parentheses indicates that a function is being called 398 00:26:39.603 --> 00:26:41.954 Even though you don’t see it explicitly written in the code, 399 00:26:42.363 --> 00:26:45.019 you’re still invoking a function 400 00:26:45.617 --> 00:26:48.532 This function is referred to as the default constructor 401 00:26:49.018 --> 00:26:50.903 A default function, 402 00:26:50.903 --> 00:26:53.287 or a default constructor 403 00:26:53.287 --> 00:26:55.745 This is how it looks like 404 00:26:55.927 --> 00:26:57.561 Type public 405 00:26:58.567 --> 00:27:00.871 And write in the class name 406 00:27:01.556 --> 00:27:02.766 But this 407 00:27:02.767 --> 00:27:06.963 does not specify a return type 408 00:27:06.963 --> 00:27:10.938 Let's write this now 409 00:27:10.938 --> 00:27:13.843 This is how a constructor function looks like 410 00:27:15.018 --> 00:27:21.069 It is automatically called 411 00:27:21.635 --> 00:27:24.046 when an object is created 412 00:27:24.667 --> 00:27:25.753 There is 413 00:27:26.241 --> 00:27:29.902 also a function automatically called when an object is destroyed, 414 00:27:29.903 --> 00:27:32.318 called a destructor 415 00:27:32.335 --> 00:27:34.636 Let's write this 416 00:27:34.637 --> 00:27:37.830 A destructor is defined by prefixing 417 00:27:37.830 --> 00:27:39.784 the class name with a tilde 418 00:27:39.785 --> 00:27:44.267 It is automatically triggered when the object is no longer in use 419 00:27:44.267 --> 00:27:45.783 and the memory is being reclaimed 420 00:27:46.279 --> 00:27:48.411 However, in this example, an error occurs 421 00:27:48.411 --> 00:27:51.116 because the destructor was defined with the public keyword 422 00:27:51.355 --> 00:27:53.735 Destructors are not meant to be explicitly called by the programmer 423 00:27:53.868 --> 00:27:56.770 but are instead invoked automatically by the system 424 00:27:57.390 --> 00:27:59.253 Writing it 425 00:27:59.254 --> 00:28:01.201 without the public keyword resolves this issue 426 00:28:01.636 --> 00:28:04.187 But destructors are rarely used 427 00:28:04.446 --> 00:28:08.410 because in C#, memory management 428 00:28:08.411 --> 00:28:10.166 does not require creating 429 00:28:10.166 --> 00:28:14.011 and intentionally destroying an object 430 00:28:14.320 --> 00:28:17.090 It is handled by the garbage collector 431 00:28:17.090 --> 00:28:18.224 or GC 432 00:28:18.224 --> 00:28:22.078 The GC identifies objects that 433 00:28:22.078 --> 00:28:25.475 are no longer in use 434 00:28:25.476 --> 00:28:27.745 and automatically frees the memory they occupy 435 00:28:27.866 --> 00:28:31.689 At this point, the destructor is called internally to clean up resources 436 00:28:31.956 --> 00:28:36.334 Rather than relying on destructors, 437 00:28:36.335 --> 00:28:37.978 if an object is no longer needed, 438 00:28:38.712 --> 00:28:41.945 creating and invoking a dedicated function 439 00:28:42.216 --> 00:28:43.375 to clean up and mark the object as unused 440 00:28:43.375 --> 00:28:44.848 is often a clearer 441 00:28:45.176 --> 00:28:47.535 and more efficient approach 442 00:28:47.875 --> 00:28:53.084 But that's my personal opinion 443 00:28:53.588 --> 00:28:57.485 Every programmer has a different method 444 00:28:57.486 --> 00:28:58.819 So just know that this is one way 445 00:28:59.576 --> 00:29:02.533 I’m not going to cover destructors in detail 446 00:29:02.942 --> 00:29:05.177 This is what a constructor function looks like 447 00:29:05.177 --> 00:29:06.746 So, in the constructor, 448 00:29:06.980 --> 00:29:09.852 I’ll include what we did below like this 449 00:29:09.855 --> 00:29:12.354 Then this becomes a reference to itself 450 00:29:12.354 --> 00:29:14.091 I'll write it like this 451 00:29:14.092 --> 00:29:17.006 But actually, if you look here, it’s written like this 452 00:29:17.007 --> 00:29:19.408 If you write this in front, 453 00:29:19.408 --> 00:29:21.635 this refers to this instance 454 00:29:22.021 --> 00:29:23.891 So earlier, when I explained, 455 00:29:24.095 --> 00:29:25.535 if in this function, 456 00:29:27.537 --> 00:29:31.936 something like this was created, 457 00:29:33.556 --> 00:29:37.194 this could be very confusing 458 00:29:37.195 --> 00:29:38.605 and could cause issues 459 00:29:38.796 --> 00:29:40.750 Because even though there’s no error, 460 00:29:40.751 --> 00:29:45.125 if the field outside and the local variable have the same name, 461 00:29:45.331 --> 00:29:47.613 in that case, let’s say here, 462 00:29:47.613 --> 00:29:50.937 you want to assign a value to the name field 463 00:29:50.937 --> 00:29:53.569 Then, if you check, you’ll see this happens 464 00:29:54.139 --> 00:29:55.777 Since the name is the same, 465 00:29:55.777 --> 00:29:58.566 you might intend to assign a value here, 466 00:29:58.570 --> 00:29:59.497 but the name is the same 467 00:29:59.497 --> 00:30:01.776 So in this function, 468 00:30:02.018 --> 00:30:06.072 if you want to access the field outside and assign a value, 469 00:30:06.073 --> 00:30:06.955 it becomes ambiguous 470 00:30:06.955 --> 00:30:11.936 So, by writing this. in front, 471 00:30:12.221 --> 00:30:15.796 you can access the field, not the local variable, 472 00:30:15.994 --> 00:30:19.485 and assign a value to it 473 00:30:19.796 --> 00:30:21.736 That’s why the this keyword exists 474 00:30:22.085 --> 00:30:24.655 This is specifically used when 475 00:30:24.656 --> 00:30:30.893 an object created from this class refers to itself 476 00:30:31.356 --> 00:30:34.214 Let’s move on from this for now 477 00:30:34.820 --> 00:30:36.931 This is how it is written 478 00:30:40.763 --> 00:30:42.595 Now let’s try outputting it 479 00:30:42.901 --> 00:30:45.952 When creating a class like this, 480 00:30:46.256 --> 00:30:52.557 it’s better to stack functions neatly inside the class for processing 481 00:30:52.909 --> 00:30:57.287 What I mean is, instead of creating an Animal object 482 00:30:57.288 --> 00:30:58.423 and printing its name externally like this, 483 00:30:58.723 --> 00:31:04.707 if you define a function inside the Animal class that prints its own name, 484 00:31:04.903 --> 00:31:08.108 it becomes easier to access and use 485 00:31:09.082 --> 00:31:11.525 So instead of doing it here, 486 00:31:11.666 --> 00:31:15.191 I’ll create a print function here 487 00:31:15.191 --> 00:31:18.149 public void print 488 00:31:18.149 --> 00:31:21.641 and print name 489 00:31:21.642 --> 00:31:23.982 And it will print the name 490 00:31:23.983 --> 00:31:25.916 In this case, you can replace animal with this 491 00:31:26.324 --> 00:31:28.746 And this can also be omitted here 492 00:31:30.130 --> 00:31:31.958 If you don’t use this, 493 00:31:31.959 --> 00:31:34.733 by default, it refers to this instance 494 00:31:37.254 --> 00:31:40.451 Now, when you create an Animal object, 495 00:31:41.403 --> 00:31:43.445 you can type animal. and see 496 00:31:43.446 --> 00:31:49.020 the list of properties and functions that animal has 497 00:31:49.202 --> 00:31:50.827 So you can say something like this 498 00:31:50.827 --> 00:31:53.448 Hey Animal, 499 00:31:53.448 --> 00:31:57.480 Animal, show me your properties or functions, 500 00:31:58.043 --> 00:32:01.529 show me the function to print your name 501 00:32:01.530 --> 00:32:03.089 This way, you can access it 502 00:32:03.267 --> 00:32:04.670 And since this is a function, 503 00:32:04.671 --> 00:32:07.434 you must include parentheses at the end 504 00:32:08.843 --> 00:32:13.042 Since there are no parameters, you can finish it like this 505 00:32:14.359 --> 00:32:18.781 When you run this, you’ll see that the name Animal is printed 506 00:32:20.448 --> 00:32:22.460 Pretty simple 507 00:32:22.486 --> 00:32:24.391 Class is created like this 508 00:32:26.322 --> 00:32:32.267 So, in a way, we learned about variables and functions earlier, right? 509 00:32:32.267 --> 00:32:37.187 Those are actually important components that make up a class 510 00:32:37.193 --> 00:32:41.789 That’s why we covered them first, 511 00:32:41.790 --> 00:32:44.111 and now, through classes, we can utilize them effectively 512 00:32:48.693 --> 00:32:51.490 Now, let’s try inheritance 513 00:32:52.193 --> 00:32:57.093 Earlier, I mentioned that there are four key elements in object-oriented programming 514 00:32:57.093 --> 00:33:00.302 Among them, I’ll explain inheritance 515 00:33:00.693 --> 00:33:03.434 To implement inheritance, let’s set Animal as the parent class 516 00:33:03.434 --> 00:33:07.793 and create a class called Cat that inherits from it 517 00:33:07.793 --> 00:33:11.353 Similarly, we write public, 518 00:33:12.693 --> 00:33:15.156 followed by class Cat 519 00:33:15.893 --> 00:33:19.793 To indicate inheritance, leave a space and add a colon, 520 00:33:19.793 --> 00:33:23.447 and then write the name of the class you want to inherit from 521 00:33:23.893 --> 00:33:27.393 Naturally, 522 00:33:27.393 --> 00:33:29.857 you also define a scope using curly braces 523 00:33:31.593 --> 00:33:35.981 Now, instead of creating an Animal instance, 524 00:33:36.792 --> 00:33:38.519 let’s try creating a Cat instance 525 00:33:40.093 --> 00:33:43.292 Since Cat inherits everything from Animal, 526 00:33:43.292 --> 00:33:46.297 it has all the elements that Animal contains 527 00:33:46.992 --> 00:33:53.827 When you create a variable in the parent format, like Animal, 528 00:33:53.837 --> 00:33:57.292 what actually gets created through new 529 00:33:57.292 --> 00:34:00.392 is an object in memory that adheres to the format 530 00:34:00.392 --> 00:34:03.292 The format created in memory 531 00:34:03.292 --> 00:34:04.757 takes the form of Cat 532 00:34:04.757 --> 00:34:07.800 but transforms and is stored in the parent format 533 00:34:07.800 --> 00:34:09.819 That’s the meaning here 534 00:34:10.493 --> 00:34:13.489 This process involves type conversion 535 00:34:14.093 --> 00:34:17.693 I will cover this again later 536 00:34:17.693 --> 00:34:19.462 So, you can create it like this 537 00:34:19.693 --> 00:34:22.693 This means that A cat is an animal 538 00:34:22.693 --> 00:34:24.525 That's one way we can do this 539 00:34:24.993 --> 00:34:27.446 However, you need to be careful about one thing 540 00:34:28.193 --> 00:34:31.493 If you follow this method exactly 541 00:34:31.493 --> 00:34:33.292 but reverse the order, 542 00:34:33.292 --> 00:34:35.693 for example, making this Animal 543 00:34:35.693 --> 00:34:37.892 and making this Cat, 544 00:34:37.892 --> 00:34:39.493 then there will be a problem 545 00:34:39.493 --> 00:34:43.143 The issue arises because, in memory, when a smaller object is created, 546 00:34:43.593 --> 00:34:47.693 and you try to store it as a larger type, 547 00:34:47.693 --> 00:34:48.892 this leads to inconsistencies 548 00:34:48.892 --> 00:34:51.346 What I mean can be explained with a visual 549 00:34:55.047 --> 00:34:57.917 Let’s say we have Animal and we have Cat 550 00:34:58.135 --> 00:34:59.881 If Animal takes up this much space, 551 00:35:00.094 --> 00:35:01.247 and Cat takes up this much space, 552 00:35:02.188 --> 00:35:04.058 then if you create an object of type A, 553 00:35:04.059 --> 00:35:06.624 representing all of what Animal is 554 00:35:06.985 --> 00:35:10.525 Now, after creating A in the memory, 555 00:35:11.487 --> 00:35:14.106 and then try to assign it to C, 556 00:35:15.280 --> 00:35:17.858 like in this shape 557 00:35:17.859 --> 00:35:18.697 But this doesn’t work 558 00:35:18.698 --> 00:35:22.520 The reason is that the actual memory size for A is smaller, 559 00:35:23.370 --> 00:35:28.087 but you’re trying to use it as a larger object, 560 00:35:28.221 --> 00:35:29.415 which doesn’t make sense 561 00:35:29.703 --> 00:35:31.139 In essence, 562 00:35:31.486 --> 00:35:32.952 we have Animal 563 00:35:32.953 --> 00:35:33.953 Animal 564 00:35:36.913 --> 00:35:37.726 is Cat 565 00:35:39.737 --> 00:35:43.086 That's what we're saying 566 00:35:43.086 --> 00:35:45.845 Cat is smaller than an animal in terms of memory structure, 567 00:35:46.537 --> 00:35:47.577 and there is an issue 568 00:35:47.577 --> 00:35:49.046 An animal is not a cat, right? 569 00:35:49.897 --> 00:35:51.064 Conversely, 570 00:35:51.697 --> 00:35:54.617 let's say this is bigger here 571 00:35:54.617 --> 00:35:55.657 Let's say we have Cat 572 00:35:55.657 --> 00:35:56.533 Cat 573 00:35:59.610 --> 00:36:00.610 is Animal 574 00:36:02.537 --> 00:36:04.257 That's the sentence 575 00:36:04.257 --> 00:36:05.419 This sentence is for this 576 00:36:05.889 --> 00:36:06.977 The equal sign, yeah? 577 00:36:06.977 --> 00:36:10.576 The assignment operator = first processes the right-hand side 578 00:36:10.576 --> 00:36:12.537 So, a cat is an animal 579 00:36:12.537 --> 00:36:13.713 becomes the valid expression 580 00:36:13.728 --> 00:36:16.177 However, an animal is a cat 581 00:36:16.177 --> 00:36:17.137 doesn’t make sense, right? 582 00:36:17.697 --> 00:36:18.537 Animal is Cat 583 00:36:18.537 --> 00:36:19.896 his illustrates the logical flow 584 00:36:19.896 --> 00:36:21.657 So this is how it works 585 00:36:21.657 --> 00:36:24.174 If we visualize this, 586 00:36:25.337 --> 00:36:26.857 we have an Animal 587 00:36:26.857 --> 00:36:29.143 and below it, a Cat that inherits from it 588 00:36:33.817 --> 00:36:35.936 When we create a Cat object, 589 00:36:36.234 --> 00:36:39.906 it is treated as an Animal object 590 00:36:39.940 --> 00:36:41.577 This transition moves upwards, 591 00:36:41.577 --> 00:36:46.217 and such a change is referred to as upcasting 592 00:36:46.217 --> 00:36:47.416 Changing the type like this 593 00:36:47.416 --> 00:36:52.417 is what we call type casting 594 00:36:53.656 --> 00:36:54.857 Specifically, this is called type conversion 595 00:36:54.857 --> 00:36:56.614 or type cast 596 00:36:58.137 --> 00:36:58.937 Type cast 597 00:36:58.937 --> 00:37:00.857 or type conversion 598 00:37:00.857 --> 00:37:02.757 Among type conversions, 599 00:37:03.137 --> 00:37:04.857 when a larger type moves to a smaller one, 600 00:37:04.857 --> 00:37:06.813 this is upcasting 601 00:37:07.137 --> 00:37:08.696 On the other hand, 602 00:37:08.696 --> 00:37:10.016 when a smaller type transitions into a larger one, 603 00:37:10.016 --> 00:37:14.577 this is referred to as downcasting 604 00:37:14.577 --> 00:37:16.016 That's the term 605 00:37:16.016 --> 00:37:18.137 Upcasting does not pose any issues 606 00:37:18.137 --> 00:37:21.897 This is because a larger object 607 00:37:22.336 --> 00:37:24.052 is adjusted to fit into a smaller type 608 00:37:24.497 --> 00:37:27.777 But creating a smaller object and then expanding it into a larger object? 609 00:37:29.056 --> 00:37:31.457 If the memory is already allocated for the smaller object, 610 00:37:31.457 --> 00:37:33.783 arbitrarily attempting to expand it into a larger object would cause issues 611 00:37:34.617 --> 00:37:38.056 So downcasting must be used carefully 612 00:37:38.056 --> 00:37:39.888 When is downcasting allowed? 613 00:37:40.416 --> 00:37:41.897 If you initially create a larger object, 614 00:37:41.897 --> 00:37:45.060 convert it into a smaller object for some processing, 615 00:37:45.796 --> 00:37:47.097 like when you convert a Cat object into an Animal object, 616 00:37:47.097 --> 00:37:49.416 then convert that Animal back into a Cat object, 617 00:37:49.416 --> 00:37:51.056 that's downcasting 618 00:37:51.056 --> 00:37:52.617 This process is returning to the original size 619 00:37:55.217 --> 00:37:57.137 Upcasting, on the other hand, happens naturally 620 00:37:57.137 --> 00:38:00.701 So please remember these precautions 621 00:38:02.256 --> 00:38:04.577 So, this causes an error 622 00:38:04.577 --> 00:38:05.511 Doesn't work 623 00:38:08.976 --> 00:38:12.359 So, this is called upcasting 624 00:38:14.857 --> 00:38:18.460 When we talk about upcasting and downcasting, it can get very confusing 625 00:38:18.855 --> 00:38:20.377 Even though I’ve drawn it out, 626 00:38:20.377 --> 00:38:23.056 think of it as a relationship between classes. 627 00:38:23.056 --> 00:38:24.776 After forming this relationship, 628 00:38:24.776 --> 00:38:26.457 if the movement is upwards, 629 00:38:26.457 --> 00:38:28.377 then that’s upcasting 630 00:38:28.377 --> 00:38:29.857 If the movement is downwards, 631 00:38:29.857 --> 00:38:31.137 then that’s downcasting 632 00:38:31.137 --> 00:38:33.073 You can think of it this way 633 00:38:34.721 --> 00:38:36.544 Once everything is set up this way, 634 00:38:38.377 --> 00:38:39.976 all properties are inherited 635 00:38:39.976 --> 00:38:42.657 If we run the program, 636 00:38:42.657 --> 00:38:44.416 since everything is inherited, 637 00:38:44.416 --> 00:38:45.524 the same output, Animal, 638 00:38:45.525 --> 00:38:47.021 will be displayed like this 639 00:38:49.274 --> 00:38:51.395 Now, let’s try something else 640 00:38:51.896 --> 00:38:55.338 The Cat class has inherited everything from Animal 641 00:38:55.893 --> 00:38:59.013 It's supposed to be Cat, 642 00:38:59.336 --> 00:39:01.536 but since we used Animal here, 643 00:39:01.536 --> 00:39:03.550 we're seeing Animal being printed 644 00:39:03.937 --> 00:39:07.678 For instance, for the Cat class, 645 00:39:09.900 --> 00:39:12.616 we want the name property to display Cat, 646 00:39:12.616 --> 00:39:14.619 instead of Animal 647 00:39:15.404 --> 00:39:16.536 Fo literally, 648 00:39:16.536 --> 00:39:18.657 we want to rename it as Cat 649 00:39:18.657 --> 00:39:23.134 How can we do that? Here's how 650 00:39:23.457 --> 00:39:25.416 We declare 651 00:39:25.416 --> 00:39:26.737 a public constructor 652 00:39:26.737 --> 00:39:28.244 for the Cat class 653 00:39:32.217 --> 00:39:33.437 And here, 654 00:39:34.657 --> 00:39:37.946 the name, Cat 655 00:39:39.105 --> 00:39:40.811 Let's change it like this 656 00:39:41.696 --> 00:39:43.749 Now run it to see 657 00:39:44.071 --> 00:39:44.883 this right here 658 00:39:48.696 --> 00:39:51.806 See, Cat is indeed Animal 659 00:39:51.968 --> 00:39:56.376 But now that we have an object, Cat, 660 00:39:56.376 --> 00:39:58.916 this is being called like this 661 00:39:59.617 --> 00:40:03.656 So the order would've been this first and then this 662 00:40:03.656 --> 00:40:05.096 Let's try printing it 663 00:40:05.096 --> 00:40:07.330 Let’s confirm this 664 00:40:07.816 --> 00:40:12.720 In the Animal constructor, 665 00:40:12.777 --> 00:40:15.096 I’ll add the line saying 666 00:40:15.096 --> 00:40:16.536 Animal constructor is called 667 00:40:16.536 --> 00:40:19.256 Here, Cat constructor is called 668 00:40:19.256 --> 00:40:21.739 Let me write it out 669 00:40:22.696 --> 00:40:24.216 So this is what happens 670 00:40:24.216 --> 00:40:27.376 You’ll see the Animal constructor is called first, 671 00:40:27.376 --> 00:40:31.699 and then the Cat constructor is called 672 00:40:31.976 --> 00:40:33.696 So, both the parent and the child constructors are called 673 00:40:33.696 --> 00:40:34.256 Like this 674 00:40:41.936 --> 00:40:44.656 Constructors function in this way, 675 00:40:44.656 --> 00:40:46.564 but there’s one more thing I’d like to try 676 00:40:47.897 --> 00:40:49.864 What I’d like to do is create a specific functionality 677 00:40:50.057 --> 00:40:54.696 I’m going to add a function called walking to the parent class 678 00:40:54.696 --> 00:40:59.017 hen, I want to modify the walking function in the child class 679 00:40:59.017 --> 00:40:59.965 to handle it differently 680 00:40:59.966 --> 00:41:01.943 This is something you can do 681 00:41:01.944 --> 00:41:04.536 So let’s add a function 682 00:41:04.536 --> 00:41:06.816 to the Animal class 683 00:41:06.816 --> 00:41:09.137 I’ll define it as public 684 00:41:09.137 --> 00:41:17.137 We’ll call the function void Walk 685 00:41:17.137 --> 00:41:22.376 The walk function will have a default behavior 686 00:41:22.376 --> 00:41:25.295 When walking, I'll say, thump thump 687 00:41:27.177 --> 00:41:31.387 So in the Animal class, walking will produce a thump thump sound 688 00:41:31.777 --> 00:41:34.376 However, I want walking in the Cat class to behave differently 689 00:41:34.376 --> 00:41:36.577 For now, let’s run the walk function in the Animal class and check the output 690 00:41:36.577 --> 00:41:37.288 Animal 691 00:41:37.702 --> 00:41:39.009 Although it’s technically an Animal, 692 00:41:39.023 --> 00:41:40.383 it is actually a Cat object 693 00:41:40.816 --> 00:41:44.970 Let’s call the walk function on the Cat object 694 00:41:49.496 --> 00:41:52.969 This should call the parent class’s function 695 00:41:55.297 --> 00:41:57.696 Let's change it like this 696 00:41:57.696 --> 00:41:59.753 Let's delete this 697 00:42:00.656 --> 00:42:02.617 This right here 698 00:42:02.617 --> 00:42:04.842 is still in Animal 699 00:42:04.845 --> 00:42:08.722 Although we’re calling it Animal, the actual data type is Cat 700 00:42:09.514 --> 00:42:12.371 Let’s create the walk function 701 00:42:12.822 --> 00:42:15.740 from the parent, Animal, 702 00:42:16.816 --> 00:42:19.096 to its child, Cat, as well, 703 00:42:19.096 --> 00:42:22.176 just like we did with the constructor earlier 704 00:42:22.373 --> 00:42:25.536 For example, how does a cat walk? 705 00:42:25.536 --> 00:42:27.096 A cat probably walks quietly or softly, right? 706 00:42:27.096 --> 00:42:31.262 So let’s have the walk function output tiptoeing 707 00:42:32.496 --> 00:42:36.337 The walk function in the parent class produces a thump thump sound, 708 00:42:36.337 --> 00:42:41.177 while the walk function in the Cat class outputs tiptoeing 709 00:42:41.177 --> 00:42:42.531 Now see, 710 00:42:43.416 --> 00:42:45.137 when you call the walk function 711 00:42:45.137 --> 00:42:49.617 on a Cat object, what is being called 712 00:42:49.617 --> 00:42:52.017 is the tiptoeing we added 713 00:42:53.384 --> 00:42:56.219 However, we don’t always use it this way 714 00:42:57.220 --> 00:43:00.230 Instead of directly calling the walk function on the Cat class, 715 00:43:00.231 --> 00:43:04.437 we often keep the variable in the parent class format 716 00:43:04.804 --> 00:43:08.990 and the object in the child class format 717 00:43:09.479 --> 00:43:11.004 The benefit of this approach is this 718 00:43:11.004 --> 00:43:14.263 For instance, if you create a Dog class later, 719 00:43:14.740 --> 00:43:15.796 even with that, 720 00:43:15.797 --> 00:43:18.477 you won’t need to modify this part of the code 721 00:43:18.743 --> 00:43:20.876 You just need to change this part to dog 722 00:43:20.879 --> 00:43:23.213 For example, copy this as is, 723 00:43:24.760 --> 00:43:29.917 then modify it like this, replacing it with dog 724 00:43:30.384 --> 00:43:32.283 Call this Puppy 725 00:43:33.744 --> 00:43:34.865 Dog 726 00:43:36.293 --> 00:43:40.432 For the walk function, let’s say it walks with a da-da-da-da sound 727 00:43:43.156 --> 00:43:46.083 Now that we’ve done this, 728 00:43:46.084 --> 00:43:48.798 the advantage here is this 729 00:43:48.799 --> 00:43:53.482 even if we change things like this, there are no issues 730 00:43:55.960 --> 00:43:57.356 You don’t need to modify this part at all 731 00:43:58.309 --> 00:44:01.031 Just change the object being created, 732 00:44:01.032 --> 00:44:04.317 and if you run it like this, it switches cleanly to the Puppy 733 00:44:05.619 --> 00:44:08.118 Now, if you switch back to cat, 734 00:44:12.127 --> 00:44:16.275 you’ll see that everything outputs as thump thump 735 00:44:16.275 --> 00:44:20.899 This happens because, as I mentioned earlier, 736 00:44:20.899 --> 00:44:22.273 when I created this, 737 00:44:22.844 --> 00:44:26.549 if I call one cat and the other puppy, 738 00:44:32.275 --> 00:44:34.614 and add outputs for the puppy too 739 00:44:38.597 --> 00:44:41.678 If I set this up to print, it’ll behave like this 740 00:44:41.878 --> 00:44:43.063 Thump thump here, 741 00:44:45.026 --> 00:44:47.047 and here's Pupy 742 00:44:50.290 --> 00:44:53.843 Both the cat and the dog will output thump thump 743 00:44:54.431 --> 00:44:56.893 What we want is not thump thump, 744 00:44:57.218 --> 00:45:01.987 but for each to call its own walk function 745 00:45:01.987 --> 00:45:03.177 This isn’t happening because, 746 00:45:03.511 --> 00:45:08.246 in programming, there’s a rule that when calling functions or methods, 747 00:45:08.398 --> 00:45:13.421 the function being called doesn’t belong to the created object 748 00:45:13.661 --> 00:45:14.651 It belongs 749 00:45:14.652 --> 00:45:16.800 to the type of the variable on the left side 750 00:45:16.800 --> 00:45:21.058 So, it’s the function of the variable’s data type that gets called 751 00:45:21.544 --> 00:45:26.264 For the walk function, this means 752 00:45:26.264 --> 00:45:28.314 it calls Animal’s walk 753 00:45:28.315 --> 00:45:29.811 The same applies to the print function 754 00:45:30.767 --> 00:45:34.853 What we want, though, is, let's keep this here 755 00:45:35.155 --> 00:45:37.085 When the object changes, 756 00:45:37.215 --> 00:45:40.119 call Cat’s walk for cats 757 00:45:40.424 --> 00:45:43.267 and Dog’s walk for Puppy 758 00:45:43.830 --> 00:45:45.664 It should follow the type of the created object 759 00:45:46.979 --> 00:45:49.681 There’s a way to achieve this, which is 760 00:45:50.024 --> 00:45:51.798 to override the function 761 00:45:52.015 --> 00:45:55.092 Currently, the function hasn’t been properly overridden 762 00:45:55.519 --> 00:45:58.484 There’s a concept of overriding a function’s behavior 763 00:45:58.484 --> 00:46:00.970 When we have functions 764 00:46:00.976 --> 00:46:03.933 with the same name but different parameters, 765 00:46:04.037 --> 00:46:07.227 we call it overloading 766 00:46:07.384 --> 00:46:08.546 which is in overlap 767 00:46:08.729 --> 00:46:11.718 Functions with the same name 768 00:46:12.119 --> 00:46:14.105 but different argument lists 769 00:46:14.105 --> 00:46:16.030 are selected based on the parameters passed to them 770 00:46:16.031 --> 00:46:20.293 Automatically calling the appropriate function 771 00:46:20.293 --> 00:46:22.252 based on the given parameters, that's overloading 772 00:46:22.409 --> 00:46:23.763 And we also have 773 00:46:23.763 --> 00:46:25.978 a concept called overriding 774 00:46:26.144 --> 00:46:27.208 This means 775 00:46:27.451 --> 00:46:30.563 we can override a parent class’s function 776 00:46:30.564 --> 00:46:33.929 in the child class 777 00:46:34.183 --> 00:46:37.367 To do this, 778 00:46:37.368 --> 00:46:40.357 we use the override keyword 779 00:46:40.968 --> 00:46:47.145 Here’s how it looks 780 00:46:47.463 --> 00:46:51.453 We add the override keyword to the child class’s function, 781 00:46:51.453 --> 00:46:54.486 and virtual to the parent 782 00:46:55.984 --> 00:47:01.329 By making it virtual, 783 00:47:01.330 --> 00:47:05.434 the child class can override it 784 00:47:05.435 --> 00:47:07.728 So, this is how you write it 785 00:47:07.903 --> 00:47:10.119 So what happens is this 786 00:47:11.511 --> 00:47:13.586 Cat's Walk, 787 00:47:15.503 --> 00:47:17.699 though we just see Animal here, 788 00:47:18.345 --> 00:47:19.772 let's finish Puppy too 789 00:47:19.773 --> 00:47:21.328 Override here too 790 00:47:23.183 --> 00:47:25.185 Let's write this and run it 791 00:47:26.400 --> 00:47:27.066 It changed 792 00:47:28.078 --> 00:47:31.944 The cat outputs tiptoeing, and the dog outputs da-da-da-da 793 00:47:32.703 --> 00:47:36.156 So, by using virtual and override, 794 00:47:36.338 --> 00:47:38.999 you ensure 795 00:47:39.868 --> 00:47:42.672 that the function corresponds 796 00:47:42.904 --> 00:47:49.334 not to the data type of the variable, but to the object’s type 797 00:47:49.334 --> 00:47:51.137 This is very important 798 00:47:51.138 --> 00:47:52.980 You should remember that 799 00:47:53.583 --> 00:47:56.046 Now, let’s try encapsulation 800 00:47:56.208 --> 00:47:58.277 Earlier, I mentioned 801 00:47:58.281 --> 00:48:00.084 that we can hide certain functionalities 802 00:48:00.503 --> 00:48:03.959 I’ll try implementing this in the Animal class 803 00:48:04.904 --> 00:48:06.062 In Animal, 804 00:48:06.579 --> 00:48:07.913 instead of the name, 805 00:48:08.544 --> 00:48:10.281 I’ll define an age 806 00:48:10.891 --> 00:48:13.097 So, this time, instead of public, 807 00:48:13.503 --> 00:48:19.647 I’ll make it private int age 808 00:48:25.502 --> 00:48:28.865 Then, in the Animal class, 809 00:48:29.373 --> 00:48:30.515 I’ll set the age 810 00:48:31.830 --> 00:48:32.615 to 10 811 00:48:32.615 --> 00:48:35.003 Let's say this 812 00:48:39.182 --> 00:48:41.403 To display this, we need a function to output the value 813 00:48:41.404 --> 00:48:44.361 Since it’s private, you can’t access it directly from outside 814 00:48:44.362 --> 00:48:46.509 For example, if I create an Animal instance 815 00:48:46.830 --> 00:48:50.204 and try to access the age field, 816 00:48:50.204 --> 00:48:52.942 it won’t appear in the list of accessible members 817 00:48:52.943 --> 00:48:57.503 To explain, let's try Animal here 818 00:49:00.246 --> 00:49:04.075 This here allows you to split the screen 819 00:49:04.076 --> 00:49:04.913 Like this 820 00:49:04.914 --> 00:49:05.626 Here it is 821 00:49:05.626 --> 00:49:06.818 Just pull it up like this 822 00:49:07.393 --> 00:49:10.952 We have the variable age for Animal 823 00:49:12.249 --> 00:49:14.293 Let's create it here 824 00:49:21.736 --> 00:49:24.304 Let's say we have it like this 825 00:49:24.305 --> 00:49:25.330 Now in Animal, 826 00:49:25.331 --> 00:49:27.388 click on Animal 827 00:49:27.967 --> 00:49:28.967 We have name 828 00:49:29.391 --> 00:49:30.627 We have name, 829 00:49:31.113 --> 00:49:32.933 but we don't have age 830 00:49:33.292 --> 00:49:37.318 It's private, so it's not open here 831 00:49:37.626 --> 00:49:40.124 Let's switch this to public 832 00:49:41.713 --> 00:49:42.782 We have age 833 00:49:43.767 --> 00:49:46.228 Switch it back to private, 834 00:49:47.632 --> 00:49:50.810 press ctrl and space bar to see it again 835 00:49:51.051 --> 00:49:52.693 Now the age is gone 836 00:49:53.207 --> 00:49:55.475 So whether to keep it 837 00:49:55.476 --> 00:49:56.776 public or private 838 00:49:56.780 --> 00:49:58.569 is up to the developer 839 00:49:58.833 --> 00:50:01.802 So since age is private now, 840 00:50:02.604 --> 00:50:05.156 we may want to print this 841 00:50:05.171 --> 00:50:09.003 So let's add another function here 842 00:50:09.513 --> 00:50:11.754 Below PrintName, 843 00:50:14.592 --> 00:50:17.147 make a function called PrintAge 844 00:50:17.680 --> 00:50:19.673 Copy it 845 00:50:19.674 --> 00:50:22.256 and call age 846 00:50:25.713 --> 00:50:27.171 Like so 847 00:50:28.661 --> 00:50:29.763 Here, age 848 00:50:29.861 --> 00:50:30.990 PrintAge 849 00:50:31.730 --> 00:50:34.115 Now it calls 850 00:50:34.975 --> 00:50:37.573 Now, 10 851 00:50:38.859 --> 00:50:43.122 Let's delete this part with // 852 00:50:43.153 --> 00:50:45.020 Let's remove all these 853 00:50:52.353 --> 00:50:55.276 Let'a call PrintAge in here 854 00:50:56.052 --> 00:50:58.574 Now it provides 10 855 00:50:58.851 --> 00:51:04.604 This is how certain information can be private 856 00:51:04.731 --> 00:51:08.060 If it's not private but protected, 857 00:51:08.529 --> 00:51:10.600 so now in the parent, 858 00:51:12.171 --> 00:51:13.578 let's see it here 859 00:51:14.071 --> 00:51:17.054 The parent can be private too 860 00:51:17.062 --> 00:51:20.342 So the child who inherits this can't access it 861 00:51:20.371 --> 00:51:24.688 Se, I can't try age under name equals Cat 862 00:51:25.572 --> 00:51:26.514 We see no age 863 00:51:27.451 --> 00:51:33.071 If we change this to protected, 864 00:51:33.120 --> 00:51:34.867 like this, 865 00:51:35.768 --> 00:51:38.929 in Cat, type age and you see it 866 00:51:38.930 --> 00:51:40.544 like this 867 00:51:41.738 --> 00:51:45.693 Let's say 20 for Cat and 30 for Puppy 868 00:51:46.451 --> 00:51:48.170 For this, 869 00:51:49.595 --> 00:51:51.136 let's do PrintAge for both 870 00:52:01.730 --> 00:52:04.094 So Animal, PrintAge 871 00:52:04.094 --> 00:52:06.279 Let's do PrintAge for both cat and dog 872 00:52:08.131 --> 00:52:11.016 Now we see 10, 20, 30 873 00:52:11.017 --> 00:52:12.909 it tracks all of the ages 874 00:52:14.397 --> 00:52:17.224 So using private an protected, 875 00:52:17.225 --> 00:52:21.648 we allow certain accessibility 876 00:52:21.649 --> 00:52:23.838 to the things to tailor our needs 877 00:52:24.370 --> 00:52:28.944 Lastly, let’s talk about polymorphism 878 00:52:29.165 --> 00:52:34.041 Actually, what we’ve been discussing so far aligns with the topic of polymorphism 879 00:52:34.272 --> 00:52:38.808 Polymorphism is essentially about how we automatically typecast objects, 880 00:52:38.809 --> 00:52:41.232 changing their type format, as we’ve seen 881 00:52:41.610 --> 00:52:43.133 Like this 882 00:52:44.843 --> 00:52:48.341 What we can see here 883 00:52:48.771 --> 00:52:50.078 is something like this 884 00:52:50.423 --> 00:52:51.720 When I first talked about polymorphism, 885 00:52:51.720 --> 00:52:54.620 I said this in my diagram 886 00:52:55.290 --> 00:52:57.512 There is a parent class A and a child class B, 887 00:52:58.749 --> 00:53:02.780 A looking this big and B this big 888 00:53:03.262 --> 00:53:05.168 in an inheritance 889 00:53:05.168 --> 00:53:07.781 Then A belongs in here 890 00:53:08.604 --> 00:53:12.790 Suppose A has a certain set of variables 891 00:53:12.791 --> 00:53:15.472 and to B, 892 00:53:15.473 --> 00:53:17.837 I created another variable or function 893 00:53:17.849 --> 00:53:21.092 Let's create a function called A 894 00:53:21.657 --> 00:53:24.911 So when the object is type A, 895 00:53:25.296 --> 00:53:27.844 we can create it as type B 896 00:53:30.131 --> 00:53:31.910 and have it like this 897 00:53:32.672 --> 00:53:34.939 But in the end, 898 00:53:34.940 --> 00:53:36.804 if it expresses this as a 899 00:53:36.804 --> 00:53:41.402 a.AAA does not exist 900 00:53:41.865 --> 00:53:42.786 It does not 901 00:53:42.862 --> 00:53:48.058 Because, this A is in fact B in the memory, 902 00:53:48.164 --> 00:53:51.802 shortened to contain the form of A 903 00:53:52.211 --> 00:53:54.103 So this is the form it has 904 00:53:54.225 --> 00:53:57.635 So there's no function called AAA 905 00:53:58.011 --> 00:54:03.307 It should be B so that it allows access to this function of this member 906 00:54:04.234 --> 00:54:06.275 So that's what polymorphism does 907 00:54:06.275 --> 00:54:10.461 Let's demonstrate it 908 00:54:10.637 --> 00:54:12.867 Here we have our Puppy part 909 00:54:12.868 --> 00:54:14.744 In here, 910 00:54:16.146 --> 00:54:18.235 I'll create a variable called type 911 00:54:19.607 --> 00:54:21.868 Let's create a function called PrintType 912 00:54:22.170 --> 00:54:28.271 Public Void PrintType 913 00:54:30.410 --> 00:54:32.427 Let's print anything here 914 00:54:34.491 --> 00:54:35.945 Let's print 915 00:54:37.263 --> 00:54:40.513 I just made it for Puppy, 916 00:54:41.091 --> 00:54:41.861 in Dog 917 00:54:42.336 --> 00:54:46.388 So let's just remove all these 918 00:54:47.880 --> 00:54:49.646 and just look at this 919 00:54:54.038 --> 00:54:57.559 Let's have this Dog an object, 920 00:54:58.143 --> 00:55:00.500 now Animal, 921 00:55:05.690 --> 00:55:06.596 like this 922 00:55:07.833 --> 00:55:10.108 When we do type casting, 923 00:55:10.109 --> 00:55:12.500 this is casted implicitly 924 00:55:12.850 --> 00:55:14.821 It can either be done implicitly or explicitly 925 00:55:14.822 --> 00:55:15.967 Explicit typecasting 926 00:55:15.967 --> 00:55:19.606 means we write Animal here 927 00:55:19.811 --> 00:55:23.126 So in C#, when we do type casting, 928 00:55:24.318 --> 00:55:27.659 using parentheses is the old way of doing it 929 00:55:28.091 --> 00:55:33.649 But in C#, we just write it out as this, as Animal 930 00:55:33.650 --> 00:55:35.716 That's when we use as 931 00:55:37.450 --> 00:55:41.654 If we type cast this object as Animal, 932 00:55:42.925 --> 00:55:44.023 we aren't sure 933 00:55:44.024 --> 00:55:47.696 whether it inherited Animal or not 934 00:55:47.697 --> 00:55:51.025 We need to read the code to make sure 935 00:55:51.844 --> 00:55:56.874 When the compiler is performing its tasks, 936 00:55:56.875 --> 00:55:57.875 it needs to verify 937 00:55:58.845 --> 00:56:02.097 and check if the type relationship, 938 00:56:02.098 --> 00:56:04.024 the inheritance hierarchy is valid 939 00:56:04.024 --> 00:56:07.995 If so, it will reduce the object to the Animal type and return it accordingly 940 00:56:08.003 --> 00:56:09.779 If not, 941 00:56:09.780 --> 00:56:13.167 if it is not in inheritance 942 00:56:13.167 --> 00:56:18.518 but still tries to type cast, it will say null 943 00:56:18.838 --> 00:56:21.966 In fact, for classes, 944 00:56:22.674 --> 00:56:24.546 like we made, 945 00:56:25.290 --> 00:56:30.000 we said new to our class, but we can also ad null 946 00:56:32.251 --> 00:56:36.146 Null simply means empty 947 00:56:36.776 --> 00:56:38.988 It has no existence 948 00:56:38.989 --> 00:56:43.760 It has its container, but no memory is assigned 949 00:56:43.760 --> 00:56:46.510 Or no value is assigned 950 00:56:47.091 --> 00:56:51.297 That's why it returns null 951 00:56:51.298 --> 00:56:53.241 If this is not inheritance, 952 00:56:53.590 --> 00:56:55.798 it is better to use the keyword 953 00:56:56.130 --> 00:57:02.194 of as when we type cast it 954 00:57:02.771 --> 00:57:04.725 Let's change it 955 00:57:04.725 --> 00:57:07.294 I changed the object Dog to Animal 956 00:57:07.561 --> 00:57:09.689 Then, in Dog, 957 00:57:10.618 --> 00:57:17.622 there is a function we made, PrintType 958 00:57:17.622 --> 00:57:19.525 Right here 959 00:57:19.765 --> 00:57:25.251 However, I didn’t reassign the dog object to a new value 960 00:57:25.251 --> 00:57:28.709 Instead, I simply type casted the existing dog object to a different type 961 00:57:29.012 --> 00:57:30.462 This process is called casting 962 00:57:30.759 --> 00:57:31.840 We've done the casting 963 00:57:31.840 --> 00:57:37.633 After casting, if I attempt to call the printType function, it won’t work 964 00:57:37.634 --> 00:57:40.192 It doesn't exist 965 00:57:41.189 --> 00:57:42.256 There is no function like that 966 00:57:43.329 --> 00:57:46.979 The reason is that the cast changes the object’s type, 967 00:57:46.980 --> 00:57:49.638 and functions specific to the original type, 968 00:57:49.639 --> 00:57:52.570 like printType, are no longer accessible 969 00:57:52.581 --> 00:57:55.703 That's what polymorphism does 970 00:57:56.578 --> 00:58:00.481 Let’s summarize what we learned in this lesson 971 00:58:00.871 --> 00:58:04.348 Understanding Object-Oriented Programming and Classes OOP: Creating everything as an object and programming through interactions between objects 972 00:58:04.348 --> 00:58:05.443 Classes: user-defined data types with attributes and functions Variables: written in noun, functions: written in verb Method: function Field: variable 973 00:58:05.443 --> 00:58:06.574 Object: Variable created using a class Classes have an inside and outside area Access modifiers: public, private, protected 974 00:58:06.574 --> 00:58:07.828 Local variables inside functions are accessed first 975 00:58:07.828 --> 00:58:09.636 Constructor, Inheritance, Encapsulation, and Polymorphism Constructor: Class: public class Animal Constructor: public Animal() Destructor: ~Animal() 976 00:58:09.636 --> 00:58:12.179 Inheritance Classes form relationships where child classes represent larger, more specific forms than parent classes 977 00:58:12.179 --> 00:58:15.129 Abstraction Simplifies complexity by implementing only the necessary attributes in a class 978 00:58:15.130 --> 00:58:17.237 Encapsulation Prevents access from external classes, often achieved through encapsulation Access modifiers: public, private, protected 979 00:58:17.246 --> 00:58:18.758 Polymorphism Type casting allows objects to change forms through type transformation. 980 00:58:18.758 --> 00:58:20.906 Possible only within inheritance relationships, enabling objects to take on multiple forms Objects can only access those with the right type