Download 13 - Object-oriented programming/005 Inheritance_en.srt from Tan115/java-development-for-beginners-learnit: direct link, hf CLI and curl.
- Browser
- Download file 15.2 kB
-
https://huggingface.co/Tan115/java-development-for-beginners-learnit/resolve/main/13%20-%20Object-oriented%20programming/005%20Inheritance_en.srt
- Command line
-
hf download 'hf://Tan115/java-development-for-beginners-learnit/13 - Object-oriented programming/005 Inheritance_en.srt'
-
curl -L -o '005 Inheritance_en.srt' https://huggingface.co/Tan115/java-development-for-beginners-learnit/resolve/main/13%20-%20Object-oriented%20programming/005%20Inheritance_en.srt
15.2 kB
| 1 | |
| 00:00:05,000 --> 00:00:06,000 | |
| Hello, Jim. | |
| 2 | |
| 00:00:06,000 --> 00:00:11,000 | |
| In this lesson, we are going to learn more about one of the key principles of the object oriented programming | |
| 3 | |
| 00:00:11,000 --> 00:00:12,000 | |
| approach. | |
| 4 | |
| 00:00:12,000 --> 00:00:13,000 | |
| I'm talking about inheritance. | |
| 5 | |
| 00:00:14,000 --> 00:00:16,000 | |
| And they will talk not only about inheritance. | |
| 6 | |
| 00:00:16,000 --> 00:00:23,000 | |
| Well, on super keyword and you operator instance of I will show you how we can extend classes and what | |
| 7 | |
| 00:00:23,000 --> 00:00:25,000 | |
| rules should be followed during inheritance. | |
| 8 | |
| 00:00:25,000 --> 00:00:27,000 | |
| So let's start. | |
| 9 | |
| 00:00:27,000 --> 00:00:32,000 | |
| I believe you remember theoretical part which we discussed in lesson about inheritance. | |
| 10 | |
| 00:00:32,000 --> 00:00:37,000 | |
| So now it is time for practical code examples demo to understand this topic better. | |
| 11 | |
| 00:00:38,000 --> 00:00:42,000 | |
| I always try to create real life example for you so that you could understand how this works for real | |
| 12 | |
| 00:00:43,000 --> 00:00:44,000 | |
| in this lesson. | |
| 13 | |
| 00:00:44,000 --> 00:00:46,000 | |
| Imagine that you have product type in your program. | |
| 14 | |
| 00:00:46,000 --> 00:00:50,000 | |
| I didn't create too many fields to not distract attention from inheritance topic. | |
| 15 | |
| 00:00:51,000 --> 00:00:54,000 | |
| Just imagine that product can have multiple fields and methods. | |
| 16 | |
| 00:00:55,000 --> 00:00:56,000 | |
| And we also have the phone type. | |
| 17 | |
| 00:00:57,000 --> 00:00:59,000 | |
| Phone is also a product in our online shop. | |
| 18 | |
| 00:01:00,000 --> 00:01:03,000 | |
| It will have similar fields and behavior as product has. | |
| 19 | |
| 00:01:03,000 --> 00:01:09,000 | |
| But besides that, it has also some phone specific behavior like ring method, for example. | |
| 20 | |
| 00:01:10,000 --> 00:01:15,000 | |
| Now let's create phone object I a product variable and initialize it with foreign object. | |
| 21 | |
| 00:01:15,000 --> 00:01:22,000 | |
| Take into account for an object is also a type product and you can see that our product variable has | |
| 22 | |
| 00:01:22,000 --> 00:01:24,000 | |
| own methods that are declared in product type. | |
| 23 | |
| 00:01:25,000 --> 00:01:31,000 | |
| I have said gather and calculate remaining amount method which is not declared in phone but declared | |
| 24 | |
| 00:01:31,000 --> 00:01:32,000 | |
| in product. | |
| 25 | |
| 00:01:32,000 --> 00:01:39,000 | |
| So the idea of inheritance is to describe new types based on existing one from technical side. | |
| 26 | |
| 00:01:39,000 --> 00:01:46,000 | |
| This allows us to reuse already written code, which is also pretty cool because let's imagine you need | |
| 27 | |
| 00:01:46,000 --> 00:01:49,000 | |
| to change the logic how remaining amount in stock is calculated. | |
| 28 | |
| 00:01:50,000 --> 00:01:56,000 | |
| Instead of changing this method in all classes where this method is used, I can just do change in one | |
| 29 | |
| 00:01:56,000 --> 00:02:01,000 | |
| place in product class and behaviour will become different in old child classes. | |
| 30 | |
| 00:02:01,000 --> 00:02:07,000 | |
| And you also see that foreign object doesn't have name field, but somehow I can set the value for name | |
| 31 | |
| 00:02:07,000 --> 00:02:09,000 | |
| property of the form. | |
| 32 | |
| 00:02:09,000 --> 00:02:12,000 | |
| That is because inheritance of this field from product object. | |
| 33 | |
| 00:02:13,000 --> 00:02:15,000 | |
| OK, but let's try to ring right now. | |
| 34 | |
| 00:02:16,000 --> 00:02:21,000 | |
| Unfortunately, we have compilation error because Renk method is not defined for the type product, | |
| 35 | |
| 00:02:21,000 --> 00:02:26,000 | |
| but we see here that we created for an object and we still don't have that Renk method available. | |
| 36 | |
| 00:02:27,000 --> 00:02:27,000 | |
| Why? | |
| 37 | |
| 00:02:28,000 --> 00:02:30,000 | |
| Because Java is not mahzarin. | |
| 38 | |
| 00:02:30,000 --> 00:02:33,000 | |
| What object will be here in this line during the runtime? | |
| 39 | |
| 00:02:34,000 --> 00:02:40,000 | |
| You can initialize the variable with any object of this type and Java doesn't want to guess what object | |
| 40 | |
| 00:02:40,000 --> 00:02:41,000 | |
| is there for real. | |
| 41 | |
| 00:02:41,000 --> 00:02:47,000 | |
| What Java knows that in case code is compiler, that means object which was assigned to this variable | |
| 42 | |
| 00:02:47,000 --> 00:02:50,000 | |
| has the same interface as this type for sure. | |
| 43 | |
| 00:02:50,000 --> 00:02:56,000 | |
| That's why we don't see any matters from form type here, because we have variable of type product. | |
| 44 | |
| 00:02:57,000 --> 00:03:03,000 | |
| When I create variable or form that I can invoke Renk method without compilation error. | |
| 45 | |
| 00:03:03,000 --> 00:03:08,000 | |
| But what if my class or method works with product type and I want to call Remarque method? | |
| 46 | |
| 00:03:09,000 --> 00:03:12,000 | |
| I will be able to do that only in case during the runtime. | |
| 47 | |
| 00:03:12,000 --> 00:03:14,000 | |
| I have phone object here. | |
| 48 | |
| 00:03:15,000 --> 00:03:21,000 | |
| But how I can understand this now I will show you a new keyword and operator which you will be able | |
| 49 | |
| 00:03:21,000 --> 00:03:23,000 | |
| to use in your programs. | |
| 50 | |
| 00:03:23,000 --> 00:03:25,000 | |
| Here is an instance of operator. | |
| 51 | |
| 00:03:26,000 --> 00:03:29,000 | |
| It allows you to check the type consistency with the object. | |
| 52 | |
| 00:03:30,000 --> 00:03:33,000 | |
| It is a binary operator which works with two operands. | |
| 53 | |
| 00:03:34,000 --> 00:03:38,000 | |
| Let me show you how you can use it if object behind the product variable. | |
| 54 | |
| 00:03:38,000 --> 00:03:41,000 | |
| This one is instance of phone type. | |
| 55 | |
| 00:03:41,000 --> 00:03:44,000 | |
| The result will be true in terms of returns. | |
| 56 | |
| 00:03:44,000 --> 00:03:51,000 | |
| Is a true or false and if instance of returns true, I can cast this variable to type the remember how | |
| 57 | |
| 00:03:51,000 --> 00:03:53,000 | |
| Wikus primitive types. | |
| 58 | |
| 00:03:53,000 --> 00:03:56,000 | |
| We can just reference types in a similar way. | |
| 59 | |
| 00:03:56,000 --> 00:03:57,000 | |
| Right. | |
| 60 | |
| 00:03:57,000 --> 00:03:59,000 | |
| Phone and variable name. | |
| 61 | |
| 00:03:59,000 --> 00:04:04,000 | |
| Now put the type name in parentheses and name of the variable you want to cast. | |
| 62 | |
| 00:04:05,000 --> 00:04:11,000 | |
| Now you can invoke Renk method instance of the return false in case variable reference to object is | |
| 63 | |
| 00:04:11,000 --> 00:04:15,000 | |
| incompatible with the type you specified as a second operand. | |
| 64 | |
| 00:04:16,000 --> 00:04:20,000 | |
| OK, you know what is instance of and when you can use it. | |
| 65 | |
| 00:04:20,000 --> 00:04:24,000 | |
| Let's proceed with inheritance topic in your child class. | |
| 66 | |
| 00:04:24,000 --> 00:04:26,000 | |
| You always have a reference to a parent class. | |
| 67 | |
| 00:04:27,000 --> 00:04:32,000 | |
| You might want to have the reference to the parent class, for example, to call some parent methods. | |
| 68 | |
| 00:04:32,000 --> 00:04:33,000 | |
| Let's take a look here. | |
| 69 | |
| 00:04:34,000 --> 00:04:40,000 | |
| Product Class has a message that returns the least of all variant products associated with the current | |
| 70 | |
| 00:04:40,000 --> 00:04:40,000 | |
| product. | |
| 71 | |
| 00:04:40,000 --> 00:04:46,000 | |
| And our phone object overrides this behavior and throws and supports the separation exception. | |
| 72 | |
| 00:04:46,000 --> 00:04:51,000 | |
| This exception indicates that this behavior is not supported in this class. | |
| 73 | |
| 00:04:52,000 --> 00:04:57,000 | |
| Later in this course, we will learn that this implementation violates one of the design principles | |
| 74 | |
| 00:04:57,000 --> 00:04:59,000 | |
| in the object oriented programming. | |
| 75 | |
| 00:04:59,000 --> 00:05:02,000 | |
| But you would face with similar cases many in. | |
| 76 | |
| 00:05:02,000 --> 00:05:09,000 | |
| Times, believe me, and now what will happen if in another method calculate the amount of variance | |
| 77 | |
| 00:05:09,000 --> 00:05:17,000 | |
| I will call least variance method of this object will be called and program will stop execution because | |
| 78 | |
| 00:05:17,000 --> 00:05:18,000 | |
| of the exception? | |
| 79 | |
| 00:05:18,000 --> 00:05:20,000 | |
| Let me run this program to show you this. | |
| 80 | |
| 00:05:21,000 --> 00:05:22,000 | |
| What do you have to do right now? | |
| 81 | |
| 00:05:22,000 --> 00:05:27,000 | |
| You already know that in each class you have reference to a parent class who is super keyword. | |
| 82 | |
| 00:05:28,000 --> 00:05:34,000 | |
| So let's call math at least reference not from this class, but let's call method list variants of the | |
| 83 | |
| 00:05:34,000 --> 00:05:34,000 | |
| parent class. | |
| 84 | |
| 00:05:35,000 --> 00:05:38,000 | |
| This one just right super. | |
| 85 | |
| 00:05:38,000 --> 00:05:43,000 | |
| This is a reference to the parent type Dorte and invoke the same method. | |
| 86 | |
| 00:05:43,000 --> 00:05:47,000 | |
| What we have now left me around this program one more time. | |
| 87 | |
| 00:05:47,000 --> 00:05:54,000 | |
| We won't have any error because this method was called you can always use super keyword to reference | |
| 88 | |
| 00:05:54,000 --> 00:05:56,000 | |
| methods or properties of a parent class. | |
| 89 | |
| 00:05:57,000 --> 00:05:59,000 | |
| Now imagine the next scenario. | |
| 90 | |
| 00:05:59,000 --> 00:06:04,000 | |
| Imagine you don't have the full constructor in your parent object because you can create products only | |
| 91 | |
| 00:06:04,000 --> 00:06:05,000 | |
| with names. | |
| 92 | |
| 00:06:06,000 --> 00:06:10,000 | |
| How this will impact your phone that you're going to have compilation error. | |
| 93 | |
| 00:06:10,000 --> 00:06:11,000 | |
| Why? | |
| 94 | |
| 00:06:11,000 --> 00:06:20,000 | |
| Because the grade child object GBM invokes constructor of all parent classes and now you can call constructor | |
| 95 | |
| 00:06:20,000 --> 00:06:23,000 | |
| of parent classes because it doesn't know what should be passed. | |
| 96 | |
| 00:06:23,000 --> 00:06:28,000 | |
| As a constructor argument, the full constructor often looks like this. | |
| 97 | |
| 00:06:29,000 --> 00:06:35,000 | |
| So what we need to do to remove this compilation error, we need to explicitly call constructor of parent | |
| 98 | |
| 00:06:35,000 --> 00:06:42,000 | |
| class and pass a string parameter to it that's type super parentheses and pass some string argument | |
| 99 | |
| 00:06:42,000 --> 00:06:43,000 | |
| to it. | |
| 100 | |
| 00:06:43,000 --> 00:06:48,000 | |
| Here we call parent constructor and you see that now compilation error is gone. | |
| 101 | |
| 00:06:49,000 --> 00:06:56,000 | |
| Now Joram will be able to create an object of the form type one more important common here call of the | |
| 102 | |
| 00:06:56,000 --> 00:06:59,000 | |
| parent constructor always should go first in child constructor. | |
| 103 | |
| 00:07:00,000 --> 00:07:03,000 | |
| I mean you can't call any other methods before super. | |
| 104 | |
| 00:07:03,000 --> 00:07:05,000 | |
| Here, let me show you. | |
| 105 | |
| 00:07:06,000 --> 00:07:10,000 | |
| If I write here some code, it doesn't matter what exactly are right here. | |
| 106 | |
| 00:07:11,000 --> 00:07:11,000 | |
| Let me cancel. | |
| 107 | |
| 00:07:11,000 --> 00:07:14,000 | |
| Output will have again compilation error. | |
| 108 | |
| 00:07:14,000 --> 00:07:20,000 | |
| You need to remember that invocation of Ben constructor and using of super keyword should always go | |
| 109 | |
| 00:07:20,000 --> 00:07:22,000 | |
| first in shall constructor. | |
| 110 | |
| 00:07:22,000 --> 00:07:23,000 | |
| I will make a post now. | |
| 111 | |
| 00:07:24,000 --> 00:07:27,000 | |
| Is it clear if this is clear, let's move on. | |
| 112 | |
| 00:07:28,000 --> 00:07:30,000 | |
| I told you that OP is fun. | |
| 113 | |
| 00:07:31,000 --> 00:07:37,000 | |
| Now you know that to create a child's object constructor of a parent type is called what else happens | |
| 114 | |
| 00:07:37,000 --> 00:07:39,000 | |
| during child object initialization. | |
| 115 | |
| 00:07:39,000 --> 00:07:44,000 | |
| Here is a quick exercise for you that will help you to remember for the rest of your lives what happens | |
| 116 | |
| 00:07:44,000 --> 00:07:46,000 | |
| during child object construction. | |
| 117 | |
| 00:07:47,000 --> 00:07:49,000 | |
| Here is the code of our exercise. | |
| 118 | |
| 00:07:49,000 --> 00:07:50,000 | |
| So let's start. | |
| 119 | |
| 00:07:51,000 --> 00:07:53,000 | |
| We have parent type here. | |
| 120 | |
| 00:07:53,000 --> 00:07:57,000 | |
| You can see static initialization lock installation, lock constructor. | |
| 121 | |
| 00:07:58,000 --> 00:08:03,000 | |
| We have console output everywhere here and it's clearly specified that this output belongs to the parent | |
| 122 | |
| 00:08:03,000 --> 00:08:07,000 | |
| type and we have child types that extends parent class. | |
| 123 | |
| 00:08:08,000 --> 00:08:14,000 | |
| The child class has the same things that parent has static, initialization block, initialization lock | |
| 124 | |
| 00:08:14,000 --> 00:08:15,000 | |
| and constructor. | |
| 125 | |
| 00:08:16,000 --> 00:08:19,000 | |
| And you see the text and console output is different. | |
| 126 | |
| 00:08:19,000 --> 00:08:24,000 | |
| The question is what will be printed to console when I will create child object? | |
| 127 | |
| 00:08:25,000 --> 00:08:30,000 | |
| And another question, what will be printed to console when I will create child object the second time? | |
| 128 | |
| 00:08:31,000 --> 00:08:34,000 | |
| Press pause and try to answer this question. | |
| 129 | |
| 00:08:35,000 --> 00:08:36,000 | |
| OK, let me answer this. | |
| 130 | |
| 00:08:37,000 --> 00:08:42,000 | |
| I will run the program and we will investigate console output and let's see what we have here. | |
| 131 | |
| 00:08:43,000 --> 00:08:49,000 | |
| Static initialization block of the parent is executed and after that static installation lock of the | |
| 132 | |
| 00:08:49,000 --> 00:08:51,000 | |
| child type is executed. | |
| 133 | |
| 00:08:51,000 --> 00:08:59,000 | |
| Why static blocks are executed when classes uploaded into GBM parent class was uploaded first and after | |
| 134 | |
| 00:08:59,000 --> 00:09:05,000 | |
| that child class was uploaded after the JVM wants to initialize parent fields first. | |
| 135 | |
| 00:09:05,000 --> 00:09:08,000 | |
| That's why it executes parent installation block. | |
| 136 | |
| 00:09:08,000 --> 00:09:15,000 | |
| And after that parent constructor and only after that JVM starts to create child object and executes | |
| 137 | |
| 00:09:15,000 --> 00:09:19,000 | |
| child and installation work and child constructor home. | |
| 138 | |
| 00:09:19,000 --> 00:09:20,000 | |
| This example is clear. | |
| 139 | |
| 00:09:20,000 --> 00:09:24,000 | |
| What happens when I try to create child objects a second time? | |
| 140 | |
| 00:09:24,000 --> 00:09:28,000 | |
| Everything the same beside the execution of static initialization locks. | |
| 141 | |
| 00:09:29,000 --> 00:09:29,000 | |
| Why? | |
| 142 | |
| 00:09:30,000 --> 00:09:35,000 | |
| Because everything what is followed by static keyword, including initialization blocks, is associated | |
| 143 | |
| 00:09:35,000 --> 00:09:39,000 | |
| not with the object of this class, but with the class itself. | |
| 144 | |
| 00:09:39,000 --> 00:09:44,000 | |
| So when classes are uploaded into GBM, these blocks are executed. | |
| 145 | |
| 00:09:44,000 --> 00:09:48,000 | |
| They won't be executed two times in a row home. | |
| 146 | |
| 00:09:48,000 --> 00:09:54,000 | |
| Now with this example, it has become clearer what is initialization order during the construction of | |
| 147 | |
| 00:09:54,000 --> 00:09:55,000 | |
| a giant object. | |
| 148 | |
| 00:09:55,000 --> 00:10:01,000 | |
| The last thing you need to know about inheritance is that in Java, multiple inheritance is not supported | |
| 149 | |
| 00:10:01,000 --> 00:10:02,000 | |
| that. | |
| 150 | |
| 00:10:02,000 --> 00:10:08,000 | |
| Means you can't define new type on the basis of two or more opposite types after extensive keyword. | |
| 151 | |
| 00:10:08,000 --> 00:10:14,000 | |
| You can specify only one class, but in some other languages, multiple inheritance is supported. | |
| 152 | |
| 00:10:15,000 --> 00:10:17,000 | |
| That's all what we have in our agenda for today. | |
| 153 | |
| 00:10:18,000 --> 00:10:20,000 | |
| Let's recap what we have learned today. | |
| 154 | |
| 00:10:20,000 --> 00:10:24,000 | |
| We learned what inheritance is and how we can extend the time. | |
| 155 | |
| 00:10:24,000 --> 00:10:31,000 | |
| Now, you know, two more key words, super and instance of you learn some specific constraints while | |
| 156 | |
| 00:10:31,000 --> 00:10:32,000 | |
| extending classes. | |
| 157 | |
| 00:10:33,000 --> 00:10:39,000 | |
| And today you learned what initialization order is of new objects that is extended from another class. | |
| 158 | |
| 00:10:39,000 --> 00:10:40,000 | |
| That's all. | |
| 159 | |
| 00:10:41,000 --> 00:10:44,000 | |
| Thank you for your attention and see you in the next lesson. | |