diff --git a/.gitattributes b/.gitattributes index ba71aa758171f205bbe4e331a38e0b932d548218..ac4385b1a359ff5025de54bc50fdeac8de5784d7 100644 --- a/.gitattributes +++ b/.gitattributes @@ -41,3 +41,15 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text 02[[:space:]]-[[:space:]]Core[[:space:]]Java/005[[:space:]]First[[:space:]]code[[:space:]]Setup.mp4 filter=lfs diff=lfs merge=lfs -text 02[[:space:]]-[[:space:]]Core[[:space:]]Java/009[[:space:]]Literal.mp4 filter=lfs diff=lfs merge=lfs -text 02[[:space:]]-[[:space:]]Core[[:space:]]Java/008[[:space:]]Data[[:space:]]Types.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/006[[:space:]]How[[:space:]]Java[[:space:]]Works.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/007[[:space:]]Variables.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/012[[:space:]]Arithmetic[[:space:]]Operators.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/010[[:space:]]Type[[:space:]]Conversion.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/013[[:space:]]Relational[[:space:]]Operators.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/015[[:space:]]If[[:space:]]Else.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/014[[:space:]]Logical[[:space:]]Operators.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/016[[:space:]]If[[:space:]]Else[[:space:]]If.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/017[[:space:]]Ternary.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/018[[:space:]]Switch[[:space:]]Statement.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/021[[:space:]]Do[[:space:]]While[[:space:]]Loop.mp4 filter=lfs diff=lfs merge=lfs -text +02[[:space:]]-[[:space:]]Core[[:space:]]Java/019[[:space:]]Need[[:space:]]For[[:space:]]Loop.mp4 filter=lfs diff=lfs merge=lfs -text diff --git a/02 - Core Java/006 How Java Works.mp4 b/02 - Core Java/006 How Java Works.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..a90ff2824d51c774a00331d4766267bc3b547171 --- /dev/null +++ b/02 - Core Java/006 How Java Works.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9aa724697127c337f5df4574bb9c27eeade3a68cd54d2dd18a61e86226550fd0 +size 127336724 diff --git a/02 - Core Java/007 Variables.mp4 b/02 - Core Java/007 Variables.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..0da42279899d50d6693526407e9136d01a3cfcce --- /dev/null +++ b/02 - Core Java/007 Variables.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ce88ef29ca96761dda3ea1dcf55857cf07399f447ecf0afd451e2f43adc5a75 +size 218963038 diff --git a/02 - Core Java/010 Type Conversion.mp4 b/02 - Core Java/010 Type Conversion.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..956967ddcf6164c2162ce7d85cc0c86e7830d806 --- /dev/null +++ b/02 - Core Java/010 Type Conversion.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f62562a79f793cea12e6e41ec3e783e27bc252afa283c20bd8717112f358eea +size 94369188 diff --git a/02 - Core Java/012 Arithmetic Operators.mp4 b/02 - Core Java/012 Arithmetic Operators.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..0e095787d6a3c730d183cc466a845b03e52721f6 --- /dev/null +++ b/02 - Core Java/012 Arithmetic Operators.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1371ddb937f01079c957041f72e6f16b0a71d9f43e65ce02f79d5581085d8377 +size 52178681 diff --git a/02 - Core Java/013 Relational Operators.mp4 b/02 - Core Java/013 Relational Operators.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..11afc5317b4772652331c8a71912df638d9eae23 --- /dev/null +++ b/02 - Core Java/013 Relational Operators.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a6e74a3f8afd707291119795eeb4e61e8af605f9375fae1c4de98b448138d90 +size 62286093 diff --git a/02 - Core Java/014 Logical Operators.mp4 b/02 - Core Java/014 Logical Operators.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..84a3aa7e4b96638efd4cc1e6b8d5b6571232b5e5 --- /dev/null +++ b/02 - Core Java/014 Logical Operators.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6e2fdfa710946f18edef5cd7c1240c530e2b2b3eec800431f39e5579d635bf5a +size 158452971 diff --git a/02 - Core Java/015 If Else.mp4 b/02 - Core Java/015 If Else.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..f2eb5a6b32ae62811d13f887e1860f04ffd34f40 --- /dev/null +++ b/02 - Core Java/015 If Else.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64b8a0b2e85fcb76f130337f4f011d1c1bd832d801dfe6120fbba12f58032acf +size 58497706 diff --git a/02 - Core Java/016 If Else If.mp4 b/02 - Core Java/016 If Else If.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..3c41c558dfc5d4aaf1c771827129c8e3165ee9c5 --- /dev/null +++ b/02 - Core Java/016 If Else If.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b310e06d124fe76713875dc998e3b70979bd8b0c4207667891a9be0011f1f97f +size 80802646 diff --git a/02 - Core Java/017 Ternary.mp4 b/02 - Core Java/017 Ternary.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..8128b0d65395a2242554af57dd01501b4f7a9b51 --- /dev/null +++ b/02 - Core Java/017 Ternary.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ad2004a559981fe83482a7ac7cdc349948b80c834e1b47f44781b4a8c04198ac +size 23421354 diff --git a/02 - Core Java/018 Switch Statement.mp4 b/02 - Core Java/018 Switch Statement.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..1482dccb2fae88a475ad19c63c0198bd16e4cba0 --- /dev/null +++ b/02 - Core Java/018 Switch Statement.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:858aed8380f77a1b65531c34634a8cd7ed8346407f5d692ac7c46058a0396ddc +size 131214725 diff --git a/02 - Core Java/019 Need For Loop.mp4 b/02 - Core Java/019 Need For Loop.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..c1744ac6b9f8bbbfd8b62ea106afb254b2b6bd08 --- /dev/null +++ b/02 - Core Java/019 Need For Loop.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:336307e365a5dfd3ca7c999173c1e8044c5623a4c71ee3e69a40c92742a228b4 +size 59499341 diff --git a/02 - Core Java/021 Do While Loop.mp4 b/02 - Core Java/021 Do While Loop.mp4 new file mode 100644 index 0000000000000000000000000000000000000000..e70fde01b5037c033c4f45c9e204e8529d3d36c8 --- /dev/null +++ b/02 - Core Java/021 Do While Loop.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b5bb0c1ccc7da587425f1b1e11bde5e3fbcf019954ba34b126371d30292519df +size 49889788 diff --git a/02 - Core Java/039 Mutable vs Immutable string_en.srt b/02 - Core Java/039 Mutable vs Immutable string_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..7d9bf856379a0f6254cbe8974786f1ba0f0e0189 --- /dev/null +++ b/02 - Core Java/039 Mutable vs Immutable string_en.srt @@ -0,0 +1,688 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle piano music) + +2 +00:00:03,000 --> 00:00:04,000 +-: So in the previous video we have talked + +3 +00:00:04,000 --> 00:00:05,000 +about string, right? + +4 +00:00:05,000 --> 00:00:08,000 +Now we talk about this string, + +5 +00:00:08,000 --> 00:00:10,000 +what might be happening behind the scene. + +6 +00:00:10,000 --> 00:00:11,000 +Example, let's say if you store this navin, + +7 +00:00:11,000 --> 00:00:15,000 +of course it'll be getting stored in the heap memory. + +8 +00:00:15,000 --> 00:00:16,000 +But now what I wanna do is + +9 +00:00:16,000 --> 00:00:17,000 +before printing navin here, + +10 +00:00:17,000 --> 00:00:19,000 +let's say I want to append something. + +11 +00:00:19,000 --> 00:00:22,000 +So I would say name is equal to, + +12 +00:00:22,000 --> 00:00:25,000 +I want to add my name with, let's say reddy here. + +13 +00:00:25,000 --> 00:00:26,000 +I'm giving a space + +14 +00:00:26,000 --> 00:00:28,000 +so that we can differentiate these two words. + +15 +00:00:28,000 --> 00:00:30,000 +So of course now when I say name, + +16 +00:00:30,000 --> 00:00:32,000 +it'll be Navin Reddy, right? + +17 +00:00:32,000 --> 00:00:34,000 +So basically we are trying to change the value. + +18 +00:00:34,000 --> 00:00:36,000 +Now if we talk about integer, float, double. + +19 +00:00:36,000 --> 00:00:38,000 +Basically you can change values of it, right? + +20 +00:00:38,000 --> 00:00:39,000 +And it actually makes sense. + +21 +00:00:39,000 --> 00:00:42,000 +That's why we call them as variables. + +22 +00:00:42,000 --> 00:00:43,000 +And here also, we are trying to do that. + +23 +00:00:43,000 --> 00:00:46,000 +But first of all, let's check if this works. + +24 +00:00:46,000 --> 00:00:50,000 +So if I go back and compile and run, it works. + +25 +00:00:50,000 --> 00:00:51,000 +You can see that the new data + +26 +00:00:51,000 --> 00:00:52,000 +for the navin is navin reddy, right? + +27 +00:00:52,000 --> 00:00:54,000 +And that works. + +28 +00:00:54,000 --> 00:00:56,000 +But what if I say that's not the case? + +29 +00:00:56,000 --> 00:00:59,000 +You're not changing the existing string. + +30 +00:00:59,000 --> 00:01:00,000 +Let me repeat. + +31 +00:01:00,000 --> 00:01:04,000 +You are not changing from navin to navin reddy? + +32 +00:01:04,000 --> 00:01:07,000 +So if you're thinking there will be a variable here + +33 +00:01:07,000 --> 00:01:10,000 +where you have Navin, you're making it Navin Reddy. + +34 +00:01:10,000 --> 00:01:12,000 +No, that's not the case. + +35 +00:01:12,000 --> 00:01:14,000 +That's not how string works. + +36 +00:01:14,000 --> 00:01:15,000 +Okay, so what happens? + +37 +00:01:15,000 --> 00:01:18,000 +So what I will do is I'll just remove this part now. + +38 +00:01:18,000 --> 00:01:20,000 +So what is happening, basically, + +39 +00:01:20,000 --> 00:01:23,000 +you know, since strings are used heavily in Java, + +40 +00:01:23,000 --> 00:01:26,000 +now most of the time you try to reuse the strings, + +41 +00:01:26,000 --> 00:01:28,000 +the same string, you'll be using multiple times. + +42 +00:01:28,000 --> 00:01:31,000 +Example, let's say, before I show you something, + +43 +00:01:31,000 --> 00:01:33,000 +let's say, I create a string here, + +44 +00:01:33,000 --> 00:01:37,000 +which is s1 is equal to Navin. + +45 +00:01:37,000 --> 00:01:38,000 +And you can see we have a capital N this time. + +46 +00:01:38,000 --> 00:01:41,000 +So this two data are actually different, okay? + +47 +00:01:41,000 --> 00:01:44,000 +So I will say string s2 is equal to, + +48 +00:01:44,000 --> 00:01:45,000 +and I will say Navin again. + +49 +00:01:45,000 --> 00:01:47,000 +Now, what do you think? + +50 +00:01:47,000 --> 00:01:50,000 +How many objects we are creating behind the scene? + +51 +00:01:50,000 --> 00:01:51,000 +Think about this. + +52 +00:01:51,000 --> 00:01:51,000 +Okay? + +53 +00:01:51,000 --> 00:01:53,000 +So if you're thinking, we got two objects, + +54 +00:01:53,000 --> 00:01:57,000 +actually not, we got two references. + +55 +00:01:57,000 --> 00:01:58,000 +We got s1, s2. + +56 +00:01:58,000 --> 00:02:02,000 +Of course, in your stack, you'll be having s1, s2 here, + +57 +00:02:02,000 --> 00:02:06,000 +but in total, you got only one object. + +58 +00:02:08,000 --> 00:02:09,000 +And that sounds tricky here, right? + +59 +00:02:09,000 --> 00:02:11,000 +And let me prove that point to you. + +60 +00:02:11,000 --> 00:02:13,000 +See, what I want to prove is the value + +61 +00:02:13,000 --> 00:02:15,000 +for this field and this field will be equal. + +62 +00:02:15,000 --> 00:02:17,000 +The way the I can do that is by saying S out, + +63 +00:02:17,000 --> 00:02:22,000 +s1 is equal to equal to s2. + +64 +00:02:22,000 --> 00:02:25,000 +If this is true, then they are same. + +65 +00:02:25,000 --> 00:02:27,000 +So if I run, you can see we got true. + +66 +00:02:27,000 --> 00:02:31,000 +That means the value for the addresses are same. + +67 +00:02:31,000 --> 00:02:33,000 +In total we got only one object. + +68 +00:02:33,000 --> 00:02:37,000 +So here what happens is we have a special area + +69 +00:02:37,000 --> 00:02:39,000 +inside your heap memory. + +70 +00:02:39,000 --> 00:02:42,000 +So this is your heap, that's your JVM, + +71 +00:02:42,000 --> 00:02:44,000 +and that's your stack. + +72 +00:02:44,000 --> 00:02:45,000 +So inside your heap, + +73 +00:02:45,000 --> 00:02:50,000 +you'll be having a special area called string constant pool. + +74 +00:02:51,000 --> 00:02:54,000 +And by the name itself you can see it says constant. + +75 +00:02:54,000 --> 00:02:55,000 +That's right. + +76 +00:02:55,000 --> 00:02:58,000 +Every string which you create, which you use in Java, + +77 +00:02:58,000 --> 00:03:01,000 +which is a string literal is actually a constant. + +78 +00:03:01,000 --> 00:03:01,000 +You can't change it. + +79 +00:03:01,000 --> 00:03:04,000 +Example, if I write something like navin here, + +80 +00:03:04,000 --> 00:03:05,000 +I cannot change it. + +81 +00:03:05,000 --> 00:03:06,000 +It's done. + +82 +00:03:06,000 --> 00:03:08,000 +It's navin everywhere, okay? + +83 +00:03:08,000 --> 00:03:10,000 +So you can't change the data. + +84 +00:03:10,000 --> 00:03:12,000 +So example, what happens is every time you try + +85 +00:03:12,000 --> 00:03:14,000 +to use a variable here, example, + +86 +00:03:14,000 --> 00:03:16,000 +if you execute the line number eight, it checks, + +87 +00:03:16,000 --> 00:03:18,000 +okay, we need navin variable. + +88 +00:03:18,000 --> 00:03:21,000 +Do we already have navin in the string constant pool? + +89 +00:03:21,000 --> 00:03:22,000 +No, we don't have it. + +90 +00:03:22,000 --> 00:03:23,000 +So let's create navin here. + +91 +00:03:23,000 --> 00:03:25,000 +So it'll create a string variable, navin, + +92 +00:03:25,000 --> 00:03:27,000 +it'll have some address, let's say 101, + +93 +00:03:27,000 --> 00:03:31,000 +and then the name variable here will get this address. + +94 +00:03:31,000 --> 00:03:33,000 +So this name variable is actually referring + +95 +00:03:33,000 --> 00:03:36,000 +to this particular address, okay? + +96 +00:03:36,000 --> 00:03:38,000 +Then it goes for the next statement. + +97 +00:03:38,000 --> 00:03:39,000 +Let's execute this part. + +98 +00:03:39,000 --> 00:03:41,000 +Let's ignore this line as of now, + +99 +00:03:41,000 --> 00:03:42,000 +let's focus on line number 12 and 13. + +100 +00:03:42,000 --> 00:03:45,000 +So line number 12 says, s1 is equal to Navin. + +101 +00:03:45,000 --> 00:03:46,000 +Okay, so it checks, + +102 +00:03:46,000 --> 00:03:49,000 +do we have navin with capital N in the string pool? + +103 +00:03:49,000 --> 00:03:50,000 +No, we don't have it. + +104 +00:03:50,000 --> 00:03:52,000 +So it'll create Navin here. + +105 +00:03:52,000 --> 00:03:54,000 +And let's say the address is 103. + +106 +00:03:54,000 --> 00:03:57,000 +So it'll store 103 here. + +107 +00:03:57,000 --> 00:03:58,000 +Now that makes sense, right? + +108 +00:03:58,000 --> 00:04:00,000 +And that's why you're referring to that particular location. + +109 +00:04:00,000 --> 00:04:02,000 +Now, what happens on line number 13? + +110 +00:04:02,000 --> 00:04:05,000 +Line number 13 says string s2 is equal to Navin. + +111 +00:04:05,000 --> 00:04:07,000 +It'll go to string constant pool again + +112 +00:04:07,000 --> 00:04:09,000 +and check, hey, do we have Navin with capital N? + +113 +00:04:09,000 --> 00:04:10,000 +Yes, we do have. + +114 +00:04:10,000 --> 00:04:13,000 +Now, it'll not create a new object for you, + +115 +00:04:13,000 --> 00:04:16,000 +it'll simply use this address 103. + +116 +00:04:16,000 --> 00:04:17,000 +Okay? + +117 +00:04:17,000 --> 00:04:18,000 +And that's the beauty. + +118 +00:04:18,000 --> 00:04:20,000 +That's how you're trying to save the memory. + +119 +00:04:20,000 --> 00:04:23,000 +Otherwise you could have created multiple variables + +120 +00:04:23,000 --> 00:04:24,000 +with the same data. + +121 +00:04:24,000 --> 00:04:25,000 +So it doesn't make sense, right? + +122 +00:04:25,000 --> 00:04:27,000 +And that's why it uses a string constant pool. + +123 +00:04:27,000 --> 00:04:30,000 +Now, my question to you is, what is happening here? + +124 +00:04:30,000 --> 00:04:33,000 +Now when I say you can't change the data, + +125 +00:04:33,000 --> 00:04:36,000 +that means you cannot simply update this particular + +126 +00:04:36,000 --> 00:04:37,000 +navin by Navin Reddy? + +127 +00:04:37,000 --> 00:04:40,000 +No, that's not happening, navin is still there. + +128 +00:04:40,000 --> 00:04:42,000 +It's a constant. You can't change this. + +129 +00:04:42,000 --> 00:04:43,000 +Now on line number 9, + +130 +00:04:43,000 --> 00:04:47,000 +what is happening is it is creating a new object + +131 +00:04:47,000 --> 00:04:51,000 +which will have a data which is navin reddy, + +132 +00:04:51,000 --> 00:04:53,000 +let's say it has the address, which is 105. + +133 +00:04:53,000 --> 00:04:54,000 +So on line number 9, + +134 +00:04:54,000 --> 00:04:58,000 +basically what you're doing is you are changing this address + +135 +00:04:58,000 --> 00:05:01,000 +from 101 to 105. + +136 +00:05:01,000 --> 00:05:04,000 +You are just changing the address. + +137 +00:05:04,000 --> 00:05:06,000 +You're not changing the actual data. + +138 +00:05:06,000 --> 00:05:09,000 +You are basically creating a new object. + +139 +00:05:09,000 --> 00:05:09,000 +Okay? + +140 +00:05:09,000 --> 00:05:12,000 +Now, you might be thinking what will happen to navin here? + +141 +00:05:12,000 --> 00:05:13,000 +The thing is, + +142 +00:05:13,000 --> 00:05:16,000 +now this particular object is eligible + +143 +00:05:16,000 --> 00:05:17,000 +for garbage collection. + +144 +00:05:17,000 --> 00:05:19,000 +It'll be removed after some time, + +145 +00:05:19,000 --> 00:05:21,000 +just to get some memory back. + +146 +00:05:21,000 --> 00:05:22,000 +Okay? + +147 +00:05:22,000 --> 00:05:24,000 +So point to remember is once you create this object, + +148 +00:05:24,000 --> 00:05:25,000 +you can't change it. + +149 +00:05:25,000 --> 00:05:28,000 +And that's where we have some terms to remember. + +150 +00:05:28,000 --> 00:05:29,000 +And the terms are, + +151 +00:05:29,000 --> 00:05:33,000 +so there's a concept of mutable strings + +152 +00:05:33,000 --> 00:05:35,000 +and immutable string. + +153 +00:05:35,000 --> 00:05:36,000 +Now, what is mutable means? + +154 +00:05:36,000 --> 00:05:40,000 +Mutable means change something which can be changed. + +155 +00:05:40,000 --> 00:05:43,000 +And immutable means unchanged or you cannot change it. + +156 +00:05:43,000 --> 00:05:46,000 +So basically, by default strings are immutable. + +157 +00:05:46,000 --> 00:05:48,000 +Once you create the object, you can't change it. + +158 +00:05:48,000 --> 00:05:49,000 +And that's very important remember, + +159 +00:05:49,000 --> 00:05:51,000 +it's immutable string, okay? + +160 +00:05:51,000 --> 00:05:53,000 +So whatever data you're creating here is immutable. + +161 +00:05:53,000 --> 00:05:55,000 +But what if you want mutable string? + +162 +00:05:55,000 --> 00:05:58,000 +And that's why we have two different classes in Java to use. + +163 +00:05:58,000 --> 00:06:00,000 +Instead of using string, + +164 +00:06:00,000 --> 00:06:04,000 +you can use something called a string buffer + +165 +00:06:04,000 --> 00:06:07,000 +or a string builder. + +166 +00:06:07,000 --> 00:06:09,000 +Now, both are very similar with one difference + +167 +00:06:09,000 --> 00:06:10,000 +that defense we'll discuss later, + +168 +00:06:10,000 --> 00:06:14,000 +but they both are similar and they both provide you a way + +169 +00:06:14,000 --> 00:06:18,000 +to implement a immutable string. + +170 +00:06:18,000 --> 00:06:18,000 +Okay? + +171 +00:06:18,000 --> 00:06:19,000 +Now, how do we do that? + +172 +00:06:19,000 --> 00:06:22,000 +That we'll see in the upcoming videos. + diff --git a/02 - Core Java/039 Source-code.url b/02 - Core Java/039 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/039 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/040 Source-code.url b/02 - Core Java/040 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/040 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/040 StringBuffer and StringBuilder_en.srt b/02 - Core Java/040 StringBuffer and StringBuilder_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..1619b150d420becfe1f32c865659dc5526c7d438 --- /dev/null +++ b/02 - Core Java/040 StringBuffer and StringBuilder_en.srt @@ -0,0 +1,620 @@ +1 +00:00:00,000 --> 00:00:03,000 +(soft chimes) + +2 +00:00:03,000 --> 00:00:05,000 +-: In this video, we'll work with string buffer. + +3 +00:00:05,000 --> 00:00:07,000 +So, what we have is we have a string anvil + +4 +00:00:07,000 --> 00:00:09,000 +and we have work with it, right? + +5 +00:00:09,000 --> 00:00:11,000 +So I'll do is remove everything + +6 +00:00:11,000 --> 00:00:13,000 +and let's work with string buffer. + +7 +00:00:13,000 --> 00:00:15,000 +So I'll say string buffer + +8 +00:00:15,000 --> 00:00:16,000 +and lets create a reference call it + +9 +00:00:16,000 --> 00:00:19,000 +equal to new... string buffer + +10 +00:00:19,000 --> 00:00:21,000 +so when we talk about string buffer + +11 +00:00:21,000 --> 00:00:22,000 +of course in this string buffer, you will have a string + +12 +00:00:22,000 --> 00:00:25,000 +and if which is actually mutable + +13 +00:00:25,000 --> 00:00:26,000 +you can change it... + +14 +00:00:26,000 --> 00:00:27,000 +now to make it more efficient + +15 +00:00:27,000 --> 00:00:28,000 +you know what string buffer does? + +16 +00:00:28,000 --> 00:00:31,000 +String buffer will give you a "buffer size".. okay? + +17 +00:00:31,000 --> 00:00:33,000 +Which is of 16 bytes + +18 +00:00:33,000 --> 00:00:34,000 +so I can. I can just try to print that first + +19 +00:00:34,000 --> 00:00:36,000 +I will say sb. 'dot' + +20 +00:00:36,000 --> 00:00:39,000 +so we can know the capacity of the string buffer + +21 +00:00:39,000 --> 00:00:41,000 +even if its empty, it will give a capacity + +22 +00:00:41,000 --> 00:00:43,000 +of 16 characters and you got it there . + +23 +00:00:43,000 --> 00:00:46,000 +In fact, you can also provide some initial string here,.. + +24 +00:00:46,000 --> 00:00:48,000 +So let's say.. uhh... if I say + +25 +00:00:48,000 --> 00:00:48,000 +"Navin" here + +26 +00:00:48,000 --> 00:00:51,000 +now Navin is a 5 charters right? This is how + +27 +00:00:51,000 --> 00:00:53,000 +you basically create a string buffer.. in bracket + +28 +00:00:53,000 --> 00:00:54,000 +you will mention the string + +29 +00:00:54,000 --> 00:00:56,000 +so, it will create a string buffer object + +30 +00:00:56,000 --> 00:00:59,000 +and the beauty is you can change this data. + +31 +00:00:59,000 --> 00:01:02,000 +Okay.. now the thing is..uhh.. if you talk about + +32 +00:01:02,000 --> 00:01:03,000 +capacity what it will be?.. + +33 +00:01:03,000 --> 00:01:06,000 +So capacity is..it should be still 16 right? + +34 +00:01:06,000 --> 00:01:07,000 +No. It actually changes + +35 +00:01:07,000 --> 00:01:10,000 +if you can see.. the capacity will become 21. + +36 +00:01:10,000 --> 00:01:12,000 +It's because.. it will store my name and it + +37 +00:01:12,000 --> 00:01:16,000 +will also give me a 16 space extra. + +38 +00:01:16,000 --> 00:01:18,000 +Now why it gives me that space is because.. + +39 +00:01:18,000 --> 00:01:21,000 +Every time we'll change data, and if it consumes + +40 +00:01:21,000 --> 00:01:24,000 +a continuous memory location inside your heap memory + +41 +00:01:24,000 --> 00:01:26,000 +and what if there's no continuous location next + +42 +00:01:26,000 --> 00:01:28,000 +(fumbles).. I mean Th.. what if there is no location + +43 +00:01:28,000 --> 00:01:30,000 +available. There's no space available. + +44 +00:01:30,000 --> 00:01:32,000 +In that case, it has to relocate. So to + +45 +00:01:32,000 --> 00:01:34,000 +reduce the relo.. relocation of the string + +46 +00:01:34,000 --> 00:01:37,000 +it gives you a buffer. Okay. + +47 +00:01:37,000 --> 00:01:40,000 +So that's the capacity and now we know how it works. + +48 +00:01:40,000 --> 00:01:42,000 +We can also print the length of the string and we can + +49 +00:01:42,000 --> 00:01:45,000 +say... sb.length. And length is available in + +50 +00:01:45,000 --> 00:01:48,000 +normal string as well. So if I compile + +51 +00:01:48,000 --> 00:01:50,000 +and run, you can see the length is 5 + +52 +00:01:50,000 --> 00:01:52,000 +so, length and capacities are 2 different things. + +53 +00:01:52,000 --> 00:01:56,000 +Okay, now can I append data? + +54 +00:01:56,000 --> 00:01:58,000 +See, the ultimate thing was.. can I change + +55 +00:01:58,000 --> 00:02:00,000 +my data, which is "Navin" in this case. + +56 +00:02:00,000 --> 00:02:03,000 +The beauty is string buffer will give you + +57 +00:02:03,000 --> 00:02:05,000 +a lot of different methods to do that.. + +58 +00:02:05,000 --> 00:02:06,000 +You can see we have a metho...method called + +59 +00:02:06,000 --> 00:02:08,000 +append.. so you can use append + +60 +00:02:08,000 --> 00:02:10,000 +and you can append a data which you want to.. + +61 +00:02:10,000 --> 00:02:13,000 +So lets say I want to append.. "Reddy" here, + +62 +00:02:13,000 --> 00:02:14,000 +and of course I want to give a space + +63 +00:02:14,000 --> 00:02:17,000 +between "Navin" uhh.. "Navin" and "Reddy". + +64 +00:02:17,000 --> 00:02:19,000 +So you can append and you can print the data... + +65 +00:02:19,000 --> 00:02:21,000 +So you can print 'sb' as it is.. + +66 +00:02:21,000 --> 00:02:23,000 +Let's.. uh. First we have to compile the + +67 +00:02:23,000 --> 00:02:25,000 +code.. Run. And you can see the code + +68 +00:02:25,000 --> 00:02:27,000 +"Navin" "Reddy" it wo.. that works. + +69 +00:02:27,000 --> 00:02:30,000 +Next, what if you want to assign string buffer + +70 +00:02:30,000 --> 00:02:32,000 +data to string is example if I do that..uh, + +71 +00:02:32,000 --> 00:02:35,000 +you can see.. it will... give you a log. + +72 +00:02:35,000 --> 00:02:37,000 +It says, type mismatch: cannot convert from + +73 +00:02:37,000 --> 00:02:39,000 +string buffer to String.. oo of course + +74 +00:02:39,000 --> 00:02:42,000 +right? HTI is of type string, so in that case + +75 +00:02:42,000 --> 00:02:45,000 +you can get the string back.. do we have + +76 +00:02:45,000 --> 00:02:46,000 +a method for it? + +77 +00:02:46,000 --> 00:02:48,000 +Lets see...yes we do have two string + +78 +00:02:48,000 --> 00:02:49,000 +we can use that + +79 +00:02:49,000 --> 00:02:52,000 +I think that's the only method we can use... + +80 +00:02:52,000 --> 00:02:53,000 +Yup, so let's see those two string. + +81 +00:02:53,000 --> 00:02:56,000 +So, basically you can use two string to convert + +82 +00:02:56,000 --> 00:02:58,000 +the.. . string buffer data. I mean it will + +83 +00:02:58,000 --> 00:03:01,000 +get the string format, it was stored in HTR + +84 +00:03:01,000 --> 00:03:03,000 +and you can print it. + +85 +00:03:03,000 --> 00:03:05,000 +Okay. We can use that... what else we can do + +86 +00:03:05,000 --> 00:03:06,000 +with string buffer? + +87 +00:03:06,000 --> 00:03:09,000 +If I come back here if I say sb'dot', + +88 +00:03:09,000 --> 00:03:11,000 +you can see we have so many methods here. + +89 +00:03:11,000 --> 00:03:13,000 +Okay, so we have append.. we have chars, it will give you + +90 +00:03:13,000 --> 00:03:17,000 +a character... we have done that + +91 +00:03:17,000 --> 00:03:19,000 +okay, so we can also delete something, we can + +92 +00:03:19,000 --> 00:03:20,000 +delete a character at an index. So next + +93 +00:03:20,000 --> 00:03:23,000 +I will delete a character at location "2". + +94 +00:03:23,000 --> 00:03:27,000 +If you try to delete that data..compile and.. run. + +95 +00:03:27,000 --> 00:03:29,000 +You can see we got, oh! Something is missing + +96 +00:03:29,000 --> 00:03:30,000 +the way is missing there. + +97 +00:03:30,000 --> 00:03:33,000 +Because that is at index number "2" + +98 +00:03:33,000 --> 00:03:35,000 +okay, so the idea is we basically we can change it + +99 +00:03:35,000 --> 00:03:37,000 +and you can experiment it a lot. + +100 +00:03:37,000 --> 00:03:41,000 +You can try to use different methods, we can delete, + +101 +00:03:41,000 --> 00:03:43,000 +we can insert also in between, lets say + +102 +00:03:43,000 --> 00:03:46,000 +I want to insert .. uh. Java at the start, + +103 +00:03:46,000 --> 00:03:48,000 +you can do that. So you can say insert + +104 +00:03:48,000 --> 00:03:49,000 +you can mention the index value where you want + +105 +00:03:49,000 --> 00:03:52,000 +to insert exact location, and the data that you + +106 +00:03:52,000 --> 00:03:54,000 +..which you want to insert. So I will say + +107 +00:03:54,000 --> 00:03:56,000 +Java 'space', now what it will do is + +108 +00:03:56,000 --> 00:04:00,000 +It will add Java at index 0, I mean that is start + +109 +00:04:00,000 --> 00:04:02,000 +itself. So if I run, you can see it says Java.. + +110 +00:04:02,000 --> 00:04:05,000 +Of course you can add Java in between as well. + +111 +00:04:05,000 --> 00:04:08,000 +Uh.. lets say I want to add.. after... + +112 +00:04:08,000 --> 00:04:10,000 +5th location I think... that should work. + +113 +00:04:10,000 --> 00:04:13,000 +After this space it's 6, so let's try that.. + +114 +00:04:13,000 --> 00:04:15,000 +Compile Run. You can see its.. "Navin" + +115 +00:04:15,000 --> 00:04:17,000 +"Java", "Reddy" so it works. + +116 +00:04:17,000 --> 00:04:20,000 +Okay.. so, yeah that's how we can basically use insert + +117 +00:04:20,000 --> 00:04:22,000 +we have more methods to explore and I would + +118 +00:04:22,000 --> 00:04:24,000 +recommend you to do that. Can we just see + +119 +00:04:24,000 --> 00:04:26,000 +one more.. We can also get the substring.. + +120 +00:04:26,000 --> 00:04:27,000 +Let's say if you have a very big string + +121 +00:04:27,000 --> 00:04:30,000 +you can specify.. the starting point, ending point + +122 +00:04:30,000 --> 00:04:33,000 +using substring. You can set the length as well. + +123 +00:04:33,000 --> 00:04:36,000 +So If you set the length of the stream, let's + +124 +00:04:36,000 --> 00:04:38,000 +say I want to set the length of "30". + +125 +00:04:38,000 --> 00:04:40,000 +So, if you want to add your data and + +126 +00:04:40,000 --> 00:04:42,000 +if you want to have a specific length.. + +127 +00:04:42,000 --> 00:04:43,000 +Which is 30 here. + +128 +00:04:43,000 --> 00:04:44,000 +It will save the data which is whatever + +129 +00:04:44,000 --> 00:04:46,000 +you have. "Navin" 20 will be stored and + +130 +00:04:46,000 --> 00:04:47,000 +all the other values will be... will be + +131 +00:04:47,000 --> 00:04:49,000 +null.. let's see. So after "Navin" Reddy" it will + +132 +00:04:49,000 --> 00:04:52,000 +add some extra data here... okay. + +133 +00:04:52,000 --> 00:04:54,000 +So that you can replace that later. Cool.. we + +134 +00:04:54,000 --> 00:04:57,000 +can do that... we can also set a minimum or + +135 +00:04:57,000 --> 00:04:59,000 +maximum data. I think there's minimum capacity + +136 +00:04:59,000 --> 00:05:02,000 +method which we can use... I think the method + +137 +00:05:02,000 --> 00:05:05,000 +name is "ensurecapacity", yeah okay. + +138 +00:05:05,000 --> 00:05:08,000 +So we can, we can set the ensurecapacity to 100 + +139 +00:05:08,000 --> 00:05:11,000 +so it..minimum, minimum capacity will be 100. + +140 +00:05:11,000 --> 00:05:13,000 +Okay um that's how you have different methods. + +141 +00:05:13,000 --> 00:05:15,000 +Now what's the difference between string buffer and + +142 +00:05:15,000 --> 00:05:17,000 +string builder. So as I mentioned we also have + +143 +00:05:17,000 --> 00:05:18,000 +string builder right?..there's only one + +144 +00:05:18,000 --> 00:05:22,000 +difference, string buffer is thread safe and + +145 +00:05:22,000 --> 00:05:23,000 +string builder is not.. + +146 +00:05:23,000 --> 00:05:25,000 +Now what is thread safe means that we + +147 +00:05:25,000 --> 00:05:26,000 +will understand once we talk about + +148 +00:05:26,000 --> 00:05:29,000 +thread concept.. so that point also .. + +149 +00:05:29,000 --> 00:05:31,000 +we'll talk about difference between string buffer + +150 +00:05:31,000 --> 00:05:33,000 +and string builder, but what other methods + +151 +00:05:33,000 --> 00:05:35,000 +you have in string buffer is almost + +152 +00:05:35,000 --> 00:05:37,000 +the same as string builder. So if you + +153 +00:05:37,000 --> 00:05:39,000 +know one.. you know the other one as well. + +154 +00:05:39,000 --> 00:05:41,000 +Okay, so that's about string buffer and... + +155 +00:05:41,000 --> 00:05:43,000 +Some of the methods of string. + diff --git a/02 - Core Java/041 Source-code.url b/02 - Core Java/041 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/041 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/041 Static Variable_en.srt b/02 - Core Java/041 Static Variable_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..bb8aa875755fd0b956614af528b1ac5214af9d74 --- /dev/null +++ b/02 - Core Java/041 Static Variable_en.srt @@ -0,0 +1,700 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, we'll talk about static keyword. + +3 +00:00:06,000 --> 00:00:08,000 +From the start itself, when we have written the first code, + +4 +00:00:08,000 --> 00:00:10,000 +we are looking at the static keyword, + +5 +00:00:10,000 --> 00:00:12,000 +and the question still remains, right, + +6 +00:00:12,000 --> 00:00:14,000 +what is the static? + +7 +00:00:14,000 --> 00:00:15,000 +So we know what is method is, + +8 +00:00:15,000 --> 00:00:17,000 +we know what is return type, we know what is class. + +9 +00:00:17,000 --> 00:00:21,000 +But again, problem gets stuck here in static itself. + +10 +00:00:21,000 --> 00:00:23,000 +Now to make you understand this, let's take an example here. + +11 +00:00:23,000 --> 00:00:26,000 +So let's say we have an example of mobile phone, + +12 +00:00:26,000 --> 00:00:28,000 +and every mobile phone has a brand. + +13 +00:00:28,000 --> 00:00:31,000 +So brand will be string, so it's String brand, + +14 +00:00:32,000 --> 00:00:34,000 +and we can have a default value for it. + +15 +00:00:34,000 --> 00:00:37,000 +We can have int, which is price, + +16 +00:00:37,000 --> 00:00:42,000 +and then String network, okay? + +17 +00:00:43,000 --> 00:00:46,000 +And we can also make it for a specific brand as well, + +18 +00:00:46,000 --> 00:00:47,000 +but let's keep it simple. + +19 +00:00:47,000 --> 00:00:49,000 +Let's have a mobile phone, different brands, + +20 +00:00:49,000 --> 00:00:53,000 +different price, different network, and we'll say name. + +21 +00:00:53,000 --> 00:00:56,000 +Okay, now see name itself of a mobile phone changes, right? + +22 +00:00:56,000 --> 00:01:00,000 +So we used to say normal phones, then we got smartphones, + +23 +00:01:00,000 --> 00:01:02,000 +and now we have foldable phones, right? + +24 +00:01:02,000 --> 00:01:04,000 +So we can have a different name to phones nowadays. + +25 +00:01:04,000 --> 00:01:06,000 +Every phone is called a smartphone now + +26 +00:01:06,000 --> 00:01:08,000 +because that's what we call it. + +27 +00:01:08,000 --> 00:01:09,000 +Usually, we used to call it mobile, + +28 +00:01:09,000 --> 00:01:11,000 +then we used to call it phone, right? + +29 +00:01:11,000 --> 00:01:13,000 +So we have a name here as well. + +30 +00:01:13,000 --> 00:01:16,000 +What do you call a mobile phone? So now it's a smartphone. + +31 +00:01:16,000 --> 00:01:18,000 +Okay, so if you want to work with this mobile phone, + +32 +00:01:18,000 --> 00:01:19,000 +of course, you have to create an object, right? + +33 +00:01:19,000 --> 00:01:21,000 +So if I say Mobile obj, + +34 +00:01:21,000 --> 00:01:24,000 +if I will say Mobile obj1 = new Mobile. + +35 +00:01:24,000 --> 00:01:26,000 +And of course, every mobile will have + +36 +00:01:26,000 --> 00:01:27,000 +a different parameter here. + +37 +00:01:27,000 --> 00:01:29,000 +What I will do is I will just remove this network just + +38 +00:01:29,000 --> 00:01:32,000 +to keep it simple, we just want three variables here, + +39 +00:01:32,000 --> 00:01:35,000 +obj1.brand, I want to set the brand here. + +40 +00:01:35,000 --> 00:01:37,000 +Let's say the brand is Apple + +41 +00:01:37,000 --> 00:01:39,000 +since I'm using an Apple phone here. + +42 +00:01:39,000 --> 00:01:44,000 +And then the price, let's say $1,500. + +43 +00:01:44,000 --> 00:01:46,000 +And what else we have? We have a name. + +44 +00:01:46,000 --> 00:01:47,000 +So what do you call a mobile phone? + +45 +00:01:47,000 --> 00:01:50,000 +So currently, if we talk about Apple phones + +46 +00:01:50,000 --> 00:01:54,000 +or any phones nowadays, we call them as a smartphone, okay? + +47 +00:01:54,000 --> 00:01:55,000 +I know this variable doesn't make sense, + +48 +00:01:55,000 --> 00:01:57,000 +but just for the example, + +49 +00:01:57,000 --> 00:02:00,000 +I'm saying name = "SmartPhone", right? + +50 +00:02:00,000 --> 00:02:03,000 +Now if you talk about this phone here, + +51 +00:02:03,000 --> 00:02:05,000 +we have all these values, right? + +52 +00:02:05,000 --> 00:02:09,000 +Now of course, if I create multiple objects, + +53 +00:02:09,000 --> 00:02:10,000 +it should have a different value. + +54 +00:02:10,000 --> 00:02:11,000 +Of course, every object is different, + +55 +00:02:11,000 --> 00:02:13,000 +so we can have a different value to it. + +56 +00:02:13,000 --> 00:02:14,000 +If I come back to Apple, + +57 +00:02:14,000 --> 00:02:17,000 +let's say the next phone is of Samsung, + +58 +00:02:17,000 --> 00:02:20,000 +and the price is let's say $1,700. + +59 +00:02:20,000 --> 00:02:24,000 +And then we also call them as SmartPhone, okay? + +60 +00:02:24,000 --> 00:02:25,000 +The only thing that we have to change it here + +61 +00:02:25,000 --> 00:02:27,000 +as well, obj2, obj2, + +62 +00:02:27,000 --> 00:02:30,000 +and then I can print the values as well. + +63 +00:02:30,000 --> 00:02:31,000 +You know what I will do? + +64 +00:02:31,000 --> 00:02:33,000 +I will just create a method which will print everything. + +65 +00:02:33,000 --> 00:02:34,000 +I will say public void show. + +66 +00:02:34,000 --> 00:02:36,000 +It should print all the details here. + +67 +00:02:36,000 --> 00:02:39,000 +So it should print brand so that I don't have + +68 +00:02:39,000 --> 00:02:41,000 +to print that every time, + +69 +00:02:41,000 --> 00:02:45,000 +and I will say price, and I will print name as well. + +70 +00:02:45,000 --> 00:02:45,000 +Now by doing this, + +71 +00:02:45,000 --> 00:02:49,000 +what I'm doing is instead of printing it here every value, + +72 +00:02:49,000 --> 00:02:53,000 +I can simply call obj1.show do show and obj2.show. + +73 +00:02:53,000 --> 00:02:56,000 +So it'll print its own value, not other values. + +74 +00:02:56,000 --> 00:03:00,000 +And if I come back here, compile and run, + +75 +00:03:00,000 --> 00:03:01,000 +you can see we got the values, right? + +76 +00:03:01,000 --> 00:03:02,000 +Nothing complex here. + +77 +00:03:02,000 --> 00:03:05,000 +The thing is if we talk about these variables here, + +78 +00:03:05,000 --> 00:03:07,000 +they are called instance variable, right? + +79 +00:03:07,000 --> 00:03:08,000 +Now if you create a variable inside a method, + +80 +00:03:08,000 --> 00:03:10,000 +that's a local variable. + +81 +00:03:10,000 --> 00:03:12,000 +But this are your instance variable, + +82 +00:03:12,000 --> 00:03:14,000 +and every variable + +83 +00:03:14,000 --> 00:03:16,000 +for a particular object is different, right? + +84 +00:03:16,000 --> 00:03:17,000 +The value will be different. + +85 +00:03:17,000 --> 00:03:21,000 +Now what I want is I want to have the name + +86 +00:03:21,000 --> 00:03:23,000 +to be same for all the objects. + +87 +00:03:23,000 --> 00:03:25,000 +See, because what is happening is in your memory, + +88 +00:03:25,000 --> 00:03:27,000 +so let's say we have a JVM, a big JVM, + +89 +00:03:27,000 --> 00:03:30,000 +and inside this, you have a stack + +90 +00:03:30,000 --> 00:03:34,000 +where you have all the reference and locations and address, + +91 +00:03:34,000 --> 00:03:35,000 +and then you have a heap memory. + +92 +00:03:35,000 --> 00:03:38,000 +So in total, you can see we got two objects, right? + +93 +00:03:38,000 --> 00:03:43,000 +One with Mobile obj1 and one is obj2, + +94 +00:03:43,000 --> 00:03:45,000 +and then of course, they have the address as well. + +95 +00:03:45,000 --> 00:03:47,000 +So this is 101, 103. + +96 +00:03:47,000 --> 00:03:51,000 +And of course, this will be 101, 103, + +97 +00:03:51,000 --> 00:03:52,000 +and you will be referring that, right? + +98 +00:03:52,000 --> 00:03:55,000 +So this is for that here, this is for that here. + +99 +00:03:55,000 --> 00:03:57,000 +Now that's how we can basically use it, + +100 +00:03:57,000 --> 00:03:59,000 +and every object will have a different value. + +101 +00:03:59,000 --> 00:04:01,000 +But what if I want + +102 +00:04:01,000 --> 00:04:03,000 +to have a common value for all the objects? + +103 +00:04:03,000 --> 00:04:06,000 +The problem with this is if I want to make sure + +104 +00:04:06,000 --> 00:04:08,000 +that the name variable should not be a part of the object, + +105 +00:04:08,000 --> 00:04:09,000 +because that's an instance variable. + +106 +00:04:09,000 --> 00:04:11,000 +Now it will have a part of the object, + +107 +00:04:11,000 --> 00:04:13,000 +and every object will have a different value. + +108 +00:04:13,000 --> 00:04:17,000 +I want this name to be common for all. How do I do that? + +109 +00:04:17,000 --> 00:04:22,000 +Now if you make this variable as static, now by doing that, + +110 +00:04:22,000 --> 00:04:26,000 +what we are doing is we are making this name variable common + +111 +00:04:26,000 --> 00:04:28,000 +to all the object. + +112 +00:04:28,000 --> 00:04:31,000 +What I mean by that is, let's say after sometime, + +113 +00:04:31,000 --> 00:04:34,000 +if I say obj1.name. + +114 +00:04:34,000 --> 00:04:37,000 +If I change the name to let's say a Phone here, + +115 +00:04:37,000 --> 00:04:39,000 +just a Phone, not a SmartPhone anymore. + +116 +00:04:39,000 --> 00:04:42,000 +If you make this as a Phone and if you see the values, + +117 +00:04:42,000 --> 00:04:46,000 +it will affect all the objects. + +118 +00:04:46,000 --> 00:04:49,000 +You can see we are trying to change the name of obj1, + +119 +00:04:49,000 --> 00:04:54,000 +but we are actually changing the name of obj2 as well. + +120 +00:04:54,000 --> 00:04:56,000 +You can see this is also Phone now. + +121 +00:04:56,000 --> 00:04:59,000 +The reason is the static variable is actually shared + +122 +00:04:59,000 --> 00:05:01,000 +by all the objects. + +123 +00:05:01,000 --> 00:05:03,000 +So basically, we'll not be having individual static variable + +124 +00:05:03,000 --> 00:05:05,000 +for every object. + +125 +00:05:05,000 --> 00:05:06,000 +We have a common variable, + +126 +00:05:06,000 --> 00:05:08,000 +which will be used by all the objects. + +127 +00:05:08,000 --> 00:05:11,000 +Imagine there is a common variable here, name, + +128 +00:05:11,000 --> 00:05:14,000 +and all the objects will be referring to the same variable. + +129 +00:05:14,000 --> 00:05:16,000 +So if you change the value for this, + +130 +00:05:16,000 --> 00:05:19,000 +it will affect all the objects, okay? + +131 +00:05:19,000 --> 00:05:22,000 +Now this is why we use static. + +132 +00:05:22,000 --> 00:05:24,000 +Now the beauty is see, + +133 +00:05:24,000 --> 00:05:26,000 +why we have to refer brand with obj1, + +134 +00:05:26,000 --> 00:05:27,000 +price with obj1 is + +135 +00:05:27,000 --> 00:05:31,000 +because every object will have a different value for it. + +136 +00:05:31,000 --> 00:05:34,000 +Now don't you think name is a static variable, + +137 +00:05:34,000 --> 00:05:37,000 +and it will be common for all the objects? + +138 +00:05:37,000 --> 00:05:40,000 +And that's why you can actually call name + +139 +00:05:40,000 --> 00:05:43,000 +with the help of a class name itself. + +140 +00:05:43,000 --> 00:05:45,000 +You can simply say Mobile.name. + +141 +00:05:45,000 --> 00:05:47,000 +So static variables should be called + +142 +00:05:47,000 --> 00:05:50,000 +with their class name, not with objects. + +143 +00:05:50,000 --> 00:05:53,000 +You can call with objects, but avoid it. + +144 +00:05:53,000 --> 00:05:55,000 +And that's why you can see it gives you warning as well. + +145 +00:05:55,000 --> 00:05:58,000 +It says, "The static field Mobile.name should be accessed + +146 +00:05:58,000 --> 00:06:03,000 +in a static way," which is using the class name, + +147 +00:06:03,000 --> 00:06:05,000 +which is mobile, okay? + +148 +00:06:05,000 --> 00:06:06,000 +So the point + +149 +00:06:06,000 --> 00:06:09,000 +to remember here is static variables are actually shared + +150 +00:06:09,000 --> 00:06:11,000 +by different objects, + +151 +00:06:11,000 --> 00:06:13,000 +and if you want to refer to a static variable, + +152 +00:06:13,000 --> 00:06:14,000 +you can use a class name. + +153 +00:06:14,000 --> 00:06:17,000 +So there's a different area in your JVM + +154 +00:06:17,000 --> 00:06:19,000 +where you have all the static variables. + +155 +00:06:19,000 --> 00:06:22,000 +And here as well, I should say Mobile. + +156 +00:06:22,000 --> 00:06:24,000 +Now the thing is in non-static methods, + +157 +00:06:24,000 --> 00:06:26,000 +you can use static variables. + +158 +00:06:26,000 --> 00:06:30,000 +There's no harm, okay? And that's a point to remember. + +159 +00:06:30,000 --> 00:06:31,000 +In the next video, + +160 +00:06:31,000 --> 00:06:33,000 +we'll talk about what if you create a static method? + +161 +00:06:33,000 --> 00:06:35,000 +Yes, we can do that. We can create static method. + +162 +00:06:35,000 --> 00:06:38,000 +And the question is, can I use instance variables there? + +163 +00:06:38,000 --> 00:06:40,000 +Okay, we'll see that in the next video. + +164 +00:06:40,000 --> 00:06:44,000 +But here, we talked about what is a static keyword. + +165 +00:06:44,000 --> 00:06:47,000 +So static keyword means you are making something + +166 +00:06:47,000 --> 00:06:49,000 +as a class member, not a object member. + +167 +00:06:49,000 --> 00:06:52,000 +So static, this name here belongs to a class, + +168 +00:06:52,000 --> 00:06:53,000 +not to the object, + +169 +00:06:53,000 --> 00:06:55,000 +and that's why you were able to use this + +170 +00:06:55,000 --> 00:06:58,000 +with the help of a class name, okay? + +171 +00:06:58,000 --> 00:07:00,000 +And then you're saving memory as well + +172 +00:07:00,000 --> 00:07:03,000 +because not every object will have their own value for it + +173 +00:07:03,000 --> 00:07:05,000 +or their own copy for it. + +174 +00:07:05,000 --> 00:07:06,000 +So that's about static variable. + +175 +00:07:06,000 --> 00:07:09,000 +In the next video, we'll talk about static methods. + diff --git a/02 - Core Java/042 Source-code.url b/02 - Core Java/042 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/042 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/042 Static method_en.srt b/02 - Core Java/042 Static method_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..79a75ed8559372f5bc517a73bd8fd94c8b895cbb --- /dev/null +++ b/02 - Core Java/042 Static method_en.srt @@ -0,0 +1,508 @@ +1 +00:00:03,000 --> 00:00:05,000 +-: Now once we talked about static variable, + +2 +00:00:05,000 --> 00:00:07,000 +it's time to talk about static methods. + +3 +00:00:07,000 --> 00:00:08,000 +Now what I will do is + +4 +00:00:08,000 --> 00:00:11,000 +I will just try to remove all this content from here + +5 +00:00:11,000 --> 00:00:13,000 +just to keep it simple + +6 +00:00:13,000 --> 00:00:16,000 +and let's make a static method. + +7 +00:00:16,000 --> 00:00:17,000 +Now, the method which we have here show + +8 +00:00:17,000 --> 00:00:19,000 +is not a static method, right? + +9 +00:00:19,000 --> 00:00:20,000 +It's a instance method, + +10 +00:00:20,000 --> 00:00:23,000 +is because we have not mentioned a static keyword. + +11 +00:00:23,000 --> 00:00:27,000 +Now, whenever you want to work with a non-static method, + +12 +00:00:27,000 --> 00:00:29,000 +you need to create an object of it. + +13 +00:00:29,000 --> 00:00:30,000 +Of course, if you want to call, + +14 +00:00:30,000 --> 00:00:33,000 +you have to create object of mobile. + +15 +00:00:33,000 --> 00:00:36,000 +In fact, you know, actually we need those objects here + +16 +00:00:36,000 --> 00:00:40,000 +and let's keep it here just to use it. + +17 +00:00:40,000 --> 00:00:42,000 +Now let me create a simple static method. + +18 +00:00:42,000 --> 00:00:47,000 +So I will say public static void show1. + +19 +00:00:48,000 --> 00:00:49,000 +Again, if you can see, + +20 +00:00:49,000 --> 00:00:52,000 +I'm using a different method name just to differentiate it. + +21 +00:00:52,000 --> 00:00:54,000 +And here, if I try to, + +22 +00:00:54,000 --> 00:00:58,000 +first of all, I will just print in static method. + +23 +00:00:59,000 --> 00:01:01,000 +Now, how do we call the static method? + +24 +00:01:01,000 --> 00:01:04,000 +To call show, we have to get object, right? + +25 +00:01:04,000 --> 00:01:08,000 +You can't simply come back here and say, mobile.show, + +26 +00:01:08,000 --> 00:01:09,000 +this will not work. + +27 +00:01:09,000 --> 00:01:11,000 +If you try to call mobile.show, + +28 +00:01:11,000 --> 00:01:12,000 +it'll give you an error. + +29 +00:01:12,000 --> 00:01:13,000 +It says, + +30 +00:01:13,000 --> 00:01:14,000 +let's see the error. + +31 +00:01:14,000 --> 00:01:15,000 +It says cannot make a static reference + +32 +00:01:15,000 --> 00:01:17,000 +to a non-static method. + +33 +00:01:17,000 --> 00:01:19,000 +That means you cannot call a non-static method + +34 +00:01:19,000 --> 00:01:20,000 +with the help of a class name. + +35 +00:01:20,000 --> 00:01:23,000 +And that's why you need object here, which you can see. + +36 +00:01:23,000 --> 00:01:24,000 +We are using obj1, obj2 . + +37 +00:01:25,000 --> 00:01:28,000 +But can I call show one from mobile? + +38 +00:01:28,000 --> 00:01:29,000 +Let's try, and we can. + +39 +00:01:29,000 --> 00:01:32,000 +You can see that there's no problem. + +40 +00:01:32,000 --> 00:01:34,000 +So you can call a static method + +41 +00:01:34,000 --> 00:01:36,000 +directly with the help of a class name. + +42 +00:01:36,000 --> 00:01:37,000 +That's what we are doing here. + +43 +00:01:37,000 --> 00:01:39,000 +We're directly calling it with a class name. + +44 +00:01:39,000 --> 00:01:41,000 +Now the question is, + +45 +00:01:41,000 --> 00:01:41,000 +this looks cool, right? + +46 +00:01:41,000 --> 00:01:44,000 +Static method can be used directly with a class name, + +47 +00:01:44,000 --> 00:01:46,000 +but can I use a variables here? + +48 +00:01:46,000 --> 00:01:48,000 +Let's try to do the same thing. + +49 +00:01:48,000 --> 00:01:51,000 +I will just copy this line and paste it here. + +50 +00:01:51,000 --> 00:01:52,000 +Let's see what happens. + +51 +00:01:52,000 --> 00:01:54,000 +The moment you do that, + +52 +00:01:54,000 --> 00:01:55,000 +you can say there's no problem with name. + +53 +00:01:55,000 --> 00:01:58,000 +So that means you can use a static variable + +54 +00:01:58,000 --> 00:02:00,000 +inside a static method, + +55 +00:02:00,000 --> 00:02:03,000 +but you cannot use a non-static variable + +56 +00:02:03,000 --> 00:02:06,000 +inside a static method. + +57 +00:02:06,000 --> 00:02:08,000 +The question is why? + +58 +00:02:08,000 --> 00:02:10,000 +I mean, think about this, + +59 +00:02:10,000 --> 00:02:12,000 +brand and price they instance variable. + +60 +00:02:12,000 --> 00:02:14,000 +That means it'll be different for different objects. + +61 +00:02:14,000 --> 00:02:16,000 +Now, when you call show with obj1, + +62 +00:02:16,000 --> 00:02:17,000 +we know that in this show + +63 +00:02:17,000 --> 00:02:19,000 +when you are printing brand and price, + +64 +00:02:19,000 --> 00:02:21,000 +you're talking about obj1. + +65 +00:02:21,000 --> 00:02:23,000 +When you call show with obj2, + +66 +00:02:23,000 --> 00:02:27,000 +you know that the brand and price will be of obj2. + +67 +00:02:27,000 --> 00:02:29,000 +But then when you call mobile.show1, + +68 +00:02:29,000 --> 00:02:33,000 +how do we know which object you're referring to? + +69 +00:02:33,000 --> 00:02:36,000 +So this brand belongs to obj1 or it belongs to obj2 + +70 +00:02:36,000 --> 00:02:37,000 +and that's why it says, + +71 +00:02:37,000 --> 00:02:39,000 +hey, there's a confusion, right? + +72 +00:02:39,000 --> 00:02:40,000 +Let's not do that. + +73 +00:02:40,000 --> 00:02:42,000 +Let's not have a non-static variable + +74 +00:02:42,000 --> 00:02:45,000 +directly getting access in a static method. + +75 +00:02:45,000 --> 00:02:46,000 +It's not possible. + +76 +00:02:46,000 --> 00:02:48,000 +This time I added a directly keyword that + +77 +00:02:48,000 --> 00:02:50,000 +that means indirectly is possible. + +78 +00:02:50,000 --> 00:02:51,000 +Now, how will you do the indirectly? + +79 +00:02:51,000 --> 00:02:52,000 +It's very simple. + +80 +00:02:52,000 --> 00:02:56,000 +What if you can actually pass the object itself? + +81 +00:02:56,000 --> 00:02:57,000 +You can say, + +82 +00:02:57,000 --> 00:03:00,000 +hey, I want to run show 1 for obj1 object. + +83 +00:03:01,000 --> 00:03:02,000 +Now, since you are passing the object, + +84 +00:03:02,000 --> 00:03:04,000 +you have to also accept it here. + +85 +00:03:04,000 --> 00:03:07,000 +So it can say mobile obj. + +86 +00:03:07,000 --> 00:03:10,000 +And now, now since we have the object, + +87 +00:03:10,000 --> 00:03:13,000 +I can refer that with the help of obj1.brand, + +88 +00:03:13,000 --> 00:03:16,000 +I mean, obj.brand on obj.prize. + +89 +00:03:16,000 --> 00:03:19,000 +So basically we cannot directly use a non-static variable + +90 +00:03:19,000 --> 00:03:21,000 +inside a static method. + +91 +00:03:21,000 --> 00:03:23,000 +But if you have object reference, then you can use it. + +92 +00:03:23,000 --> 00:03:25,000 +Because now we can differentiate. + +93 +00:03:25,000 --> 00:03:26,000 +So the brand which I'm talking about + +94 +00:03:26,000 --> 00:03:28,000 +is for obj1, not for obj2. + +95 +00:03:28,000 --> 00:03:29,000 +So that's how we have to do it. + +96 +00:03:29,000 --> 00:03:31,000 +So that's how we work with static method. + +97 +00:03:31,000 --> 00:03:33,000 +Now, if you observe here, + +98 +00:03:33,000 --> 00:03:34,000 +from start we are using this, right? + +99 +00:03:34,000 --> 00:03:37,000 +The main method is static. + +100 +00:03:37,000 --> 00:03:39,000 +Why in this world we have a main static? + +101 +00:03:39,000 --> 00:03:42,000 +Is because if you don't make main static, + +102 +00:03:42,000 --> 00:03:43,000 +if this was a syntax, + +103 +00:03:43,000 --> 00:03:46,000 +the problem is if you don't mention static, + +104 +00:03:46,000 --> 00:03:50,000 +that means main becomes a non-static method, + +105 +00:03:50,000 --> 00:03:53,000 +which means if you want to call main, + +106 +00:03:53,000 --> 00:03:55,000 +we have to first create the object of demo. + +107 +00:03:55,000 --> 00:03:57,000 +Oh, that means you cannot call main + +108 +00:03:57,000 --> 00:03:59,000 +without the object of demo. + +109 +00:03:59,000 --> 00:04:01,000 +But the question is, we all know + +110 +00:04:01,000 --> 00:04:04,000 +that main is a starting point of execution. + +111 +00:04:04,000 --> 00:04:07,000 +If the execution has not started, + +112 +00:04:07,000 --> 00:04:08,000 +how can you create object of demo? + +113 +00:04:08,000 --> 00:04:11,000 +Okay, so if there's a deadlock here, right? + +114 +00:04:11,000 --> 00:04:13,000 +So to solve that problem, what we can do is + +115 +00:04:13,000 --> 00:04:15,000 +we can make this variable as static. + +116 +00:04:15,000 --> 00:04:17,000 +The moment you do that, we are saying, + +117 +00:04:17,000 --> 00:04:18,000 +hey, you know what? + +118 +00:04:18,000 --> 00:04:22,000 +To call main we don't need object of demo. + +119 +00:04:22,000 --> 00:04:24,000 +Problem solved, right? + +120 +00:04:24,000 --> 00:04:26,000 +So yeah, this is how we can solve this problem + +121 +00:04:26,000 --> 00:04:30,000 +and this is what static methods are. + +122 +00:04:30,000 --> 00:04:31,000 +Anything else? + +123 +00:04:31,000 --> 00:04:33,000 +Actually, I think we, it looks good. + +124 +00:04:33,000 --> 00:04:35,000 +In the next video, let's try to see + +125 +00:04:35,000 --> 00:04:37,000 +something more about static. + +126 +00:04:37,000 --> 00:04:39,000 +There's a concept of static block, + +127 +00:04:39,000 --> 00:04:41,000 +which we'll discuss in the next video. + diff --git a/02 - Core Java/043 Source-code.url b/02 - Core Java/043 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/043 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/043 Static block_en.srt b/02 - Core Java/043 Static block_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..b0a475dff758cbe661b906350311a51750a4f59d --- /dev/null +++ b/02 - Core Java/043 Static block_en.srt @@ -0,0 +1,800 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle piano music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now we know what is static variables, + +3 +00:00:05,000 --> 00:00:06,000 +static methods, right? + +4 +00:00:06,000 --> 00:00:08,000 +Now let's try to expand this more. + +5 +00:00:08,000 --> 00:00:09,000 +To simplify this, + +6 +00:00:09,000 --> 00:00:10,000 +what I will do is I will remove the static method + +7 +00:00:10,000 --> 00:00:11,000 +for some time + +8 +00:00:11,000 --> 00:00:14,000 +and I will also decrease this code. + +9 +00:00:14,000 --> 00:00:16,000 +Let's only work with one object now. + +10 +00:00:16,000 --> 00:00:20,000 +And here, see, normally when you work with variables, + +11 +00:00:20,000 --> 00:00:23,000 +so we know that this is an instance variable, + +12 +00:00:23,000 --> 00:00:26,000 +and this is a static variable, right, the name. + +13 +00:00:26,000 --> 00:00:29,000 +Now to instantiate the instance variable, + +14 +00:00:29,000 --> 00:00:29,000 +what do we do? + +15 +00:00:29,000 --> 00:00:33,000 +Of course we can instantiate the variables from here, + +16 +00:00:33,000 --> 00:00:34,000 +but then what if you want + +17 +00:00:34,000 --> 00:00:36,000 +to instantiate the variable on this line, + +18 +00:00:36,000 --> 00:00:37,000 +if you want to give some value? + +19 +00:00:37,000 --> 00:00:39,000 +Not instantiate, if you want to initialize the variable, + +20 +00:00:39,000 --> 00:00:40,000 +what you will do? + +21 +00:00:40,000 --> 00:00:42,000 +So basically we have seen one concept, + +22 +00:00:42,000 --> 00:00:43,000 +right, which is constructor. + +23 +00:00:43,000 --> 00:00:48,000 +So using constructor, we can say, okay, public Mobile, + +24 +00:00:49,000 --> 00:00:51,000 +and we can give some default values to it, right? + +25 +00:00:51,000 --> 00:00:53,000 +So by default, the brand will be empty string, + +26 +00:00:53,000 --> 00:00:55,000 +and by default the price will be, + +27 +00:00:55,000 --> 00:00:57,000 +let's say $200, right? + +28 +00:00:57,000 --> 00:01:01,000 +So this is the default brand, default text, + +29 +00:01:01,000 --> 00:01:02,000 +and default number, right? + +30 +00:01:02,000 --> 00:01:03,000 +Which we want to give. + +31 +00:01:03,000 --> 00:01:04,000 +What about name? + +32 +00:01:04,000 --> 00:01:06,000 +I can also give a default name to a name, + +33 +00:01:06,000 --> 00:01:09,000 +which is phone in this case, let's say. + +34 +00:01:09,000 --> 00:01:10,000 +And we can do that. + +35 +00:01:10,000 --> 00:01:12,000 +We can initialize the static variable + +36 +00:01:12,000 --> 00:01:13,000 +inside a constructor, + +37 +00:01:13,000 --> 00:01:14,000 +and there's no harm in that. + +38 +00:01:14,000 --> 00:01:19,000 +Now the problem is every time you create an object, + +39 +00:01:19,000 --> 00:01:20,000 +every time you create one more object, + +40 +00:01:20,000 --> 00:01:22,000 +it'll call the construct, right? + +41 +00:01:22,000 --> 00:01:24,000 +And don't you think it makes sense for this tool + +42 +00:01:24,000 --> 00:01:26,000 +to initialize every time, + +43 +00:01:26,000 --> 00:01:29,000 +but why name has to initialize every time? + +44 +00:01:29,000 --> 00:01:32,000 +I feel that the name variable, which is static, + +45 +00:01:32,000 --> 00:01:34,000 +should be initialized only once. + +46 +00:01:34,000 --> 00:01:34,000 +What do you feel? + +47 +00:01:34,000 --> 00:01:36,000 +Of course the same thing, right? + +48 +00:01:36,000 --> 00:01:37,000 +How will you do that? + +49 +00:01:37,000 --> 00:01:38,000 +So if you mention that here, + +50 +00:01:38,000 --> 00:01:40,000 +of course every time you call the constructor, + +51 +00:01:40,000 --> 00:01:41,000 +it will initialize. + +52 +00:01:41,000 --> 00:01:42,000 +We don't wanna do that. + +53 +00:01:42,000 --> 00:01:43,000 +Now to achieve that, + +54 +00:01:43,000 --> 00:01:47,000 +we have a special block called a static block. + +55 +00:01:47,000 --> 00:01:48,000 +Now, in the static block, + +56 +00:01:48,000 --> 00:01:52,000 +we can basically initialize the variable, + +57 +00:01:52,000 --> 00:01:53,000 +the static variable. + +58 +00:01:53,000 --> 00:01:54,000 +Now the good thing + +59 +00:01:54,000 --> 00:01:57,000 +about this is the static block will be called only once. + +60 +00:01:57,000 --> 00:02:00,000 +Irrespective how many objects you create, + +61 +00:02:00,000 --> 00:02:02,000 +it'll call the static block only once. + +62 +00:02:02,000 --> 00:02:04,000 +So what I will do is just to demonstrate + +63 +00:02:04,000 --> 00:02:08,000 +that I will create one more object here, let's say. + +64 +00:02:08,000 --> 00:02:09,000 +And I will not assign any value to it, + +65 +00:02:09,000 --> 00:02:14,000 +just to default values obj2, second object. + +66 +00:02:14,000 --> 00:02:16,000 +So it'll call the constructor two times. + +67 +00:02:16,000 --> 00:02:19,000 +It'll call the static block only once. + +68 +00:02:19,000 --> 00:02:19,000 +To prove that, + +69 +00:02:19,000 --> 00:02:21,000 +what I will do is I will just print something. + +70 +00:02:21,000 --> 00:02:24,000 +I will say in static block. + +71 +00:02:24,000 --> 00:02:25,000 +Now the syntax for static block is very simple. + +72 +00:02:25,000 --> 00:02:27,000 +It's static keyword, + +73 +00:02:27,000 --> 00:02:29,000 +and then you just open and close. + +74 +00:02:29,000 --> 00:02:30,000 +There's no bracket or something. + +75 +00:02:30,000 --> 00:02:33,000 +It's just a simple block, okay? + +76 +00:02:33,000 --> 00:02:33,000 +And in the constructor as well, + +77 +00:02:33,000 --> 00:02:38,000 +I will say in constructor, okay? + +78 +00:02:38,000 --> 00:02:42,000 +And now with this, let me just compile this code, okay? + +79 +00:02:42,000 --> 00:02:43,000 +There is an issue. + +80 +00:02:43,000 --> 00:02:45,000 +I have returned one extra, okay. + +81 +00:02:45,000 --> 00:02:49,000 +I have removed the curly brackets from here. + +82 +00:02:49,000 --> 00:02:52,000 +So compile and run. + +83 +00:02:52,000 --> 00:02:54,000 +So you can see it is calling the static block only once, + +84 +00:02:54,000 --> 00:02:57,000 +and it's calling constructor two times. + +85 +00:02:57,000 --> 00:02:58,000 +There's one more issue, + +86 +00:02:58,000 --> 00:03:01,000 +why static block is called first + +87 +00:03:01,000 --> 00:03:03,000 +even before the constructor is calling static block. + +88 +00:03:03,000 --> 00:03:04,000 +And that's weird. + +89 +00:03:04,000 --> 00:03:05,000 +Is it the sequence? + +90 +00:03:05,000 --> 00:03:07,000 +Let's try, let me just put this + +91 +00:03:07,000 --> 00:03:09,000 +after the constructor. + +92 +00:03:09,000 --> 00:03:11,000 +Let's see if it doesn't make any sense. + +93 +00:03:11,000 --> 00:03:13,000 +Anyway, Java does not follow the sequence. + +94 +00:03:13,000 --> 00:03:16,000 +But just to confirm, + +95 +00:03:16,000 --> 00:03:20,000 +you can see the static block is still getting called first. + +96 +00:03:20,000 --> 00:03:23,000 +Now to understand that, let's go to the diagram here. + +97 +00:03:23,000 --> 00:03:24,000 +See what happens. + +98 +00:03:24,000 --> 00:03:26,000 +There are two words, + +99 +00:03:26,000 --> 00:03:27,000 +and we only focus on one word. + +100 +00:03:27,000 --> 00:03:30,000 +Every time you get the object, that two steps, + +101 +00:03:30,000 --> 00:03:34,000 +one, your class loads, + +102 +00:03:35,000 --> 00:03:39,000 +and then your objects are, okay, + +103 +00:03:39,000 --> 00:03:40,000 +so first your class loads + +104 +00:03:40,000 --> 00:03:42,000 +and then your object gets instantiated. + +105 +00:03:42,000 --> 00:03:44,000 +So there are two steps. + +106 +00:03:44,000 --> 00:03:46,000 +Every time you create the object, + +107 +00:03:46,000 --> 00:03:48,000 +there are two steps, class loads + +108 +00:03:48,000 --> 00:03:50,000 +and object will then will get initialized. + +109 +00:03:50,000 --> 00:03:53,000 +The thing is, the class will load only once. + +110 +00:03:53,000 --> 00:03:57,000 +So basically in your JVM, you have a special area here, + +111 +00:03:57,000 --> 00:04:00,000 +which is called a class loader, okay? + +112 +00:04:00,000 --> 00:04:03,000 +And this will have all the class which are loaded. + +113 +00:04:03,000 --> 00:04:04,000 +And by default, of course you don't have + +114 +00:04:04,000 --> 00:04:05,000 +any class loaded, right? + +115 +00:04:05,000 --> 00:04:09,000 +So the class will get loaded from your library + +116 +00:04:09,000 --> 00:04:11,000 +to the class loader. + +117 +00:04:11,000 --> 00:04:13,000 +And it'll happen only once. + +118 +00:04:13,000 --> 00:04:15,000 +So for the first object, when it sees, "Hey, + +119 +00:04:15,000 --> 00:04:18,000 +this object creation, it'll load the class first," + +120 +00:04:18,000 --> 00:04:20,000 +and then it'll create the object. + +121 +00:04:20,000 --> 00:04:22,000 +Now, every time you load a class, + +122 +00:04:22,000 --> 00:04:25,000 +it'll call the static block. + +123 +00:04:25,000 --> 00:04:27,000 +Now, since class loading happens first, + +124 +00:04:27,000 --> 00:04:29,000 +and that's why it'll call the static block first, + +125 +00:04:29,000 --> 00:04:31,000 +and then it'll create the object. + +126 +00:04:31,000 --> 00:04:34,000 +And that's why it'll call the constructor + +127 +00:04:34,000 --> 00:04:36,000 +after static block. + +128 +00:04:36,000 --> 00:04:38,000 +Now, irrespective of how many objects you create, + +129 +00:04:38,000 --> 00:04:40,000 +the class is already loaded, right? + +130 +00:04:40,000 --> 00:04:42,000 +So you can simply create the next object + +131 +00:04:42,000 --> 00:04:43,000 +without loading because it's already loaded. + +132 +00:04:43,000 --> 00:04:46,000 +Now my question is, if that is how it is working, + +133 +00:04:46,000 --> 00:04:48,000 +what if I don't create any object? + +134 +00:04:48,000 --> 00:04:50,000 +I'm not getting any object now. + +135 +00:04:50,000 --> 00:04:52,000 +So if you don't create the object, + +136 +00:04:52,000 --> 00:04:54,000 +of course it'll not call the constructor. + +137 +00:04:54,000 --> 00:04:55,000 +But what about static block? + +138 +00:04:55,000 --> 00:04:57,000 +It'll not even call the static block. + +139 +00:04:57,000 --> 00:05:00,000 +If you can see, we don't have any output, oh, okay. + +140 +00:05:00,000 --> 00:05:02,000 +So that means if you don't create the object, + +141 +00:05:02,000 --> 00:05:05,000 +it'll not load the class as well. + +142 +00:05:05,000 --> 00:05:06,000 +But what if you wanna do that? + +143 +00:05:06,000 --> 00:05:10,000 +What if you want to load the class? + +144 +00:05:10,000 --> 00:05:11,000 +How would you do that? + +145 +00:05:11,000 --> 00:05:14,000 +Now for that, we have a special class inside Java, + +146 +00:05:14,000 --> 00:05:16,000 +which is called a Class class. + +147 +00:05:16,000 --> 00:05:17,000 +Yeah, that's a class. + +148 +00:05:17,000 --> 00:05:20,000 +Okay, I know Java has been weird. + +149 +00:05:20,000 --> 00:05:22,000 +We have Object class, we have Class class, + +150 +00:05:22,000 --> 00:05:23,000 +but that's how it is designed. + +151 +00:05:23,000 --> 00:05:26,000 +So Java Class, if you go there, + +152 +00:05:26,000 --> 00:05:29,000 +you can see it's a class and it has a lot of methods to it. + +153 +00:05:29,000 --> 00:05:32,000 +One of the methods which we want to use here is forName. + +154 +00:05:32,000 --> 00:05:36,000 +Now forName is a method which does one thing + +155 +00:05:36,000 --> 00:05:38,000 +which loads your class. + +156 +00:05:38,000 --> 00:05:40,000 +And you just have to mention your class name. + +157 +00:05:40,000 --> 00:05:41,000 +What's the class name? + +158 +00:05:41,000 --> 00:05:43,000 +Well, the class name is Mobile. + +159 +00:05:43,000 --> 00:05:44,000 +So in this you can mention the class name. + +160 +00:05:44,000 --> 00:05:46,000 +That's it, okay? + +161 +00:05:46,000 --> 00:05:48,000 +The only problem is it throws an exception, okay? + +162 +00:05:48,000 --> 00:05:50,000 +At this point we have not talked about exception, + +163 +00:05:50,000 --> 00:05:52,000 +but just ignore this line. + +164 +00:05:52,000 --> 00:05:55,000 +We'll talk about this in detail later once we start + +165 +00:05:55,000 --> 00:05:56,000 +with the exception concept. + +166 +00:05:56,000 --> 00:05:58,000 +But as of now, just to make it work, + +167 +00:05:58,000 --> 00:06:00,000 +I'm just writing this line, ignore it. + +168 +00:06:00,000 --> 00:06:04,000 +What's important is the class.forName will load your class. + +169 +00:06:04,000 --> 00:06:07,000 +If I go to forName, you can see forName is a method, + +170 +00:06:07,000 --> 00:06:08,000 +and it's a static method. + +171 +00:06:08,000 --> 00:06:11,000 +So you can call this with a class name. + +172 +00:06:12,000 --> 00:06:14,000 +And this particular method, + +173 +00:06:15,000 --> 00:06:18,000 +do they have any definition mentioned? + +174 +00:06:18,000 --> 00:06:20,000 +Okay, so it says the system class loader is used. + +175 +00:06:20,000 --> 00:06:23,000 +So basically it'll load the system class loader + +176 +00:06:23,000 --> 00:06:26,000 +to load this class. + +177 +00:06:26,000 --> 00:06:28,000 +Okay, so which class, Mobile class. + +178 +00:06:28,000 --> 00:06:31,000 +Okay, so now if I just write this line, + +179 +00:06:31,000 --> 00:06:32,000 +it'll not create the object, + +180 +00:06:32,000 --> 00:06:36,000 +it'll just instantiate it, compile, run. + +181 +00:06:36,000 --> 00:06:40,000 +You can see it says a new static block. + +182 +00:06:40,000 --> 00:06:42,000 +Okay, that means you're just loading the class. + +183 +00:06:42,000 --> 00:06:44,000 +So basically that's how you load your class. + +184 +00:06:44,000 --> 00:06:49,000 +You can simply use this line to load your class. + +185 +00:06:49,000 --> 00:06:50,000 +Normally when you build a project, + +186 +00:06:50,000 --> 00:06:51,000 +you don't actually use this feature. + +187 +00:06:51,000 --> 00:06:53,000 +Yes, there are some situation where you use it. + +188 +00:06:53,000 --> 00:06:57,000 +Example, when we start with JDBC, we'll talk about this one, + +189 +00:06:57,000 --> 00:06:59,000 +but we normally don't use it. + +190 +00:06:59,000 --> 00:07:00,000 +I'm just using this just to show you how + +191 +00:07:00,000 --> 00:07:02,000 +to call a static block, + +192 +00:07:02,000 --> 00:07:03,000 +how to load a class. + +193 +00:07:03,000 --> 00:07:05,000 +Okay, I hope this video makes sense + +194 +00:07:05,000 --> 00:07:07,000 +where we talked about static block. + +195 +00:07:07,000 --> 00:07:11,000 +So static block is used to initialize the static variables. + +196 +00:07:11,000 --> 00:07:12,000 +So that's it from this video in which we talked + +197 +00:07:12,000 --> 00:07:14,000 +about static block. + +198 +00:07:14,000 --> 00:07:16,000 +In the other videos we have talked about static keyword, + +199 +00:07:16,000 --> 00:07:18,000 +static variables, static methods, + +200 +00:07:18,000 --> 00:07:19,000 +and yeah, that's about static. + diff --git a/02 - Core Java/044 Encapsulation_en.srt b/02 - Core Java/044 Encapsulation_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..9f09825f1e86d043db3f51aac11547bd58d24f43 --- /dev/null +++ b/02 - Core Java/044 Encapsulation_en.srt @@ -0,0 +1,1144 @@ +1 +00:00:00,000 --> 00:00:03,000 +(soft music plays) + +2 +00:00:03,000 --> 00:00:05,000 +-: In this video, let's talk about Encapsulation. + +3 +00:00:05,000 --> 00:00:07,000 +Now I know that's a new term, + +4 +00:00:07,000 --> 00:00:09,000 +but when we started with OOPs concept, + +5 +00:00:09,000 --> 00:00:12,000 +now Encapsulation is a part of OOPs. + +6 +00:00:12,000 --> 00:00:16,000 +So when I say let's implement Object Oriented Programming, + +7 +00:00:16,000 --> 00:00:18,000 +so yeah, we have talked about class, + +8 +00:00:18,000 --> 00:00:19,000 +we have talked about objects, + +9 +00:00:19,000 --> 00:00:21,000 +and the next step is Encapsulation here. + +10 +00:00:21,000 --> 00:00:24,000 +Now by word, what Encapsulation means? + +11 +00:00:24,000 --> 00:00:25,000 +We have the concept of capsules + +12 +00:00:25,000 --> 00:00:28,000 +where we have medicine powder there? + +13 +00:00:28,000 --> 00:00:30,000 +So basically we can imagine capsule + +14 +00:00:30,000 --> 00:00:34,000 +as something which is keeping it closed, keeping it tight, + +15 +00:00:34,000 --> 00:00:37,000 +and no one from the outside world can use it. + +16 +00:00:37,000 --> 00:00:39,000 +That makes sense, right? + +17 +00:00:39,000 --> 00:00:41,000 +So what we can do here is let's say, + +18 +00:00:41,000 --> 00:00:42,000 +let's design a class, okay? + +19 +00:00:42,000 --> 00:00:44,000 +And let's name this class as Human. + +20 +00:00:44,000 --> 00:00:46,000 +I know we can use different names, + +21 +00:00:46,000 --> 00:00:47,000 +but let's go with Human here. + +22 +00:00:47,000 --> 00:00:51,000 +And I know we are aliens, but let's say this is Human. + +23 +00:00:51,000 --> 00:00:54,000 +Now here, when you have this Human class, + +24 +00:00:54,000 --> 00:00:56,000 +a Human will have some parameters, right? + +25 +00:00:56,000 --> 00:00:59,000 +Example, if you remember, we have talked about this + +26 +00:00:59,000 --> 00:01:01,000 +before, as a Human or as a person, + +27 +00:01:01,000 --> 00:01:03,000 +we know a lot of different stuff. + +28 +00:01:03,000 --> 00:01:05,000 +So in your mind you have a lot of different concepts + +29 +00:01:05,000 --> 00:01:07,000 +or you know a lot of things. + +30 +00:01:07,000 --> 00:01:11,000 +Example, you know your name, you know where you stay, + +31 +00:01:11,000 --> 00:01:12,000 +you know your favorite actor, + +32 +00:01:12,000 --> 00:01:14,000 +you know your favorite trainer. + +33 +00:01:14,000 --> 00:01:16,000 +Of course you can name, you can use my name there, + +34 +00:01:16,000 --> 00:01:18,000 +but just kidding. + +35 +00:01:18,000 --> 00:01:20,000 +So we have different information, right? + +36 +00:01:20,000 --> 00:01:21,000 +Now, can you imagine this, + +37 +00:01:21,000 --> 00:01:25,000 +that all the information are actually stored in your brain + +38 +00:01:25,000 --> 00:01:27,000 +as a variable box? + +39 +00:01:27,000 --> 00:01:30,000 +I'm a big fan of Pixel movie, which is, + +40 +00:01:30,000 --> 00:01:32,000 +I don't know the exact movie name, + +41 +00:01:32,000 --> 00:01:34,000 +but you know, Pac-Man basically eats people + +42 +00:01:34,000 --> 00:01:36,000 +and then they become pixels. + +43 +00:01:36,000 --> 00:01:40,000 +So you can imagine the data in your mind is cube, okay? + +44 +00:01:40,000 --> 00:01:42,000 +And in that box, you have the data. + +45 +00:01:42,000 --> 00:01:46,000 +Example, your name is stored in a box, which name Navin, + +46 +00:01:46,000 --> 00:01:48,000 +or which name is equal to + +47 +00:01:48,000 --> 00:01:50,000 +and in the box will be having a value. + +48 +00:01:50,000 --> 00:01:51,000 +For me it's Navin. + +49 +00:01:51,000 --> 00:01:53,000 +Let's say, your favorite actor, + +50 +00:01:53,000 --> 00:01:56,000 +so you'll be having my favorite actor, + +51 +00:01:56,000 --> 00:01:58,000 +and inside the box will be having a value. + +52 +00:01:58,000 --> 00:02:02,000 +So what I'm saying is, all the data is stored in your brain + +53 +00:02:02,000 --> 00:02:04,000 +in the format of boxes, variables. + +54 +00:02:04,000 --> 00:02:07,000 +Now of course you can use these variables, right? + +55 +00:02:07,000 --> 00:02:08,000 +So let's say my name is Navin, + +56 +00:02:08,000 --> 00:02:10,000 +and let's say if I'm walking on a street + +57 +00:02:10,000 --> 00:02:14,000 +and if somebody is asking my name, I might provide my name. + +58 +00:02:14,000 --> 00:02:17,000 +What if somebody is asking my email address? + +59 +00:02:17,000 --> 00:02:19,000 +I might provide them my email address depending + +60 +00:02:19,000 --> 00:02:20,000 +upon who that person is. + +61 +00:02:20,000 --> 00:02:25,000 +What if somebody is asking for my children's name, + +62 +00:02:25,000 --> 00:02:27,000 +my wife name, my family name, my Facebook password? + +63 +00:02:27,000 --> 00:02:30,000 +No, I'm not going to share those things, + +64 +00:02:30,000 --> 00:02:32,000 +it's because there's something called privacy, right? + +65 +00:02:32,000 --> 00:02:34,000 +And yeah, I know a lot of people don't believe in privacy, + +66 +00:02:34,000 --> 00:02:36,000 +but of course no one should be able + +67 +00:02:36,000 --> 00:02:38,000 +to get your data directly, right? + +68 +00:02:38,000 --> 00:02:40,000 +It's not like your data is available + +69 +00:02:40,000 --> 00:02:41,000 +for the entire world to see. + +70 +00:02:41,000 --> 00:02:43,000 +It's there, it's closed. + +71 +00:02:43,000 --> 00:02:44,000 +No one can see the data. + +72 +00:02:44,000 --> 00:02:45,000 +Of course they can see my Facebook profile, + +73 +00:02:45,000 --> 00:02:48,000 +but no one can see the data inside your brain. + +74 +00:02:48,000 --> 00:02:50,000 +That's important. + +75 +00:02:50,000 --> 00:02:52,000 +If they want to know your data, they have to ask + +76 +00:02:52,000 --> 00:02:56,000 +and you can decide do you want to share or not? + +77 +00:02:56,000 --> 00:02:59,000 +In the same way, let's say we have this Human class + +78 +00:02:59,000 --> 00:03:01,000 +and in this Human class we have a variable called "age" + +79 +00:03:01,000 --> 00:03:03,000 +and we have a string which is "name", + +80 +00:03:03,000 --> 00:03:04,000 +that's it, just two variables. + +81 +00:03:04,000 --> 00:03:08,000 +And now if I want to access those data, of course I have + +82 +00:03:08,000 --> 00:03:10,000 +to create object of Human here. + +83 +00:03:10,000 --> 00:03:13,000 +And let's say this is the Human object, new Human. + +84 +00:03:13,000 --> 00:03:16,000 +And once you got this object, with this object, + +85 +00:03:16,000 --> 00:03:17,000 +you can fetch data, right? + +86 +00:03:17,000 --> 00:03:19,000 +Of course you can set the date values as well. + +87 +00:03:19,000 --> 00:03:21,000 +And for this particular object, + +88 +00:03:21,000 --> 00:03:24,000 +I can say obj.age is, let's say 11. + +89 +00:03:24,000 --> 00:03:29,000 +And obj.name is, let's say Navin. + +90 +00:03:29,000 --> 00:03:30,000 +I know that's not my age, + +91 +00:03:30,000 --> 00:03:33,000 +but at this point it's okay, so we got these values + +92 +00:03:33,000 --> 00:03:35,000 +and we got this object created here. + +93 +00:03:35,000 --> 00:03:40,000 +Now what I wanna do is, what if someone ask me, "Hey Human," + +94 +00:03:40,000 --> 00:03:42,000 +or maybe someone is trying to directly trying + +95 +00:03:42,000 --> 00:03:43,000 +to fetch the data. + +96 +00:03:43,000 --> 00:03:47,000 +Example, if I say S out and if I try to say obj name. + +97 +00:03:47,000 --> 00:03:51,000 +Now what we are doing is just by using the object, + +98 +00:03:51,000 --> 00:03:53,000 +anyone can fetch data, okay? + +99 +00:03:53,000 --> 00:03:57,000 +And this will work, okay? It'll not give any issue. + +100 +00:03:57,000 --> 00:03:59,000 +So I will say compile and run. + +101 +00:03:59,000 --> 00:04:01,000 +You can see we are able to fetch data + +102 +00:04:01,000 --> 00:04:04,000 +and syntactically there's no issue. + +103 +00:04:04,000 --> 00:04:06,000 +But then if you really think that we have + +104 +00:04:06,000 --> 00:04:08,000 +to implement software for the physical world + +105 +00:04:08,000 --> 00:04:11,000 +or the real world, it should match with the real world. + +106 +00:04:11,000 --> 00:04:13,000 +Not all data are directly accessible + +107 +00:04:13,000 --> 00:04:15,000 +and we want to control it. + +108 +00:04:15,000 --> 00:04:16,000 +So one way to do that is + +109 +00:04:16,000 --> 00:04:19,000 +what you can do is you can make your variables private. + +110 +00:04:19,000 --> 00:04:21,000 +Now what do you mean by this private keyword? + +111 +00:04:21,000 --> 00:04:23,000 +Of course we'll have a detailed discussion later. + +112 +00:04:23,000 --> 00:04:25,000 +At this point, private simply means, + +113 +00:04:25,000 --> 00:04:28,000 +this particular variable, which is age, + +114 +00:04:28,000 --> 00:04:31,000 +is accessible only in the same class. + +115 +00:04:31,000 --> 00:04:32,000 +Let me repeat: + +116 +00:04:32,000 --> 00:04:37,000 +This age variable is accessible in the same class. + +117 +00:04:37,000 --> 00:04:39,000 +The moment you make it private, it's private. + +118 +00:04:39,000 --> 00:04:41,000 +No one from the outside world can use it. + +119 +00:04:41,000 --> 00:04:44,000 +Of course I can use my data because that's private to me, + +120 +00:04:44,000 --> 00:04:46,000 +but you can't access my data. + +121 +00:04:46,000 --> 00:04:49,000 +Because that's private to me, right? + +122 +00:04:49,000 --> 00:04:51,000 +The same thing can be done for the variable here. + +123 +00:04:51,000 --> 00:04:54,000 +And if you observe the moment I do that, + +124 +00:04:54,000 --> 00:04:56,000 +we got an error here. + +125 +00:04:56,000 --> 00:04:58,000 +I mean of course vs code helps you that. + +126 +00:04:58,000 --> 00:05:02,000 +VS code says, "Hey, this variable age is not visible." + +127 +00:05:02,000 --> 00:05:05,000 +You know why it's not visible? Because it's private! + +128 +00:05:05,000 --> 00:05:09,000 +Now going forward, this is what you have to do. + +129 +00:05:09,000 --> 00:05:12,000 +Every time you create a instance variable, let me repeat. + +130 +00:05:12,000 --> 00:05:17,000 +Every time you create a instance variable, make it private. + +131 +00:05:18,000 --> 00:05:20,000 +No one from the outside world should be able to use it. + +132 +00:05:20,000 --> 00:05:23,000 +Now you will say, "Hey, doing that, + +133 +00:05:23,000 --> 00:05:26,000 +we are actually breaking all the purpose + +134 +00:05:26,000 --> 00:05:27,000 +of building a software," right? + +135 +00:05:27,000 --> 00:05:29,000 +If no one is able to access this data, + +136 +00:05:29,000 --> 00:05:31,000 +then why we are even creating these variables? + +137 +00:05:31,000 --> 00:05:32,000 +That's right. + +138 +00:05:32,000 --> 00:05:34,000 +The reason why we are creating this variable + +139 +00:05:34,000 --> 00:05:35,000 +is because we can store values + +140 +00:05:35,000 --> 00:05:37,000 +and someone should be able to access them. + +141 +00:05:37,000 --> 00:05:41,000 +They should be able to access them but not directly. + +142 +00:05:41,000 --> 00:05:44,000 +So there should be some indirect way. + +143 +00:05:44,000 --> 00:05:45,000 +Yeah, we have indirect way. + +144 +00:05:45,000 --> 00:05:47,000 +So the way they should be able to access this data is + +145 +00:05:47,000 --> 00:05:49,000 +with the help of methods. + +146 +00:05:49,000 --> 00:05:52,000 +Example, let's say if you want to know my name, you have + +147 +00:05:52,000 --> 00:05:53,000 +to ask for it + +148 +00:05:53,000 --> 00:05:57,000 +and I will decide should I share that data with you or not? + +149 +00:05:57,000 --> 00:05:58,000 +So if you say, "Hey, what's your name?" + +150 +00:05:58,000 --> 00:06:01,000 +I will say, okay, I will ask my brain what's my name + +151 +00:06:01,000 --> 00:06:03,000 +because most of the time I don't remember my own name. + +152 +00:06:03,000 --> 00:06:05,000 +So I will say, hey, what's my name? + +153 +00:06:05,000 --> 00:06:07,000 +My brain will say, "Okay, your name is Navin." + +154 +00:06:07,000 --> 00:06:09,000 +And then from my methods, which is my mouth + +155 +00:06:09,000 --> 00:06:13,000 +or by using my handwriting or by typing somewhere, + +156 +00:06:13,000 --> 00:06:16,000 +there's a way I have to share that data with you, right? + +157 +00:06:16,000 --> 00:06:20,000 +I have some methods, behavior, I can talk, + +158 +00:06:20,000 --> 00:06:21,000 +I can write, I can type. + +159 +00:06:21,000 --> 00:06:24,000 +Now using those behavior, I can share the information, okay? + +160 +00:06:24,000 --> 00:06:26,000 +Not like anyone can just come to me, + +161 +00:06:26,000 --> 00:06:29,000 +they will break my head and say, "Okay, I got the data." + +162 +00:06:29,000 --> 00:06:31,000 +No, that's not how it should work. + +163 +00:06:31,000 --> 00:06:34,000 +So how do I restrict it? + +164 +00:06:34,000 --> 00:06:35,000 +So what we can do is we can make it private. + +165 +00:06:35,000 --> 00:06:37,000 +We are restricting it. + +166 +00:06:37,000 --> 00:06:40,000 +The only way to access them is with the help of methods. + +167 +00:06:40,000 --> 00:06:42,000 +So what I can do is temporary, + +168 +00:06:42,000 --> 00:06:44,000 +I will just come in these two sections + +169 +00:06:44,000 --> 00:06:48,000 +and I will assign the value here itself and we can do that. + +170 +00:06:48,000 --> 00:06:49,000 +I will say name is Navin, + +171 +00:06:49,000 --> 00:06:53,000 +and then here coming back to public, I will say public, + +172 +00:06:53,000 --> 00:06:54,000 +I want to get data. + +173 +00:06:54,000 --> 00:06:56,000 +I want to say, "Hey, get age," okay? + +174 +00:06:56,000 --> 00:06:59,000 +And of course you can use any method name, doesn't matter. + +175 +00:06:59,000 --> 00:07:03,000 +But let's say get age and this will return a integer value. + +176 +00:07:03,000 --> 00:07:06,000 +So I will say int here, and this will return. + +177 +00:07:06,000 --> 00:07:08,000 +So this will basically return the age. + +178 +00:07:08,000 --> 00:07:11,000 +Another question is, can I access age here? + +179 +00:07:11,000 --> 00:07:13,000 +And the answer is yes you can. + +180 +00:07:13,000 --> 00:07:16,000 +You can access the age variable inside a method + +181 +00:07:16,000 --> 00:07:17,000 +of a same class. + +182 +00:07:17,000 --> 00:07:19,000 +That's right. + +183 +00:07:19,000 --> 00:07:22,000 +This getAge is defined in the same class, + +184 +00:07:22,000 --> 00:07:24,000 +that's why you are able to access that. + +185 +00:07:24,000 --> 00:07:26,000 +And if you see age here or name here, + +186 +00:07:26,000 --> 00:07:29,000 +so we are trying to get the data of another class, + +187 +00:07:29,000 --> 00:07:33,000 +demo and Human, they are two different classes. + +188 +00:07:33,000 --> 00:07:34,000 +Okay, makes sense now. + +189 +00:07:34,000 --> 00:07:36,000 +Now we can also do it for the name. + +190 +00:07:36,000 --> 00:07:39,000 +So for the name I will say public string getName + +191 +00:07:40,000 --> 00:07:44,000 +and this will basically return a name. + +192 +00:07:44,000 --> 00:07:48,000 +Okay? So we got two methods, get age and get name. + +193 +00:07:48,000 --> 00:07:50,000 +And then we are returning the age and name. + +194 +00:07:50,000 --> 00:07:54,000 +Now if someone wants to fetch it, I can simply say, + +195 +00:07:54,000 --> 00:07:56,000 +"Hey, don't call name, that will not work." + +196 +00:07:56,000 --> 00:07:58,000 +Because name is a variable, this is private. + +197 +00:07:58,000 --> 00:08:02,000 +You can access the methods, you can say obj.getName + +198 +00:08:02,000 --> 00:08:04,000 +That's how you can fetch a value. + +199 +00:08:04,000 --> 00:08:06,000 +Of course you can also fetch the age. + +200 +00:08:06,000 --> 00:08:09,000 +What I can do is I can just say, plus I can give a colon + +201 +00:08:09,000 --> 00:08:12,000 +in between and I can say obj.getAge + +202 +00:08:12,000 --> 00:08:13,000 +Okay? + +203 +00:08:13,000 --> 00:08:17,000 +Now I think we should be able to access it. + +204 +00:08:17,000 --> 00:08:20,000 +Let's try and compile, run. + +205 +00:08:20,000 --> 00:08:21,000 +Oh it's working. Can you see that? + +206 +00:08:21,000 --> 00:08:23,000 +We got Navin and 11. + +207 +00:08:23,000 --> 00:08:27,000 +So the point remember is every time you create a variable + +208 +00:08:27,000 --> 00:08:30,000 +which is private, we are making sure that it is restricted. + +209 +00:08:30,000 --> 00:08:33,000 +No one from the outside world should be able to use it. + +210 +00:08:33,000 --> 00:08:36,000 +So this age variable, this name variable will be used + +211 +00:08:36,000 --> 00:08:38,000 +by their own methods. + +212 +00:08:38,000 --> 00:08:40,000 +That actually makes some sense. + +213 +00:08:40,000 --> 00:08:43,000 +But there's one little problem here. + +214 +00:08:43,000 --> 00:08:44,000 +What if I want to assign the value? + +215 +00:08:44,000 --> 00:08:48,000 +What if I don't specify the value here? Cut and cut. + +216 +00:08:48,000 --> 00:08:49,000 +Let's say we have the variables, + +217 +00:08:49,000 --> 00:08:51,000 +but I don't assign the value. + +218 +00:08:51,000 --> 00:08:53,000 +Now of course, if you don't assign the value, + +219 +00:08:53,000 --> 00:08:56,000 +default value will be zero and a null string. + +220 +00:08:56,000 --> 00:08:58,000 +But how do I set the value? + +221 +00:08:58,000 --> 00:09:02,000 +Now for getting the data we have said get, + +222 +00:09:02,000 --> 00:09:04,000 +for setting data, can we say set? + +223 +00:09:04,000 --> 00:09:05,000 +Let's try. + +224 +00:09:05,000 --> 00:09:07,000 +So here for age, I will say public. + +225 +00:09:07,000 --> 00:09:08,000 +Now when you are setting the value, + +226 +00:09:08,000 --> 00:09:10,000 +you're not getting in return something, right? + +227 +00:09:10,000 --> 00:09:14,000 +So you will say void and you will say setAge. Okay? + +228 +00:09:14,000 --> 00:09:16,000 +Now it's just that when you are setting the age, you have + +229 +00:09:16,000 --> 00:09:19,000 +to ask, "Hey, what is a value?" + +230 +00:09:19,000 --> 00:09:22,000 +So maybe you can also accept a value here, + +231 +00:09:22,000 --> 00:09:24,000 +maybe I can say A right? + +232 +00:09:24,000 --> 00:09:29,000 +And then here I can say age is equal to A. + +233 +00:09:29,000 --> 00:09:30,000 +Now what I mean by that is + +234 +00:09:30,000 --> 00:09:33,000 +of course you have a method which is accepting a value, + +235 +00:09:33,000 --> 00:09:35,000 +assigning that to age, but how it'll work? + +236 +00:09:35,000 --> 00:09:37,000 +So let's come back here, which the portion + +237 +00:09:37,000 --> 00:09:38,000 +which we have commented, + +238 +00:09:38,000 --> 00:09:39,000 +I want to set the age, right? + +239 +00:09:39,000 --> 00:09:41,000 +So instead of setting the age to the variable, + +240 +00:09:41,000 --> 00:09:44,000 +I can say obj.setAge + +241 +00:09:44,000 --> 00:09:46,000 +We are using a method now, not the variable. + +242 +00:09:46,000 --> 00:09:50,000 +And in this particular bracket you can pass the argument. + +243 +00:09:50,000 --> 00:09:52,000 +And let's say the age is 30. + +244 +00:09:52,000 --> 00:09:54,000 +So now we are passing the value. + +245 +00:09:54,000 --> 00:09:57,000 +Now this value will be assigned to this A + +246 +00:09:57,000 --> 00:10:00,000 +and from A, this will be assigned to age. + +247 +00:10:00,000 --> 00:10:02,000 +Can we do the same thing for string? + +248 +00:10:02,000 --> 00:10:06,000 +Let's try, I'll say public void setName + +249 +00:10:06,000 --> 00:10:09,000 +And of course we have to also accept the name. + +250 +00:10:09,000 --> 00:10:11,000 +But what's the format of name? + +251 +00:10:11,000 --> 00:10:14,000 +String name I will say N this time. + +252 +00:10:14,000 --> 00:10:18,000 +And here I can say name is equal to N. + +253 +00:10:18,000 --> 00:10:21,000 +So whatever value you pass, will be assigned to this n + +254 +00:10:21,000 --> 00:10:24,000 +and from N it'll be assigned to name. + +255 +00:10:24,000 --> 00:10:26,000 +Now the same thing need to be changed here. + +256 +00:10:26,000 --> 00:10:28,000 +So I will just cut this part + +257 +00:10:28,000 --> 00:10:29,000 +and I will say cut this part as well. + +258 +00:10:29,000 --> 00:10:31,000 +I will say set name. + +259 +00:10:31,000 --> 00:10:34,000 +And in the double codes I will pass the name. + +260 +00:10:34,000 --> 00:10:36,000 +And the name here is Navin. + +261 +00:10:36,000 --> 00:10:38,000 +Or maybe I'll use a different name + +262 +00:10:38,000 --> 00:10:39,000 +this time I will say Reddy. + +263 +00:10:39,000 --> 00:10:40,000 +Okay? + +264 +00:10:40,000 --> 00:10:43,000 +And now I will just come back here, clear the screen, + +265 +00:10:43,000 --> 00:10:45,000 +compile, run. + +266 +00:10:45,000 --> 00:10:48,000 +Now you see that we got Reddy and 30. + +267 +00:10:48,000 --> 00:10:50,000 +So what we are doing is we are assigning the value + +268 +00:10:50,000 --> 00:10:52,000 +with the help of methods here + +269 +00:10:52,000 --> 00:10:56,000 +and we are getting the value with the help of methods here. + +270 +00:10:56,000 --> 00:10:58,000 +So that's how we can access this data. + +271 +00:10:58,000 --> 00:11:02,000 +Now what we are doing is we are making your variable private + +272 +00:11:02,000 --> 00:11:04,000 +and then the only way to access them is + +273 +00:11:04,000 --> 00:11:05,000 +with the help of methods, right? + +274 +00:11:05,000 --> 00:11:08,000 +So basically we are binding our data with the methods. + +275 +00:11:08,000 --> 00:11:11,000 +The only way to access that data is with methods. + +276 +00:11:11,000 --> 00:11:13,000 +So no one from the outside world can use + +277 +00:11:13,000 --> 00:11:14,000 +your variables directly. + +278 +00:11:14,000 --> 00:11:17,000 +So somewhere we are encapsulating the data and methods + +279 +00:11:17,000 --> 00:11:19,000 +and that is Encapsulation. + +280 +00:11:19,000 --> 00:11:21,000 +Okay? Simple stuff, right? + +281 +00:11:21,000 --> 00:11:24,000 +So yeah, that's it from this video. + +282 +00:11:24,000 --> 00:11:26,000 +Let's try to understand this more. + +283 +00:11:26,000 --> 00:11:28,000 +What is this get and set methods? + +284 +00:11:28,000 --> 00:11:30,000 +Can we use different names + +285 +00:11:30,000 --> 00:11:32,000 +or only this name will work? + +286 +00:11:32,000 --> 00:11:34,000 +That we'll discuss in the next video. + diff --git a/02 - Core Java/044 Source-code.url b/02 - Core Java/044 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/044 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/045 Getters and setters_en.srt b/02 - Core Java/045 Getters and setters_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..3faaa6f525133f0aa3c68fbc38d14762134e01a6 --- /dev/null +++ b/02 - Core Java/045 Getters and setters_en.srt @@ -0,0 +1,328 @@ +1 +00:00:00,000 --> 00:00:02,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: Let's try to understand what these methods are + +3 +00:00:06,000 --> 00:00:08,000 +or can we change the name? + +4 +00:00:08,000 --> 00:00:11,000 +So getAge, setAge makes sense, + +5 +00:00:11,000 --> 00:00:13,000 +but can we use some other names? + +6 +00:00:13,000 --> 00:00:14,000 +Actually, we can. + +7 +00:00:14,000 --> 00:00:16,000 +Instead of saying getAge, we can say abc, + +8 +00:00:16,000 --> 00:00:18,000 +and setAge, we can say xyz. + +9 +00:00:18,000 --> 00:00:20,000 +We can do that, okay? There's no harm in that. + +10 +00:00:20,000 --> 00:00:21,000 +The only thing is when you call these methods, + +11 +00:00:21,000 --> 00:00:23,000 +of course you have to change the name. + +12 +00:00:23,000 --> 00:00:25,000 +I will say this is xyz, + +13 +00:00:25,000 --> 00:00:28,000 +so when you set the value, you have to call xyz, + +14 +00:00:28,000 --> 00:00:31,000 +and then when you fetch the value, you have to say abc, + +15 +00:00:31,000 --> 00:00:32,000 +and it surely works. + +16 +00:00:32,000 --> 00:00:36,000 +If I try to compile this and run, + +17 +00:00:36,000 --> 00:00:37,000 +you can see it works. + +18 +00:00:37,000 --> 00:00:39,000 +Now, the problem with abc and xyz or any name + +19 +00:00:39,000 --> 00:00:44,000 +is your method name should define what is your intention. + +20 +00:00:44,000 --> 00:00:46,000 +Because most of the time it's not just about writing code, + +21 +00:00:46,000 --> 00:00:47,000 +it's also about reading code. + +22 +00:00:47,000 --> 00:00:49,000 +When you write a code once, + +23 +00:00:49,000 --> 00:00:51,000 +it will be read by multiple people + +24 +00:00:51,000 --> 00:00:53,000 +in the entire lifetime of a software. + +25 +00:00:53,000 --> 00:00:54,000 +Why not use proper words + +26 +00:00:54,000 --> 00:00:56,000 +which explains what that method is doing? + +27 +00:00:56,000 --> 00:00:58,000 +So instead of saying abc, + +28 +00:00:58,000 --> 00:01:00,000 +if I say getAge, + +29 +00:01:00,000 --> 00:01:01,000 +it is actually makes sense, right? + +30 +00:01:01,000 --> 00:01:04,000 +It is much more readable what you're doing there, right? + +31 +00:01:04,000 --> 00:01:06,000 +And as I mentioned before, + +32 +00:01:06,000 --> 00:01:08,000 +it is not a compulsion to use the same name. + +33 +00:01:08,000 --> 00:01:12,000 +But then as a Java developer, we have adopted this standard. + +34 +00:01:12,000 --> 00:01:15,000 +Whenever you want to get the value of a variable, + +35 +00:01:15,000 --> 00:01:17,000 +the method name would be get space, + +36 +00:01:17,000 --> 00:01:18,000 +the other variable name, + +37 +00:01:18,000 --> 00:01:19,000 +the variable name, which is age. + +38 +00:01:19,000 --> 00:01:20,000 +To set the value, + +39 +00:01:20,000 --> 00:01:22,000 +you will say set and you will use the variable name, + +40 +00:01:22,000 --> 00:01:24,000 +which is age, okay? + +41 +00:01:24,000 --> 00:01:25,000 +So, that's how we can use this. + +42 +00:01:25,000 --> 00:01:27,000 +So, this is called a getter. + +43 +00:01:27,000 --> 00:01:29,000 +So for the variable user, this is a getter. + +44 +00:01:29,000 --> 00:01:32,000 +And when you want to set the value, + +45 +00:01:32,000 --> 00:01:34,000 +so the method is called setter. + +46 +00:01:34,000 --> 00:01:38,000 +So, we can say that those are getters and setters. + +47 +00:01:38,000 --> 00:01:40,000 +In fact, the beauty is + +48 +00:01:40,000 --> 00:01:42,000 +if I delete the entire part, + +49 +00:01:42,000 --> 00:01:43,000 +okay, let me just do that. + +50 +00:01:43,000 --> 00:01:46,000 +I know we need to type that once again. + +51 +00:01:46,000 --> 00:01:47,000 +Actually, you don't. + +52 +00:01:47,000 --> 00:01:49,000 +Most of the IDEs have this amazing feature. + +53 +00:01:49,000 --> 00:01:52,000 +You can just right-click and there's a option of source. + +54 +00:01:52,000 --> 00:01:55,000 +In fact, it doesn't matter which IDE you're using, + +55 +00:01:55,000 --> 00:01:56,000 +you will see the option of source + +56 +00:01:56,000 --> 00:01:58,000 +or insert code, something like this. + +57 +00:01:58,000 --> 00:02:01,000 +So it will say Source Action, what do you want? + +58 +00:02:01,000 --> 00:02:03,000 +You want to get, + +59 +00:02:03,000 --> 00:02:07,000 +you have to say Generate Getters and Setters for me. + +60 +00:02:07,000 --> 00:02:08,000 +Click on that. + +61 +00:02:08,000 --> 00:02:09,000 +For both the variables, + +62 +00:02:09,000 --> 00:02:11,000 +and click on OK. + +63 +00:02:11,000 --> 00:02:14,000 +You can see it will generate the methods for you + +64 +00:02:14,000 --> 00:02:16,000 +and you see that variable names, it's same. + +65 +00:02:16,000 --> 00:02:21,000 +getAge, setAge, and then we got getName, setName, okay? + +66 +00:02:21,000 --> 00:02:23,000 +And those are called getters and setters. + +67 +00:02:23,000 --> 00:02:25,000 +And of course, there is no compulsion + +68 +00:02:25,000 --> 00:02:26,000 +to have both the methods. + +69 +00:02:26,000 --> 00:02:28,000 +You can only have getName. + +70 +00:02:28,000 --> 00:02:30,000 +You don't want to have setName, that's your choice. + +71 +00:02:30,000 --> 00:02:32,000 +There's no compulsion that there should be both. + +72 +00:02:32,000 --> 00:02:34,000 +It depends upon you how you want to implement it. + +73 +00:02:34,000 --> 00:02:38,000 +For this example, I want to assign value to age and name. + +74 +00:02:38,000 --> 00:02:39,000 +I want to get the values as well + +75 +00:02:39,000 --> 00:02:41,000 +and that's why I'm using this tool. + +76 +00:02:41,000 --> 00:02:42,000 +So only thing is for every variable, + +77 +00:02:42,000 --> 00:02:45,000 +you need two methods, getters and setters, okay? + +78 +00:02:45,000 --> 00:02:46,000 +So yeah, that's it. + +79 +00:02:46,000 --> 00:02:48,000 +But there's only one issue now. + +80 +00:02:48,000 --> 00:02:50,000 +If I use the code which is given by the IDE, + +81 +00:02:50,000 --> 00:02:53,000 +you can see this new keyword here, we got this. + +82 +00:02:53,000 --> 00:02:56,000 +What is this? So, let's talk about this in the next video. + diff --git a/02 - Core Java/045 Source-code.url b/02 - Core Java/045 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/045 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/046 Source-code.url b/02 - Core Java/046 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/046 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/046 This keyword_en.srt b/02 - Core Java/046 This keyword_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..0a887ee8c08a78f605da5ffe7c78ecbc4cc0e4f1 --- /dev/null +++ b/02 - Core Java/046 This keyword_en.srt @@ -0,0 +1,908 @@ +1 +00:00:00,000 --> 00:00:03,000 +(calm piano music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, let's talk about this + +3 +00:00:06,000 --> 00:00:07,000 +because in the previous video, + +4 +00:00:07,000 --> 00:00:08,000 +we were doing something + +5 +00:00:08,000 --> 00:00:10,000 +and then suddenly we got this keyword. + +6 +00:00:10,000 --> 00:00:13,000 +So it's important now to talk about this keyword here. + +7 +00:00:13,000 --> 00:00:15,000 +Now, what I will do is I will just remove this, + +8 +00:00:15,000 --> 00:00:19,000 +or I will just change this variable name back to A and N. + +9 +00:00:19,000 --> 00:00:24,000 +And of course, here, we have to say A and N, right? + +10 +00:00:24,000 --> 00:00:26,000 +And let me remove this keyword from here + +11 +00:00:26,000 --> 00:00:27,000 +just to see if this works. + +12 +00:00:27,000 --> 00:00:29,000 +Now, if I remove this keyword from there + +13 +00:00:29,000 --> 00:00:31,000 +and you find on this code compile, okay, + +14 +00:00:31,000 --> 00:00:34,000 +there is some problem with the variable names. + +15 +00:00:34,000 --> 00:00:36,000 +I think we are still calling X, Y, Z, + +16 +00:00:36,000 --> 00:00:37,000 +so it should be setAge. + +17 +00:00:38,000 --> 00:00:39,000 +And we are still calling A, B, C. + +18 +00:00:39,000 --> 00:00:42,000 +It should be getAge, the same thing + +19 +00:00:42,000 --> 00:00:44,000 +which we have done in the previous video. + +20 +00:00:44,000 --> 00:00:47,000 +Okay, so if I save this and if I go back and compile, + +21 +00:00:47,000 --> 00:00:51,000 +so compile, done, running, done, + +22 +00:00:51,000 --> 00:00:54,000 +so you can see we got the values right? + +23 +00:00:54,000 --> 00:00:56,000 +Now, the thing is when you talk about this variable like age + +24 +00:00:56,000 --> 00:00:58,000 +and when you are passing a here, + +25 +00:00:58,000 --> 00:01:00,000 +we are using two different variables right? + +26 +00:01:00,000 --> 00:01:04,000 +Now, the point to remember here is this a variable here + +27 +00:01:04,000 --> 00:01:05,000 +is a local variable. + +28 +00:01:05,000 --> 00:01:09,000 +This age variable here is a instance variable, right? + +29 +00:01:09,000 --> 00:01:11,000 +If you want to understand that with a diagram, + +30 +00:01:11,000 --> 00:01:13,000 +you can just go back here. + +31 +00:01:13,000 --> 00:01:15,000 +We have a stack memory, remember? + +32 +00:01:15,000 --> 00:01:17,000 +We have talked about stack and heap. + +33 +00:01:17,000 --> 00:01:19,000 +So we got a stack here and then we got heap here. + +34 +00:01:19,000 --> 00:01:22,000 +Now, the moment you create the human object, + +35 +00:01:22,000 --> 00:01:23,000 +we got this object here, Human. + +36 +00:01:23,000 --> 00:01:25,000 +So in this, we have a Human object + +37 +00:01:25,000 --> 00:01:29,000 +and of course, it will have some address, let's say, 101. + +38 +00:01:29,000 --> 00:01:32,000 +So we got obj and the address is 101. + +39 +00:01:32,000 --> 00:01:33,000 +That's the first object we got. + +40 +00:01:33,000 --> 00:01:37,000 +And there's a link here which we cannot ignore. + +41 +00:01:37,000 --> 00:01:39,000 +And then we have some more variables. + +42 +00:01:39,000 --> 00:01:41,000 +You can see if you execute. + +43 +00:01:41,000 --> 00:01:44,000 +And of course, this was my main stack. + +44 +00:01:45,000 --> 00:01:47,000 +Now, we are executing which method? + +45 +00:01:47,000 --> 00:01:52,000 +So if you say setAge, we are executing setAge method, right? + +46 +00:01:52,000 --> 00:01:54,000 +Now, setAge will have its own stack here. + +47 +00:01:54,000 --> 00:01:56,000 +Let's say, we have a setAge stack here. + +48 +00:01:56,000 --> 00:01:57,000 +You know what? + +49 +00:01:57,000 --> 00:01:59,000 +I just have to zoom it a bit + +50 +00:01:59,000 --> 00:02:01,000 +and here, let me create segments. + +51 +00:02:01,000 --> 00:02:04,000 +So we got multiple segments here + +52 +00:02:04,000 --> 00:02:05,000 +and of course, this will be linked + +53 +00:02:05,000 --> 00:02:08,000 +with this particular variable here. + +54 +00:02:08,000 --> 00:02:10,000 +And don't you think this box + +55 +00:02:10,000 --> 00:02:12,000 +which is address 101 is an object of Human? + +56 +00:02:12,000 --> 00:02:14,000 +So it will also have the instance variable. + +57 +00:02:14,000 --> 00:02:15,000 +How many instance variable we have. + +58 +00:02:15,000 --> 00:02:20,000 +We have age and name. So we got age and we got name here. + +59 +00:02:20,000 --> 00:02:23,000 +Now, if you look at the setAge, now setAge, + +60 +00:02:23,000 --> 00:02:27,000 +this particular stack is of setAge. + +61 +00:02:27,000 --> 00:02:28,000 +Every method will have its own stack. + +62 +00:02:28,000 --> 00:02:31,000 +And in this, we got a variable called a, + +63 +00:02:31,000 --> 00:02:34,000 +and the value for a which we are passing is study. + +64 +00:02:34,000 --> 00:02:36,000 +So we got 30 here, okay? + +65 +00:02:36,000 --> 00:02:40,000 +And then the same value 30, we are assigning to age. + +66 +00:02:40,000 --> 00:02:43,000 +That means we are changing this variable, + +67 +00:02:43,000 --> 00:02:45,000 +and we are assigning a value which is 30 there, + +68 +00:02:45,000 --> 00:02:47,000 +if that makes sense, okay? + +69 +00:02:47,000 --> 00:02:50,000 +So both the value for a and age is 30. + +70 +00:02:50,000 --> 00:02:52,000 +And that's why when you fetch data, + +71 +00:02:52,000 --> 00:02:56,000 +when you say getAge, it will return the value of age + +72 +00:02:56,000 --> 00:02:59,000 +in the object which is 30, in this case. + +73 +00:02:59,000 --> 00:03:00,000 +And that is perfectly working. + +74 +00:03:00,000 --> 00:03:03,000 +Now, the problem is what if I just make + +75 +00:03:03,000 --> 00:03:08,000 +this particular variable age as age, I mean, a as age? + +76 +00:03:08,000 --> 00:03:10,000 +Now, what I want is I want my local variable + +77 +00:03:10,000 --> 00:03:13,000 +and my instance variable both + +78 +00:03:13,000 --> 00:03:16,000 +to have the same variable name. + +79 +00:03:16,000 --> 00:03:19,000 +What do you mean by that is if I remove this a from here, + +80 +00:03:19,000 --> 00:03:22,000 +the variable name here will be age. + +81 +00:03:22,000 --> 00:03:25,000 +So both the variable name, the local variable + +82 +00:03:25,000 --> 00:03:28,000 +and the instance variable have the same name, + +83 +00:03:28,000 --> 00:03:30,000 +then it should work, right? + +84 +00:03:30,000 --> 00:03:31,000 +What we're trying to do is + +85 +00:03:31,000 --> 00:03:33,000 +the age variable will get 30 here. + +86 +00:03:33,000 --> 00:03:37,000 +So of course, in this stack, we got 30. + +87 +00:03:37,000 --> 00:03:40,000 +And if you see we are assigning age is equal to age. + +88 +00:03:40,000 --> 00:03:42,000 +So I'm expecting that this age will get the value + +89 +00:03:42,000 --> 00:03:45,000 +from this particular age. It should work, right? + +90 +00:03:45,000 --> 00:03:46,000 +Let's try. + +91 +00:03:46,000 --> 00:03:51,000 +If I go back to my code and compile and run, + +92 +00:03:51,000 --> 00:03:52,000 +oh, can you see that? + +93 +00:03:52,000 --> 00:03:54,000 +The value for age is still zero. + +94 +00:03:54,000 --> 00:03:56,000 +That means the assignment which we are assuming + +95 +00:03:56,000 --> 00:03:58,000 +that it will be 30, it is still zero. + +96 +00:03:58,000 --> 00:04:03,000 +Why? The reason is if we look at line number 11, + +97 +00:04:03,000 --> 00:04:07,000 +and if you see it, we are saying age is equal to age. + +98 +00:04:07,000 --> 00:04:10,000 +The problem is by default, now see, + +99 +00:04:10,000 --> 00:04:12,000 +this age can be local variable. + +100 +00:04:12,000 --> 00:04:14,000 +This age can be instance variable, right? + +101 +00:04:14,000 --> 00:04:17,000 +Because the names are same. So can we do this? + +102 +00:04:17,000 --> 00:04:21,000 +Can we say it is possible that + +103 +00:04:21,000 --> 00:04:24,000 +the age here is actually a local variable? + +104 +00:04:24,000 --> 00:04:26,000 +It is also possible that + +105 +00:04:26,000 --> 00:04:29,000 +the age is assigning value to itself. + +106 +00:04:29,000 --> 00:04:32,000 +Maybe this age is not even a instance variable. + +107 +00:04:32,000 --> 00:04:35,000 +That means this age is assigning value to itself + +108 +00:04:35,000 --> 00:04:39,000 +and this age here, which we have created, + +109 +00:04:39,000 --> 00:04:41,000 +it is still zero. + +110 +00:04:41,000 --> 00:04:42,000 +And that's why we are getting zero. + +111 +00:04:42,000 --> 00:04:44,000 +How do we solve this problem? + +112 +00:04:44,000 --> 00:04:46,000 +So somewhere we have to mention, hey, you know? + +113 +00:04:46,000 --> 00:04:48,000 +This particular age here, this is not a local variable. + +114 +00:04:48,000 --> 00:04:49,000 +This is an instance variable. + +115 +00:04:49,000 --> 00:04:52,000 +So remember this point, the preference will always give, + +116 +00:04:52,000 --> 00:04:54,000 +always be given to local variable. + +117 +00:04:54,000 --> 00:04:58,000 +So in case of confusion, it is always the local variable. + +118 +00:04:58,000 --> 00:04:59,000 +We have to mention by some way, + +119 +00:04:59,000 --> 00:05:01,000 +hey, this is not local variable. + +120 +00:05:01,000 --> 00:05:03,000 +That's an instance variable. How do you mention that? + +121 +00:05:03,000 --> 00:05:05,000 +So in order to say that's an instance variable, + +122 +00:05:05,000 --> 00:05:06,000 +can we do this? + +123 +00:05:06,000 --> 00:05:08,000 +Can we say Human? + +124 +00:05:08,000 --> 00:05:11,000 +Can we create object here itself, obj1 = new Human? + +125 +00:05:11,000 --> 00:05:14,000 +And then we can say obj1.age. + +126 +00:05:14,000 --> 00:05:16,000 +Now, what we are doing is we are saying + +127 +00:05:16,000 --> 00:05:19,000 +this age is a instance variable. + +128 +00:05:19,000 --> 00:05:20,000 +And you might be thinking, + +129 +00:05:20,000 --> 00:05:24,000 +hey, how can we use age with a object? + +130 +00:05:24,000 --> 00:05:24,000 +See, we can't do that + +131 +00:05:24,000 --> 00:05:27,000 +because we are in the same class, okay? + +132 +00:05:27,000 --> 00:05:28,000 +We were not able to do that here + +133 +00:05:28,000 --> 00:05:30,000 +because we were in different class. + +134 +00:05:30,000 --> 00:05:33,000 +But again, the problem is we are saying this age is instance + +135 +00:05:33,000 --> 00:05:35,000 +and this age is local, this should work. + +136 +00:05:35,000 --> 00:05:36,000 +Let's see if this is a problem + +137 +00:05:36,000 --> 00:05:38,000 +or we have solved the problem. + +138 +00:05:38,000 --> 00:05:42,000 +Let's run this. Oh, it is still zero. What is happening? + +139 +00:05:42,000 --> 00:05:44,000 +Let's try to understand more. + +140 +00:05:44,000 --> 00:05:46,000 +Now, when you execute this setAge method, + +141 +00:05:46,000 --> 00:05:49,000 +what we are doing is we are creating a new object. + +142 +00:05:49,000 --> 00:05:50,000 +Oh, that's the issue. + +143 +00:05:50,000 --> 00:05:54,000 +We are basically creating a new object. + +144 +00:05:54,000 --> 00:05:57,000 +Okay, how do we solve this problem? + +145 +00:05:57,000 --> 00:05:58,000 +We don't want to get new object + +146 +00:05:58,000 --> 00:06:00,000 +because the moment you create new object, + +147 +00:06:00,000 --> 00:06:02,000 +what will happen here is it will create a new object, + +148 +00:06:02,000 --> 00:06:04,000 +let's say 103. + +149 +00:06:04,000 --> 00:06:05,000 +And this will have, + +150 +00:06:05,000 --> 00:06:08,000 +this will also have age equal to name equal to, + +151 +00:06:08,000 --> 00:06:10,000 +and what value you're assigning here? + +152 +00:06:10,000 --> 00:06:11,000 +You're assigning value which is 30. + +153 +00:06:11,000 --> 00:06:16,000 +And then here in this setAge, you will create obj1 + +154 +00:06:17,000 --> 00:06:20,000 +which will have the address 103 and there's a link here. + +155 +00:06:20,000 --> 00:06:22,000 +So the value which you are changing is + +156 +00:06:22,000 --> 00:06:25,000 +of this age, not this one. + +157 +00:06:25,000 --> 00:06:28,000 +We want to assign the value to this particular age. + +158 +00:06:28,000 --> 00:06:31,000 +That means creating a new object is not a good idea. + +159 +00:06:31,000 --> 00:06:34,000 +Okay, how do I clear this? Let's remove this. + +160 +00:06:34,000 --> 00:06:35,000 +In fact, you know? this is a problem. + +161 +00:06:35,000 --> 00:06:37,000 +This is creating a new object. + +162 +00:06:37,000 --> 00:06:39,000 +Okay, if I don't want to get a new object, + +163 +00:06:39,000 --> 00:06:42,000 +how do I access this obj? + +164 +00:06:42,000 --> 00:06:44,000 +Oh, I got an idea here. + +165 +00:06:44,000 --> 00:06:49,000 +What if with this 30, if I can pass the object itself, + +166 +00:06:49,000 --> 00:06:50,000 +can we, will it solve the problem? + +167 +00:06:50,000 --> 00:06:54,000 +With 30, if you can pass this particular object, + +168 +00:06:54,000 --> 00:06:58,000 +and we can accept the object here. + +169 +00:06:58,000 --> 00:07:02,000 +What type of object it is? It is human, Human obj. + +170 +00:07:02,000 --> 00:07:05,000 +And then here, we can simply assign the value. + +171 +00:07:05,000 --> 00:07:08,000 +So what I'm doing is if you observe correctly, + +172 +00:07:08,000 --> 00:07:10,000 +we are not creating a new object. + +173 +00:07:10,000 --> 00:07:14,000 +We are basically passing the reference of the old object. + +174 +00:07:14,000 --> 00:07:19,000 +So here, it will be not 103, it will be 101, okay? + +175 +00:07:21,000 --> 00:07:24,000 +So this obj1 here will get the address 101, + +176 +00:07:24,000 --> 00:07:27,000 +and we can simply say obj1.age. + +177 +00:07:27,000 --> 00:07:29,000 +And now, we are not referring to this address, + +178 +00:07:29,000 --> 00:07:31,000 +we are referring to this address. + +179 +00:07:31,000 --> 00:07:33,000 +So we can change the age value 30, + +180 +00:07:33,000 --> 00:07:35,000 +and let's see if this works. + +181 +00:07:35,000 --> 00:07:36,000 +I hope this will work. + +182 +00:07:36,000 --> 00:07:40,000 +Compile, sorry, compile, run. + +183 +00:07:40,000 --> 00:07:43,000 +Oh, it is working. It is working. + +184 +00:07:43,000 --> 00:07:44,000 +So basically, the important thing + +185 +00:07:44,000 --> 00:07:45,000 +is how do you differentiate + +186 +00:07:45,000 --> 00:07:48,000 +between instance variable and a local variable? + +187 +00:07:48,000 --> 00:07:51,000 +It's bypassing the object itself, + +188 +00:07:51,000 --> 00:07:54,000 +but there's one little problem here. + +189 +00:07:54,000 --> 00:07:59,000 +The problem is if you read this line, it says obj.setAge + +190 +00:07:59,000 --> 00:08:02,000 +and then you're passing the obj object as well. + +191 +00:08:02,000 --> 00:08:03,000 +Why we have to do this? + +192 +00:08:03,000 --> 00:08:08,000 +Why you have to mention obj two times. Now, Java knows this. + +193 +00:08:08,000 --> 00:08:10,000 +Java says, hey, you don't worry. + +194 +00:08:10,000 --> 00:08:11,000 +Why you have to pass the object two times? + +195 +00:08:11,000 --> 00:08:14,000 +I mean, you're calling the method by using the object. + +196 +00:08:14,000 --> 00:08:15,000 +You are also passing the object. + +197 +00:08:15,000 --> 00:08:18,000 +Don't do that. Don't pass the object. + +198 +00:08:18,000 --> 00:08:20,000 +By default, I will give you this obj. + +199 +00:08:20,000 --> 00:08:22,000 +You don't even have to accept this obj. + +200 +00:08:22,000 --> 00:08:25,000 +What if Java says, hey don't worry. + +201 +00:08:25,000 --> 00:08:27,000 +Don't create the object, don't pass the object. + +202 +00:08:27,000 --> 00:08:30,000 +I will give you the object and that object, my friend, + +203 +00:08:30,000 --> 00:08:33,000 +in fact, you know, when we had Human here, + +204 +00:08:33,000 --> 00:08:35,000 +we don't even need this Human line here. + +205 +00:08:35,000 --> 00:08:38,000 +We could have removed that and even obj would've worked. + +206 +00:08:38,000 --> 00:08:41,000 +So your Java says, hey, why do you even use this obj + +207 +00:08:41,000 --> 00:08:43,000 +when we can simply use this? + +208 +00:08:43,000 --> 00:08:48,000 +Now, this is a keyword which represents the current object. + +209 +00:08:48,000 --> 00:08:50,000 +Now, when I say current object, what it means? + +210 +00:08:50,000 --> 00:08:53,000 +The object which is calling the method. + +211 +00:08:53,000 --> 00:08:56,000 +SetAge is called OBJ, which is this object. + +212 +00:08:57,000 --> 00:09:00,000 +Here in this method, we can actually use that object + +213 +00:09:00,000 --> 00:09:02,000 +with the help of this keyword. + +214 +00:09:02,000 --> 00:09:04,000 +So this refers to the current object + +215 +00:09:04,000 --> 00:09:06,000 +who is calling the setAge. + +216 +00:09:06,000 --> 00:09:08,000 +I hope that makes sense. + +217 +00:09:08,000 --> 00:09:10,000 +So from this point, we are always + +218 +00:09:10,000 --> 00:09:13,000 +going to use this which represent the current object, okay? + +219 +00:09:13,000 --> 00:09:18,000 +So here as well, instead of using name or n, + +220 +00:09:18,000 --> 00:09:20,000 +I should have used name. + +221 +00:09:20,000 --> 00:09:21,000 +The only thing is we have to refer + +222 +00:09:21,000 --> 00:09:24,000 +the part, first name here is this.name, + +223 +00:09:24,000 --> 00:09:26,000 +which is the object itself. + +224 +00:09:26,000 --> 00:09:27,000 +The current object. + +225 +00:09:27,000 --> 00:09:30,000 +I hope this all this thing have made sense. + +226 +00:09:30,000 --> 00:09:32,000 +This is what this keyword is. + +227 +00:09:32,000 --> 00:09:34,000 +And yeah, that's it from this video. + diff --git a/02 - Core Java/047 Constructor_en.srt b/02 - Core Java/047 Constructor_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..bb8edd0636169e7b881aad85e1b80e7357552a5d --- /dev/null +++ b/02 - Core Java/047 Constructor_en.srt @@ -0,0 +1,736 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle piano music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, we'll talk about constructors. + +3 +00:00:06,000 --> 00:00:07,000 +Now what are constructors? + +4 +00:00:07,000 --> 00:00:09,000 +Now look at this example here, which is of human. + +5 +00:00:09,000 --> 00:00:10,000 +Now when you talk about human, + +6 +00:00:10,000 --> 00:00:11,000 +we have some variables, right? + +7 +00:00:11,000 --> 00:00:13,000 +Of course, when you talk about this two variables, + +8 +00:00:13,000 --> 00:00:15,000 +these are instance variable, + +9 +00:00:15,000 --> 00:00:18,000 +and then we have seen in the image as well. + +10 +00:00:18,000 --> 00:00:20,000 +Basically when you create an object example in this, + +11 +00:00:20,000 --> 00:00:23,000 +we are just creating one object, + +12 +00:00:23,000 --> 00:00:26,000 +and that object is getting created inside the heap memory. + +13 +00:00:26,000 --> 00:00:28,000 +You can see this is the object we got. + +14 +00:00:28,000 --> 00:00:29,000 +And of course with that object we have assigned + +15 +00:00:29,000 --> 00:00:31,000 +the value for the age, + +16 +00:00:31,000 --> 00:00:32,000 +we have assigned the value for the name as well, + +17 +00:00:32,000 --> 00:00:35,000 +which is ready. But I'm not return it here, + +18 +00:00:35,000 --> 00:00:38,000 +but we know that the object is created. + +19 +00:00:38,000 --> 00:00:41,000 +Now the thing is, when you create the object, + +20 +00:00:41,000 --> 00:00:43,000 +if you observe the line number 29, + +21 +00:00:43,000 --> 00:00:46,000 +on this line, when you create the object, what happens? + +22 +00:00:46,000 --> 00:00:48,000 +When you create the object in the heap memory, + +23 +00:00:48,000 --> 00:00:50,000 +it will just create a blank object. + +24 +00:00:50,000 --> 00:00:54,000 +Example. If I try to remove this, if I keep it empty, + +25 +00:00:54,000 --> 00:00:57,000 +it will create a object with a value. + +26 +00:00:57,000 --> 00:00:58,000 +So the value would be, + +27 +00:00:58,000 --> 00:01:01,000 +I mean it will have a variable which is age, + +28 +00:01:01,000 --> 00:01:02,000 +and the value will be zero, + +29 +00:01:02,000 --> 00:01:05,000 +and it will have a variable called a name, + +30 +00:01:05,000 --> 00:01:07,000 +and the value will be null, right? + +31 +00:01:07,000 --> 00:01:10,000 +I will just try to print those things. + +32 +00:01:10,000 --> 00:01:13,000 +What if I do is, I will just print the same statement + +33 +00:01:13,000 --> 00:01:15,000 +before assigning the value, right? + +34 +00:01:15,000 --> 00:01:18,000 +So I'm printing it here and I will command this section. + +35 +00:01:18,000 --> 00:01:19,000 +So that's how you command, right? + +36 +00:01:19,000 --> 00:01:21,000 +I don't want this time to be executed. + +37 +00:01:21,000 --> 00:01:24,000 +And now if I go back here and + +38 +00:01:24,000 --> 00:01:25,000 +compile + +39 +00:01:25,000 --> 00:01:27,000 +and run, you can see we got null. + +40 +00:01:27,000 --> 00:01:30,000 +Null basically is when you don't have the data assigned + +41 +00:01:30,000 --> 00:01:31,000 +to object type. + +42 +00:01:31,000 --> 00:01:34,000 +That's right. String is an object type. + +43 +00:01:34,000 --> 00:01:36,000 +We'll discuss about string later in detail. + +44 +00:01:36,000 --> 00:01:38,000 +As of now, just remember by default + +45 +00:01:38,000 --> 00:01:40,000 +the value for string is null, + +46 +00:01:40,000 --> 00:01:42,000 +and the default value for integer is zero. + +47 +00:01:42,000 --> 00:01:46,000 +Okay, that means if you don't assign this values, + +48 +00:01:46,000 --> 00:01:48,000 +there will be default values. + +49 +00:01:48,000 --> 00:01:49,000 +What I want here is, + +50 +00:01:49,000 --> 00:01:52,000 +of course there is a way you can assign the values later. + +51 +00:01:52,000 --> 00:01:54,000 +But what if I want to assign the value + +52 +00:01:54,000 --> 00:01:57,000 +when the object is created? + +53 +00:01:57,000 --> 00:01:59,000 +That's right, on this line itself, + +54 +00:01:59,000 --> 00:02:00,000 +I want to assign the value. + +55 +00:02:00,000 --> 00:02:02,000 +That means the moment I get the object, + +56 +00:02:02,000 --> 00:02:03,000 +I don't want to have a null value, + +57 +00:02:03,000 --> 00:02:05,000 +I want to have some default values. + +58 +00:02:05,000 --> 00:02:08,000 +Example, let's say by default every object created, + +59 +00:02:08,000 --> 00:02:09,000 +the name should be something else. + +60 +00:02:09,000 --> 00:02:12,000 +The name should be, let's say Babu or something else. + +61 +00:02:12,000 --> 00:02:14,000 +I'm not sure, but can we have a default name? + +62 +00:02:14,000 --> 00:02:16,000 +Can we have a default age? + +63 +00:02:16,000 --> 00:02:18,000 +So let's say for my application, + +64 +00:02:18,000 --> 00:02:20,000 +this application can be used only by those people + +65 +00:02:20,000 --> 00:02:23,000 +who are 12 and about. + +66 +00:02:23,000 --> 00:02:25,000 +So I can simply say, hey, the minimum age for this is 12. + +67 +00:02:25,000 --> 00:02:28,000 +So by default the value for the age should be 12. + +68 +00:02:28,000 --> 00:02:29,000 +Can we do that? + +69 +00:02:30,000 --> 00:02:32,000 +So how do we achieve that? + +70 +00:02:32,000 --> 00:02:34,000 +Of course, we can assign the values here, + +71 +00:02:34,000 --> 00:02:37,000 +or we can use something called a constructor. + +72 +00:02:37,000 --> 00:02:39,000 +Now how a constructor looks like, + +73 +00:02:39,000 --> 00:02:43,000 +so a constructor looks like a method itself. + +74 +00:02:43,000 --> 00:02:46,000 +Let me repeat. A constructor looks like a method itself. + +75 +00:02:46,000 --> 00:02:48,000 +That means a constructor will have a name, + +76 +00:02:48,000 --> 00:02:50,000 +a constructor will have the round brackets. + +77 +00:02:50,000 --> 00:02:53,000 +Example, if we talk about methods here we have a name + +78 +00:02:53,000 --> 00:02:56,000 +and we have a round bracket in the same way + +79 +00:02:56,000 --> 00:02:59,000 +for the constructor as well, we have to use that. + +80 +00:02:59,000 --> 00:03:01,000 +So what I will do is I will say public. + +81 +00:03:01,000 --> 00:03:03,000 +Now the only changes in the constructor, + +82 +00:03:03,000 --> 00:03:06,000 +you don't specify the written type. + +83 +00:03:06,000 --> 00:03:09,000 +Okay? So we can directly mention the constructor name here, + +84 +00:03:09,000 --> 00:03:12,000 +but what should be my constructor name? + +85 +00:03:12,000 --> 00:03:15,000 +Now there's a compulsion that your constructor name + +86 +00:03:15,000 --> 00:03:17,000 +should be same as your class name. + +87 +00:03:17,000 --> 00:03:19,000 +So what is my class name? It's human. + +88 +00:03:19,000 --> 00:03:20,000 +And then since it looks like a method, + +89 +00:03:20,000 --> 00:03:23,000 +we can give a round bracket and we can open and close + +90 +00:03:23,000 --> 00:03:24,000 +the curly brackets. + +91 +00:03:24,000 --> 00:03:27,000 +Now one thing I wanted to mention, you can see + +92 +00:03:27,000 --> 00:03:29,000 +with the curly packets are mentioned just I mean, + +93 +00:03:29,000 --> 00:03:31,000 +for the opening brackets are just + +94 +00:03:31,000 --> 00:03:32,000 +mentioned on the same line. + +95 +00:03:32,000 --> 00:03:35,000 +And here the curly brackets are mentioned on the new line, + +96 +00:03:35,000 --> 00:03:36,000 +both works. + +97 +00:03:36,000 --> 00:03:38,000 +Some people prefer to use on the same line, + +98 +00:03:38,000 --> 00:03:40,000 +some people prefer to use on next line. + +99 +00:03:40,000 --> 00:03:41,000 +So anything works. + +100 +00:03:41,000 --> 00:03:44,000 +Now in this constructor I can assign the values, + +101 +00:03:44,000 --> 00:03:46,000 +but at this point let's not assign the value, + +102 +00:03:46,000 --> 00:03:49,000 +I will just simply print the statement. + +103 +00:03:49,000 --> 00:03:54,000 +I will say is sout, and here I will print in constructor. + +104 +00:03:54,000 --> 00:03:56,000 +So you can see we are printing in constructor here. + +105 +00:03:56,000 --> 00:03:59,000 +So that's our constructor. + +106 +00:03:59,000 --> 00:04:00,000 +Okay, so let me repeat what is constructor? + +107 +00:04:00,000 --> 00:04:03,000 +So constructor is a special method, that's right, + +108 +00:04:03,000 --> 00:04:05,000 +because it looks like a method. + +109 +00:04:05,000 --> 00:04:06,000 +It has the same name as a class name. + +110 +00:04:06,000 --> 00:04:08,000 +So those are the rules you have to remember. + +111 +00:04:08,000 --> 00:04:11,000 +The constructor will have the same name as a class name. + +112 +00:04:11,000 --> 00:04:13,000 +A constructor never returns anything. + +113 +00:04:13,000 --> 00:04:15,000 +You don't have to mention int, float, + +114 +00:04:15,000 --> 00:04:16,000 +or string anything there. + +115 +00:04:16,000 --> 00:04:18,000 +So just specify the access, which is public, + +116 +00:04:18,000 --> 00:04:21,000 +and then mention the name, which is human. + +117 +00:04:21,000 --> 00:04:23,000 +And in this you can do whatever you want. + +118 +00:04:23,000 --> 00:04:26,000 +Okay, so I'm just printing in constructor. + +119 +00:04:26,000 --> 00:04:28,000 +So let me just try and compile. + +120 +00:04:28,000 --> 00:04:30,000 +So compilation done. There's no problem. + +121 +00:04:30,000 --> 00:04:34,000 +When I done this code, one thing to observe, nowhere, + +122 +00:04:34,000 --> 00:04:36,000 +see, when you talk about these methods like get age + +123 +00:04:36,000 --> 00:04:39,000 +or set age, all these methods we have called them, right? + +124 +00:04:39,000 --> 00:04:41,000 +So this is where you are calling them. + +125 +00:04:41,000 --> 00:04:44,000 +Constructor, you can see we are not calling it, + +126 +00:04:44,000 --> 00:04:48,000 +but still constructor will be called. + +127 +00:04:48,000 --> 00:04:50,000 +Okay, so even if we don't call it, it will be called. + +128 +00:04:50,000 --> 00:04:51,000 +Now when it will be called, + +129 +00:04:51,000 --> 00:04:53,000 +every time you create an object, + +130 +00:04:53,000 --> 00:04:55,000 +it will call the constructor. + +131 +00:04:55,000 --> 00:04:57,000 +So every time you create the object, + +132 +00:04:57,000 --> 00:04:59,000 +it will call the constructor. + +133 +00:04:59,000 --> 00:05:01,000 +Example, let's say if I create two objects, + +134 +00:05:01,000 --> 00:05:02,000 +I will just copy this line and paste it here. + +135 +00:05:02,000 --> 00:05:04,000 +Of course, I should have a different name + +136 +00:05:04,000 --> 00:05:05,000 +for the difference OBJ 1. + +137 +00:05:05,000 --> 00:05:08,000 +Now you can see we have two objects. + +138 +00:05:08,000 --> 00:05:09,000 +Now, when you say you have two objects, + +139 +00:05:09,000 --> 00:05:12,000 +and if I try to compile and run this, + +140 +00:05:12,000 --> 00:05:14,000 +you can see we got in constructor two times. + +141 +00:05:14,000 --> 00:05:17,000 +So every time you create a new object, + +142 +00:05:17,000 --> 00:05:19,000 +it will create a constructor, + +143 +00:05:19,000 --> 00:05:20,000 +or it will call the constructor. + +144 +00:05:20,000 --> 00:05:22,000 +Okay, if that makes sense. + +145 +00:05:22,000 --> 00:05:24,000 +What we can do is if we can remove this line + +146 +00:05:24,000 --> 00:05:28,000 +and I can assign the values, I can say age is equal to 12, + +147 +00:05:28,000 --> 00:05:32,000 +and name is equal to let's say by default, John, + +148 +00:05:32,000 --> 00:05:35,000 +of course you can have any name, but let's say John. + +149 +00:05:35,000 --> 00:05:40,000 +Now in this case I can see if this values are assigned. + +150 +00:05:40,000 --> 00:05:42,000 +What I can also do is I can just command this section. + +151 +00:05:42,000 --> 00:05:44,000 +Let's say I'm not assigning the values. + +152 +00:05:44,000 --> 00:05:46,000 +Let's see what are default values? + +153 +00:05:46,000 --> 00:05:48,000 +Compile, run, you can see we got John and 12. + +154 +00:05:48,000 --> 00:05:52,000 +So by default you assign this value. + +155 +00:05:52,000 --> 00:05:55,000 +Okay, so this is where constructors are helpful. + +156 +00:05:55,000 --> 00:05:56,000 +Now, you might be thinking, + +157 +00:05:56,000 --> 00:05:59,000 +hey, we can assign values here itself, right? + +158 +00:05:59,000 --> 00:06:02,000 +See, there's a idea, there's a standards which we follow, + +159 +00:06:02,000 --> 00:06:05,000 +whenever you want to perform any operation, + +160 +00:06:05,000 --> 00:06:07,000 +always do that in a method. + +161 +00:06:07,000 --> 00:06:11,000 +Don't openly do it in the statement like here, + +162 +00:06:11,000 --> 00:06:13,000 +if you want to perform some operation, like assignment, + +163 +00:06:13,000 --> 00:06:16,000 +or calculation, always doing the methods. + +164 +00:06:16,000 --> 00:06:19,000 +And constructor is like method, right? + +165 +00:06:19,000 --> 00:06:20,000 +So you can assign the values here. + +166 +00:06:20,000 --> 00:06:23,000 +There's no harm in terms of compilation. + +167 +00:06:23,000 --> 00:06:25,000 +You can assign the value here as well. + +168 +00:06:25,000 --> 00:06:27,000 +But we have to follow standards, right? + +169 +00:06:27,000 --> 00:06:29,000 +And also just try to think about something at once. + +170 +00:06:29,000 --> 00:06:32,000 +Let's say you are trying to connect with a database. + +171 +00:06:32,000 --> 00:06:35,000 +Now in that scenario, your application + +172 +00:06:35,000 --> 00:06:38,000 +is trying to connect with our database or the network. + +173 +00:06:38,000 --> 00:06:40,000 +You need that connection right? + +174 +00:06:40,000 --> 00:06:42,000 +Now where you will write the connection? + +175 +00:06:42,000 --> 00:06:45,000 +You can write the connections in the constructor. + +176 +00:06:45,000 --> 00:06:47,000 +And that's why this becomes very important. + +177 +00:06:47,000 --> 00:06:48,000 +Now, if you remember, + +178 +00:06:48,000 --> 00:06:50,000 +we have also talked about method overloading. + +179 +00:06:50,000 --> 00:06:53,000 +So when you have two methods with the same name + +180 +00:06:53,000 --> 00:06:54,000 +but different parameters, + +181 +00:06:54,000 --> 00:06:56,000 +those are called method overloading. + +182 +00:06:56,000 --> 00:07:00,000 +Now can we do that in constructor? That's tricky, right? + +183 +00:07:00,000 --> 00:07:02,000 +So let's discuss that in the next video. + +184 +00:07:02,000 --> 00:07:05,000 +How do we achieve constructor overloading. + diff --git a/02 - Core Java/047 Source-code.url b/02 - Core Java/047 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/047 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/048 Default vs Parameterized Constructor_en.srt b/02 - Core Java/048 Default vs Parameterized Constructor_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..934d11ca8e36d14eca7a6cc0e7d151bc5a85c255 --- /dev/null +++ b/02 - Core Java/048 Default vs Parameterized Constructor_en.srt @@ -0,0 +1,520 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Okay, so now we know what is constructor, right? + +3 +00:00:05,000 --> 00:00:09,000 +But what about this? I mean, can I pass the values here? + +4 +00:00:09,000 --> 00:00:12,000 +What if I don't want to assign values like 12 and John? + +5 +00:00:12,000 --> 00:00:14,000 +What if I want to customize it? + +6 +00:00:14,000 --> 00:00:17,000 +So, what I want is every time I create the object, + +7 +00:00:17,000 --> 00:00:20,000 +I should have an option of passing those values, + +8 +00:00:20,000 --> 00:00:22,000 +not 12 and John every time. + +9 +00:00:22,000 --> 00:00:23,000 +Can we do that? + +10 +00:00:23,000 --> 00:00:24,000 +Of course, you can assign the values later + +11 +00:00:24,000 --> 00:00:26,000 +with the help of site methods, + +12 +00:00:26,000 --> 00:00:29,000 +but what if I have an idea where, hey, you know, + +13 +00:00:29,000 --> 00:00:31,000 +I, okay, let's go with this object, as well, + +14 +00:00:31,000 --> 00:00:33,000 +the second object. + +15 +00:00:33,000 --> 00:00:35,000 +And in the second object, I want to pass the values. + +16 +00:00:35,000 --> 00:00:37,000 +The first object, I want it to be default. + +17 +00:00:37,000 --> 00:00:40,000 +The second object, I want the age to be, let's say 18. + +18 +00:00:40,000 --> 00:00:44,000 +And the name should be let's say Navin. Can we do that? + +19 +00:00:44,000 --> 00:00:46,000 +If you can see, this line is giving you error. + +20 +00:00:46,000 --> 00:00:49,000 +It says, hey, you know you don't have a constructor + +21 +00:00:49,000 --> 00:00:51,000 +which accepts two values, + +22 +00:00:51,000 --> 00:00:53,000 +so those two values of type integer and string, + +23 +00:00:53,000 --> 00:00:54,000 +you don't have it. + +24 +00:00:54,000 --> 00:00:56,000 +And if you go up, actually, you don't have it. + +25 +00:00:56,000 --> 00:00:59,000 +You don't have a constructor which takes two parameters. + +26 +00:00:59,000 --> 00:01:01,000 +Can we do that? Let's try. + +27 +00:01:01,000 --> 00:01:03,000 +So, I will say public Human, + +28 +00:01:03,000 --> 00:01:06,000 +and I want this Human to take two parameters. + +29 +00:01:06,000 --> 00:01:11,000 +So I will say int a, String name + +30 +00:01:11,000 --> 00:01:13,000 +and then here, I can just come back + +31 +00:01:13,000 --> 00:01:15,000 +and I can say age is equal to a. + +32 +00:01:15,000 --> 00:01:18,000 +So, not the default value which I want to assign here. + +33 +00:01:18,000 --> 00:01:21,000 +I want to assign the value which is sent by the main method. + +34 +00:01:21,000 --> 00:01:25,000 +So, if you can see, we are passing 18 and Navin here, right? + +35 +00:01:25,000 --> 00:01:27,000 +So I can just go back here. + +36 +00:01:27,000 --> 00:01:30,000 +And age is equal to a. Name is equal to n. + +37 +00:01:30,000 --> 00:01:33,000 +Now if you look at this particular constructor here, + +38 +00:01:33,000 --> 00:01:35,000 +it is taking some parameter. + +39 +00:01:35,000 --> 00:01:38,000 +So this constructor is called a parameterized constructor. + +40 +00:01:38,000 --> 00:01:40,000 +Oh, I should say this is n. + +41 +00:01:40,000 --> 00:01:43,000 +Now, what is the difference between normal constructor + +42 +00:01:43,000 --> 00:01:45,000 +and a parameterized constructor is, in fact, + +43 +00:01:45,000 --> 00:01:47,000 +this normal constructor is also called + +44 +00:01:47,000 --> 00:01:49,000 +a default constructor. + +45 +00:01:49,000 --> 00:01:51,000 +Now we'll discuss why this called default constructor. + +46 +00:01:51,000 --> 00:01:53,000 +So, this is called a default constructor. + +47 +00:01:53,000 --> 00:01:56,000 +This is called a parameterized constructor + +48 +00:01:56,000 --> 00:01:57,000 +because we have parameters there. + +49 +00:01:57,000 --> 00:02:00,000 +And whatever value you pass here will be assigned. + +50 +00:02:00,000 --> 00:02:01,000 +Now, you can use both syntax. + +51 +00:02:01,000 --> 00:02:03,000 +You can either create an object + +52 +00:02:03,000 --> 00:02:06,000 +by not specifying any parameter or arguments, + +53 +00:02:06,000 --> 00:02:11,000 +and you can create a object with the help of values, + +54 +00:02:11,000 --> 00:02:13,000 +which we are passing it here. + +55 +00:02:13,000 --> 00:02:16,000 +So, this is parameterized and this is default. + +56 +00:02:16,000 --> 00:02:17,000 +Now what I will do just to explain this, + +57 +00:02:17,000 --> 00:02:20,000 +is I will just copy the same line + +58 +00:02:20,000 --> 00:02:23,000 +and I will print the values for obj1 this time, + +59 +00:02:23,000 --> 00:02:24,000 +not for obj, obj1. + +60 +00:02:24,000 --> 00:02:26,000 +So basically, we are printing both + +61 +00:02:26,000 --> 00:02:27,000 +and let's see what the output is. + +62 +00:02:27,000 --> 00:02:29,000 +Compile, run. + +63 +00:02:29,000 --> 00:02:32,000 +You can see we got the first object, John : 12. + +64 +00:02:32,000 --> 00:02:33,000 +The second object is not John and 12. + +65 +00:02:33,000 --> 00:02:35,000 +It is Navin and 18 + +66 +00:02:35,000 --> 00:02:36,000 +because you are passing those values. + +67 +00:02:36,000 --> 00:02:38,000 +Now what I will do this time and I will remove this part + +68 +00:02:38,000 --> 00:02:40,000 +just to explain you something. + +69 +00:02:40,000 --> 00:02:42,000 +See, when I mentioned that every time, + +70 +00:02:42,000 --> 00:02:44,000 +let me just commend this section, as well. + +71 +00:02:44,000 --> 00:02:47,000 +See, every time you create the object, + +72 +00:02:47,000 --> 00:02:50,000 +it will call the constructor, right? + +73 +00:02:50,000 --> 00:02:52,000 +But then if you see, I have removed the constructor. + +74 +00:02:52,000 --> 00:02:53,000 +Will this work? + +75 +00:02:53,000 --> 00:02:55,000 +And it was working before. + +76 +00:02:55,000 --> 00:02:56,000 +Remember when we started this example of Human, + +77 +00:02:56,000 --> 00:02:58,000 +we were not having any constructor. + +78 +00:02:58,000 --> 00:03:00,000 +It was still working. + +79 +00:03:00,000 --> 00:03:03,000 +At this point, your Java will say, hey, don't worry. + +80 +00:03:03,000 --> 00:03:05,000 +If you don't mention constructor there, + +81 +00:03:05,000 --> 00:03:07,000 +I will create a constructor for you. + +82 +00:03:07,000 --> 00:03:10,000 +And that's why we say that's a default constructor. + +83 +00:03:10,000 --> 00:03:13,000 +So behind the scene, you will get this constructor here, + +84 +00:03:13,000 --> 00:03:15,000 +which is public Human. + +85 +00:03:15,000 --> 00:03:18,000 +This is what you will get even if you don't mention, okay? + +86 +00:03:18,000 --> 00:03:20,000 +The only thing is it will be default. + +87 +00:03:20,000 --> 00:03:22,000 +It will be blank if you don't mention. + +88 +00:03:22,000 --> 00:03:23,000 +But if you want to do something, of course, + +89 +00:03:23,000 --> 00:03:25,000 +you have to mention that explicitly. + +90 +00:03:25,000 --> 00:03:27,000 +And we can use the same code + +91 +00:03:27,000 --> 00:03:31,000 +or I can actually ask my IDE to give me the code. + +92 +00:03:31,000 --> 00:03:34,000 +So, you can simply go to Source and Action + +93 +00:03:34,000 --> 00:03:37,000 +and you can say, hey, I want to create a constructor, + +94 +00:03:37,000 --> 00:03:38,000 +but which constructor? + +95 +00:03:38,000 --> 00:03:41,000 +So, I want a constructor which takes both these arguments. + +96 +00:03:41,000 --> 00:03:44,000 +And I also want a default constructor. + +97 +00:03:44,000 --> 00:03:46,000 +Source Action. + +98 +00:03:46,000 --> 00:03:48,000 +I want a constructor which is default. + +99 +00:03:48,000 --> 00:03:51,000 +You can see we got two constructors. + +100 +00:03:51,000 --> 00:03:52,000 +There's only one change. + +101 +00:03:52,000 --> 00:03:56,000 +The syntax which we have return was a and name, + +102 +00:03:56,000 --> 00:03:58,000 +and here if you can see, we have age and name. + +103 +00:03:58,000 --> 00:04:01,000 +And to make it work, we have to use this keyword. + +104 +00:04:01,000 --> 00:04:03,000 +I hope you remember this keyword video previously. + +105 +00:04:03,000 --> 00:04:06,000 +And that's how it works. It has the same way which works. + +106 +00:04:06,000 --> 00:04:08,000 +The only thing is we have to assign the values here. + +107 +00:04:08,000 --> 00:04:13,000 +I will say age is equal to 12 and the name is John. + +108 +00:04:13,000 --> 00:04:16,000 +So, that's how we can basically use a default constructor + +109 +00:04:16,000 --> 00:04:17,000 +and a parameterized constructor. + +110 +00:04:17,000 --> 00:04:19,000 +Of course, we can create some more constructor here. + +111 +00:04:19,000 --> 00:04:22,000 +What if you want to just specify the name, + +112 +00:04:22,000 --> 00:04:23,000 +not the role number? + +113 +00:04:23,000 --> 00:04:28,000 +So you can say, I want a constructor, Source Action, + +114 +00:04:29,000 --> 00:04:32,000 +and I want a constructor which takes only name. + +115 +00:04:32,000 --> 00:04:33,000 +So you can see we've got a constructor + +116 +00:04:33,000 --> 00:04:34,000 +which only takes a name. + +117 +00:04:34,000 --> 00:04:36,000 +Now what happens to this type of objects? + +118 +00:04:36,000 --> 00:04:38,000 +Example, if I create an object here, + +119 +00:04:38,000 --> 00:04:39,000 +let's say the second object, + +120 +00:04:39,000 --> 00:04:41,000 +and this time I'm not passing two values. + +121 +00:04:41,000 --> 00:04:43,000 +I'm just passing one value, which is name. + +122 +00:04:43,000 --> 00:04:47,000 +In this case, the age will be zero. + +123 +00:04:47,000 --> 00:04:49,000 +So what you can do is even if you're accepting name, + +124 +00:04:49,000 --> 00:04:52,000 +you can assign the age by year, so we can say 12. + +125 +00:04:52,000 --> 00:04:53,000 +You can say age because there's no confusion + +126 +00:04:53,000 --> 00:04:55,000 +or you can also say this.age + +127 +00:04:55,000 --> 00:04:57,000 +just to maintain the uniformity. + +128 +00:04:57,000 --> 00:05:00,000 +Okay, so that's how we use constructors, + +129 +00:05:00,000 --> 00:05:03,000 +default constructors, parameterized constructors, + +130 +00:05:03,000 --> 00:05:04,000 +and yeah, that's it. + diff --git a/02 - Core Java/048 Source-code.url b/02 - Core Java/048 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/048 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/049 Naming Convention_en.srt b/02 - Core Java/049 Naming Convention_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..fe08b81a92d3b942ee3db68553842c9dfc3da6a8 --- /dev/null +++ b/02 - Core Java/049 Naming Convention_en.srt @@ -0,0 +1,440 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, let's talk about the naming conventions. + +3 +00:00:06,000 --> 00:00:08,000 +Now, if you see the code which we are written till now, + +4 +00:00:08,000 --> 00:00:11,000 +somewhere, I was using capital letters, + +5 +00:00:11,000 --> 00:00:13,000 +I was using small letters. + +6 +00:00:13,000 --> 00:00:14,000 +When to use what? + +7 +00:00:14,000 --> 00:00:17,000 +And then what if you want to join two words? + +8 +00:00:17,000 --> 00:00:19,000 +How will you make it more readable? + +9 +00:00:19,000 --> 00:00:21,000 +Now that's where Java says it follows a naming convention + +10 +00:00:21,000 --> 00:00:22,000 +of camel casing. + +11 +00:00:22,000 --> 00:00:26,000 +So basically, it uses camel casing. + +12 +00:00:26,000 --> 00:00:27,000 +Now, what camel casing means, + +13 +00:00:27,000 --> 00:00:30,000 +or let's talk about in general how do we name our classes? + +14 +00:00:30,000 --> 00:00:32,000 +Now, whenever you create a class, + +15 +00:00:32,000 --> 00:00:34,000 +a class name should start with a capital letter. + +16 +00:00:34,000 --> 00:00:36,000 +So when you talk about class, + +17 +00:00:36,000 --> 00:00:37,000 +it should start with a capital letter. + +18 +00:00:37,000 --> 00:00:40,000 +Example, if you want to get a class called Calculator, + +19 +00:00:40,000 --> 00:00:41,000 +the C should be capital. + +20 +00:00:41,000 --> 00:00:44,000 +Now on the other hand, if you talk about variables + +21 +00:00:44,000 --> 00:00:48,000 +or methods, so for variable and methods, + +22 +00:00:48,000 --> 00:00:52,000 +what you will follow is you will start with a small letter. + +23 +00:00:52,000 --> 00:00:54,000 +Example, if your variable name can be marks, + +24 +00:00:54,000 --> 00:00:56,000 +a method name can be show. + +25 +00:00:56,000 --> 00:00:58,000 +So, it will always start with small. + +26 +00:00:58,000 --> 00:01:02,000 +Class, capital, variable, method, small. + +27 +00:01:02,000 --> 00:01:04,000 +In fact, we also have a concept of interfaces, + +28 +00:01:04,000 --> 00:01:05,000 +which we'll discuss later. + +29 +00:01:05,000 --> 00:01:08,000 +So if we talk about classes and interface, + +30 +00:01:08,000 --> 00:01:10,000 +we'll have to make it capital. + +31 +00:01:10,000 --> 00:01:12,000 +Let's say interface is Runable. + +32 +00:01:12,000 --> 00:01:14,000 +So basically, this is the convention which we follow. + +33 +00:01:14,000 --> 00:01:17,000 +Class, interface, we have capital letters, + +34 +00:01:17,000 --> 00:01:19,000 +variable, methods, we have small letters. + +35 +00:01:19,000 --> 00:01:21,000 +We also have a concept of constants, + +36 +00:01:21,000 --> 00:01:23,000 +which we have talked about. + +37 +00:01:23,000 --> 00:01:27,000 +So when you talk about constants, it will have a capital, + +38 +00:01:27,000 --> 00:01:28,000 +but all capital. + +39 +00:01:28,000 --> 00:01:30,000 +Example, if we talk about the value of PIE, + +40 +00:01:30,000 --> 00:01:34,000 +if we talk about a constant of BRAND, + +41 +00:01:34,000 --> 00:01:35,000 +so doesn't matter what your constant is. + +42 +00:01:35,000 --> 00:01:39,000 +So you can have all capital here. Now, why do we do this? + +43 +00:01:39,000 --> 00:01:41,000 +We do this just to see and understand + +44 +00:01:41,000 --> 00:01:43,000 +what could be the thing which you are doing. + +45 +00:01:43,000 --> 00:01:46,000 +Example, if you say, if you see this type of things, + +46 +00:01:46,000 --> 00:01:49,000 +you can guess, hey, it can be a class or interface. + +47 +00:01:49,000 --> 00:01:52,000 +If you look at this, you know this is a variable. + +48 +00:01:52,000 --> 00:01:54,000 +If you look at this, you know this is a method. + +49 +00:01:54,000 --> 00:01:57,000 +And when you look at this, you know these are constants. + +50 +00:01:57,000 --> 00:01:59,000 +It makes your code much more readable. + +51 +00:01:59,000 --> 00:02:00,000 +And that's important, right? + +52 +00:02:00,000 --> 00:02:03,000 +It's very easy to write a code which works. + +53 +00:02:03,000 --> 00:02:05,000 +It's difficult to write a code which anyone can read, + +54 +00:02:05,000 --> 00:02:08,000 +and that's why you should follow this convention. + +55 +00:02:08,000 --> 00:02:11,000 +So, this is how you use the main convention. + +56 +00:02:11,000 --> 00:02:14,000 +But what if you have a word which which has two letters? + +57 +00:02:14,000 --> 00:02:17,000 +Example, if you talk about, show my marks. + +58 +00:02:17,000 --> 00:02:21,000 +If I want to do this, so you have to say, showMyMarks, + +59 +00:02:21,000 --> 00:02:24,000 +because if you say showmymarks, + +60 +00:02:24,000 --> 00:02:27,000 +it is actually a bit difficult to read, right? + +61 +00:02:27,000 --> 00:02:30,000 +So what you can do is every time you combine words, + +62 +00:02:30,000 --> 00:02:31,000 +make sure that the first letter of the second word + +63 +00:02:31,000 --> 00:02:34,000 +or the first letter make it capital. + +64 +00:02:34,000 --> 00:02:37,000 +It makes it much more readable compared to small letters. + +65 +00:02:37,000 --> 00:02:39,000 +Now, this is what camel casing means. + +66 +00:02:39,000 --> 00:02:41,000 +You know we have camel, so it goes up and down. + +67 +00:02:41,000 --> 00:02:45,000 +So we have small letters, then we have in between, + +68 +00:02:45,000 --> 00:02:47,000 +we have capital, small, again, we have capital. + +69 +00:02:47,000 --> 00:02:49,000 +So this is camel casing. + +70 +00:02:49,000 --> 00:02:51,000 +Now, apart from this, there are different languages + +71 +00:02:51,000 --> 00:02:53,000 +which follow snake casing rule + +72 +00:02:53,000 --> 00:02:54,000 +where we use underscores. + +73 +00:02:54,000 --> 00:02:58,000 +Example, in some languages, they follow snake casing. + +74 +00:02:58,000 --> 00:02:59,000 +They use underscore. + +75 +00:02:59,000 --> 00:03:01,000 +And people who jump between different languages, + +76 +00:03:01,000 --> 00:03:04,000 +they get this tendency of using underscore. + +77 +00:03:04,000 --> 00:03:06,000 +So, again, Java will not give you any error, + +78 +00:03:06,000 --> 00:03:08,000 +but if you follow the Java pattern, + +79 +00:03:08,000 --> 00:03:10,000 +Java developers will not like this. + +80 +00:03:10,000 --> 00:03:12,000 +So, if you're writing a code where you have a Java team + +81 +00:03:12,000 --> 00:03:15,000 +or everyone who works on Java, try to follow camel casing + +82 +00:03:15,000 --> 00:03:18,000 +and, but again, it will not give you error. + +83 +00:03:18,000 --> 00:03:19,000 +It perfectly works. + +84 +00:03:19,000 --> 00:03:22,000 +Underscore works in the names, but that's what we follow. + +85 +00:03:22,000 --> 00:03:26,000 +We follow camel casing here, not snake casing, yeah? + +86 +00:03:26,000 --> 00:03:29,000 +If you have the class name, which is again, + +87 +00:03:29,000 --> 00:03:32,000 +I want to say MyData is a class, + +88 +00:03:32,000 --> 00:03:36,000 +so you can say M is capital because that's a class name. + +89 +00:03:36,000 --> 00:03:38,000 +Now I will give you, I will show you some names + +90 +00:03:38,000 --> 00:03:40,000 +and you have to guess what those are. + +91 +00:03:40,000 --> 00:03:44,000 +Example, if I say age, that's variable, right, okay? + +92 +00:03:44,000 --> 00:03:47,000 +What if I say DATA? What is this? + +93 +00:03:47,000 --> 00:03:51,000 +This is a constant because everything is capital, okay? + +94 +00:03:51,000 --> 00:03:54,000 +Yeah, in terms of constant, if you want to say my data, + +95 +00:03:54,000 --> 00:03:56,000 +which is a constant, you have to use MYDATA, + +96 +00:03:56,000 --> 00:03:58,000 +but again, everything is capital, right? + +97 +00:03:58,000 --> 00:03:59,000 +So at this point, you can use underscore + +98 +00:03:59,000 --> 00:04:02,000 +just to differentiate this, and again, + +99 +00:04:02,000 --> 00:04:03,000 +these are the good practices which I'm showing you. + +100 +00:04:03,000 --> 00:04:04,000 +How about this? + +101 +00:04:04,000 --> 00:04:08,000 +If I use something like Human, but I use a round bracket. + +102 +00:04:08,000 --> 00:04:10,000 +Any guess? Yeah, that's a constructor. + +103 +00:04:10,000 --> 00:04:11,000 +Why is a constructor? + +104 +00:04:11,000 --> 00:04:14,000 +Is because the first letter is capital. + +105 +00:04:14,000 --> 00:04:16,000 +That means it represents a class name. + +106 +00:04:16,000 --> 00:04:17,000 +And we also have a round bracket, + +107 +00:04:17,000 --> 00:04:20,000 +method like something, so that's a constructor. + +108 +00:04:20,000 --> 00:04:22,000 +So this is a naming convention which we follow + +109 +00:04:22,000 --> 00:04:25,000 +and try to use good names for your variables, your classes, + +110 +00:04:25,000 --> 00:04:29,000 +your methods, so that it will be easy for others to read. + diff --git a/02 - Core Java/049 Source-code.url b/02 - Core Java/049 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/049 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/050 Anonymous Object_en.srt b/02 - Core Java/050 Anonymous Object_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..b9e03946b017737b8704a88a2bab04417465ac2e --- /dev/null +++ b/02 - Core Java/050 Anonymous Object_en.srt @@ -0,0 +1,508 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle piano music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Okay, so now we know how to create the object, right? + +3 +00:00:05,000 --> 00:00:06,000 +So we have a class. + +4 +00:00:06,000 --> 00:00:08,000 +Let's say we have a class A, + +5 +00:00:08,000 --> 00:00:10,000 +and then in this class A, we have a show method. + +6 +00:00:10,000 --> 00:00:11,000 +Now, how do we call show method? + +7 +00:00:11,000 --> 00:00:12,000 +That's very simple. + +8 +00:00:12,000 --> 00:00:14,000 +First you have to create the object of A, + +9 +00:00:14,000 --> 00:00:16,000 +and then you can call the show method. + +10 +00:00:16,000 --> 00:00:17,000 +Now what happens behind the scenes? + +11 +00:00:17,000 --> 00:00:21,000 +So of course you have your big JVM here, + +12 +00:00:21,000 --> 00:00:24,000 +and then in this you'll be having your stack memory + +13 +00:00:24,000 --> 00:00:26,000 +and then you'll be having your heap memory as well. + +14 +00:00:26,000 --> 00:00:29,000 +Now, in this heap memory, so if you look at line number 14, + +15 +00:00:29,000 --> 00:00:31,000 +it will create an object. + +16 +00:00:31,000 --> 00:00:33,000 +Let's say the address is 1 0 1, + +17 +00:00:33,000 --> 00:00:36,000 +and then in this you'll be having a variable or methods. + +18 +00:00:36,000 --> 00:00:37,000 +We don't have a variable as of now. + +19 +00:00:37,000 --> 00:00:39,000 +So let's say we have a method which is show. + +20 +00:00:39,000 --> 00:00:41,000 +So of course in order to call this show, we need objects. + +21 +00:00:41,000 --> 00:00:44,000 +So we have done that, and then we have OBJ here. + +22 +00:00:44,000 --> 00:00:48,000 +The address will be 1 0 1 and it will refer here. + +23 +00:00:48,000 --> 00:00:50,000 +And then every time you want to call show, + +24 +00:00:50,000 --> 00:00:54,000 +basically you have to say obj.show. + +25 +00:00:54,000 --> 00:00:56,000 +That's how it works, right? Okay. + +26 +00:00:56,000 --> 00:00:58,000 +Now just to clear the things, + +27 +00:00:58,000 --> 00:01:01,000 +this OBJ here is not called the object. + +28 +00:01:01,000 --> 00:01:04,000 +It is called a reference variable, right? + +29 +00:01:04,000 --> 00:01:07,000 +So this is your reference variable. + +30 +00:01:07,000 --> 00:01:08,000 +Why this called a reference variable? + +31 +00:01:08,000 --> 00:01:09,000 +Because it's referring to an object. + +32 +00:01:09,000 --> 00:01:10,000 +The object is here. + +33 +00:01:10,000 --> 00:01:13,000 +So this is your object, okay? + +34 +00:01:13,000 --> 00:01:15,000 +So the question is, if I want to create this object, + +35 +00:01:15,000 --> 00:01:18,000 +is this line compulsory or do we have a shortcut? + +36 +00:01:18,000 --> 00:01:19,000 +Now to understand this mode, + +37 +00:01:19,000 --> 00:01:21,000 +let's also use a concept of constructor. + +38 +00:01:21,000 --> 00:01:23,000 +So let's say we have a constructor here, which is A, + +39 +00:01:23,000 --> 00:01:26,000 +and then I'm just trying to print something here, + +40 +00:01:26,000 --> 00:01:28,000 +it says object created. + +41 +00:01:28,000 --> 00:01:30,000 +So of course we know every time you create the object, + +42 +00:01:30,000 --> 00:01:33,000 +it will call the constructor as well. + +43 +00:01:33,000 --> 00:01:33,000 +So it should print. + +44 +00:01:33,000 --> 00:01:36,000 +So if I compile and run this code, + +45 +00:01:36,000 --> 00:01:38,000 +you can see it says object created + +46 +00:01:38,000 --> 00:01:40,000 +and it is printing in a show. + +47 +00:01:40,000 --> 00:01:42,000 +Now, actually, if you look at this line here, + +48 +00:01:42,000 --> 00:01:44,000 +there are two steps, not one. + +49 +00:01:44,000 --> 00:01:46,000 +Example, so let's say if you create + +50 +00:01:46,000 --> 00:01:49,000 +a local variable called marks, okay? + +51 +00:01:49,000 --> 00:01:51,000 +And then you set the marks for it, + +52 +00:01:51,000 --> 00:01:52,000 +you set the value for it. + +53 +00:01:52,000 --> 00:01:54,000 +Let's say 99, + +54 +00:01:54,000 --> 00:01:56,000 +we hate, everyone hates this type of people, right? + +55 +00:01:56,000 --> 00:01:58,000 +Anyway, so we have this variable. + +56 +00:01:58,000 --> 00:02:00,000 +So what we are doing is we are first of all + +57 +00:02:00,000 --> 00:02:04,000 +declaring the variable and then we are as assigning a value. + +58 +00:02:04,000 --> 00:02:06,000 +The same thing can be done for this thing, right? + +59 +00:02:06,000 --> 00:02:08,000 +We can do it in two steps. + +60 +00:02:08,000 --> 00:02:09,000 +We can declare the object + +61 +00:02:09,000 --> 00:02:13,000 +and then we can say OBJ equal to new A. + +62 +00:02:13,000 --> 00:02:15,000 +Now this is called reference creation, right? + +63 +00:02:15,000 --> 00:02:17,000 +This is where you're creating a reference and then + +64 +00:02:17,000 --> 00:02:19,000 +this is where you are creating an object + +65 +00:02:19,000 --> 00:02:23,000 +and assigning the value to OBJ. + +66 +00:02:23,000 --> 00:02:26,000 +That means this is, I mean, when you say new A, + +67 +00:02:26,000 --> 00:02:29,000 +this is where you create the object. + +68 +00:02:29,000 --> 00:02:30,000 +Let me show that. + +69 +00:02:30,000 --> 00:02:35,000 +Let me remove everything from here and say new A, + +70 +00:02:35,000 --> 00:02:38,000 +this is object creation, not the entire line. + +71 +00:02:38,000 --> 00:02:41,000 +When you say new A constructor, + +72 +00:02:41,000 --> 00:02:43,000 +that is your object creation. + +73 +00:02:43,000 --> 00:02:45,000 +The moment you do that in your heap memory, + +74 +00:02:45,000 --> 00:02:48,000 +of course we don't have all this data now, + +75 +00:02:48,000 --> 00:02:50,000 +so ignore all this data, which is there, + +76 +00:02:50,000 --> 00:02:52,000 +let me just remove this. This is not there. + +77 +00:02:52,000 --> 00:02:53,000 +Imagine there's no object. + +78 +00:02:53,000 --> 00:02:55,000 +So this line will create an object, + +79 +00:02:55,000 --> 00:02:58,000 +doesn't matter what's the address. Let's say 5 0 1, + +80 +00:02:58,000 --> 00:03:01,000 +and it will have this show method, okay? + +81 +00:03:01,000 --> 00:03:04,000 +The only thing is we are not doing anything. + +82 +00:03:04,000 --> 00:03:06,000 +We are not creating any variable inside stack. + +83 +00:03:06,000 --> 00:03:10,000 +Example, when you say int marks, the moment you do that, + +84 +00:03:10,000 --> 00:03:13,000 +it will create a int variable called marks, here. + +85 +00:03:13,000 --> 00:03:15,000 +The value will be blank. + +86 +00:03:15,000 --> 00:03:17,000 +And whatever value you want to assign, + +87 +00:03:17,000 --> 00:03:18,000 +you can assign that here. + +88 +00:03:18,000 --> 00:03:20,000 +But if you can see, we are not doing anything here. + +89 +00:03:20,000 --> 00:03:22,000 +We are just creating a object, + +90 +00:03:22,000 --> 00:03:26,000 +that is created inside the heap memory, nothing in stack. + +91 +00:03:26,000 --> 00:03:29,000 +And if you don't trust me, let me just show you the proof. + +92 +00:03:29,000 --> 00:03:34,000 +If I compile this code and run, it says object created. + +93 +00:03:34,000 --> 00:03:36,000 +This is the object, + +94 +00:03:36,000 --> 00:03:40,000 +and this type of objects are called anonymous object. + +95 +00:03:40,000 --> 00:03:40,000 +You know why? + +96 +00:03:40,000 --> 00:03:43,000 +It's because this type of object don't have a name, right? + +97 +00:03:43,000 --> 00:03:45,000 +What do you call when you have a reference? + +98 +00:03:45,000 --> 00:03:47,000 +It is called a referenced object. + +99 +00:03:47,000 --> 00:03:49,000 +Okay, because there's a reference in this stack. + +100 +00:03:49,000 --> 00:03:51,000 +So when you say OBJ, there's a reference here. + +101 +00:03:51,000 --> 00:03:54,000 +For the anonymous object, there is no reference. + +102 +00:03:54,000 --> 00:03:56,000 +The only problem with this type of object is + +103 +00:03:56,000 --> 00:03:58,000 +you can't reuse them. Once you have written this line, + +104 +00:03:58,000 --> 00:04:02,000 +you got the new object in the heap, how will you use it? + +105 +00:04:02,000 --> 00:04:03,000 +That's the problem, right? + +106 +00:04:03,000 --> 00:04:04,000 +So you should use this type of object + +107 +00:04:04,000 --> 00:04:05,000 +only when it's required. + +108 +00:04:05,000 --> 00:04:08,000 +So let's say if you want to call show, + +109 +00:04:08,000 --> 00:04:09,000 +you can actually call this, okay? + +110 +00:04:09,000 --> 00:04:12,000 +So you can say new A, + +111 +00:04:12,000 --> 00:04:13,000 +dot show. + +112 +00:04:13,000 --> 00:04:15,000 +This is object, right? This is object. + +113 +00:04:15,000 --> 00:04:17,000 +So using the object you can call show. + +114 +00:04:17,000 --> 00:04:21,000 +The only thing is you can't use the same object again. + +115 +00:04:21,000 --> 00:04:24,000 +So if I compile and run, it worked. + +116 +00:04:24,000 --> 00:04:28,000 +The only thing is you can't use this object once more. + +117 +00:04:28,000 --> 00:04:30,000 +So if you write this line once more, what will happen? + +118 +00:04:30,000 --> 00:04:33,000 +Now, I mentioned that you can't reuse it. + +119 +00:04:33,000 --> 00:04:34,000 +What happens when you do that? + +120 +00:04:34,000 --> 00:04:36,000 +You can see it says object created. + +121 +00:04:36,000 --> 00:04:38,000 +So it has created two objects. + +122 +00:04:38,000 --> 00:04:43,000 +Every time you say new A, it creates a new object. + +123 +00:04:43,000 --> 00:04:45,000 +So wherever you want to use a normal object, + +124 +00:04:45,000 --> 00:04:47,000 +you can use this other objects. + +125 +00:04:47,000 --> 00:04:48,000 +Okay, so that's it. + +126 +00:04:48,000 --> 00:04:49,000 +That's it from this video + +127 +00:04:49,000 --> 00:04:51,000 +where we talked about anonymous objects. + diff --git a/02 - Core Java/050 Source-code.url b/02 - Core Java/050 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/050 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/051 Source-code.url b/02 - Core Java/051 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/051 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/051 What is Inheritance_en.srt b/02 - Core Java/051 What is Inheritance_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..2f2099aa473149cb548b38d73cd3cd051cac36cb --- /dev/null +++ b/02 - Core Java/051 What is Inheritance_en.srt @@ -0,0 +1,848 @@ +1 +00:00:00,000 --> 00:00:02,000 +(gentle music) + +2 +00:00:03,000 --> 00:00:04,000 +-: So in theory, we have talked + +3 +00:00:04,000 --> 00:00:06,000 +about what is inheritance, right? + +4 +00:00:06,000 --> 00:00:09,000 +Basically, you can just use the features of a superclass. + +5 +00:00:09,000 --> 00:00:12,000 +Let's try to implement that in this code. + +6 +00:00:12,000 --> 00:00:13,000 +Of course, we'll not be working with Humans anymore. + +7 +00:00:13,000 --> 00:00:18,000 +Let me just remove the entire code from here, and all gone. + +8 +00:00:18,000 --> 00:00:22,000 +And let me also make sure that this is empty, gone. + +9 +00:00:22,000 --> 00:00:24,000 +So we have a very simple method, which is main here. + +10 +00:00:24,000 --> 00:00:26,000 +Now what I wanna do is I want + +11 +00:00:26,000 --> 00:00:28,000 +to take the same example of calculator here. + +12 +00:00:28,000 --> 00:00:30,000 +So let me get a class called Calc, + +13 +00:00:30,000 --> 00:00:32,000 +and of course, I can just create this thing + +14 +00:00:32,000 --> 00:00:35,000 +in some other files as well, that actually makes sense. + +15 +00:00:35,000 --> 00:00:39,000 +But just to keep it simple, I'm using the same file here. + +16 +00:00:39,000 --> 00:00:41,000 +So we got a class called calculator, + +17 +00:00:41,000 --> 00:00:43,000 +and in this, I can add some methods and some variables. + +18 +00:00:43,000 --> 00:00:46,000 +So let's say I will define some methods here, + +19 +00:00:46,000 --> 00:00:49,000 +and I'll say public int, it can add two numbers. + +20 +00:00:49,000 --> 00:00:53,000 +It will accept two integers as well, I would say n1, n2. + +21 +00:00:53,000 --> 00:00:55,000 +So we got two variables, right? + +22 +00:00:55,000 --> 00:01:00,000 +And then I can just return n1 + n2, okay? + +23 +00:01:00,000 --> 00:01:03,000 +Now let's say in this calculator, we have two methods. + +24 +00:01:03,000 --> 00:01:08,000 +One is add and one is sub, which is sub here. + +25 +00:01:08,000 --> 00:01:09,000 +And there will be a subtraction operation. + +26 +00:01:09,000 --> 00:01:11,000 +So we got two methods, right? + +27 +00:01:11,000 --> 00:01:14,000 +Now one thing to remember, if you look at our File Explorer, + +28 +00:01:14,000 --> 00:01:17,000 +every time you get a file, it also creates a class file. + +29 +00:01:17,000 --> 00:01:20,000 +So for every class in your code, it will have a class files. + +30 +00:01:20,000 --> 00:01:24,000 +We have used a Calculator, a Computer, + +31 +00:01:24,000 --> 00:01:25,000 +Human, Student before, + +32 +00:01:25,000 --> 00:01:27,000 +and that's why we have all these classes. + +33 +00:01:27,000 --> 00:01:29,000 +What I will do is I will just delete them. + +34 +00:01:29,000 --> 00:01:31,000 +Okay, so you can see all these classes are gone. + +35 +00:01:31,000 --> 00:01:33,000 +In fact, let me also delete the Demo one + +36 +00:01:33,000 --> 00:01:34,000 +to see what happens. + +37 +00:01:34,000 --> 00:01:36,000 +And that's fine, you can also delete, + +38 +00:01:36,000 --> 00:01:37,000 +you can delete all the class files. + +39 +00:01:37,000 --> 00:01:39,000 +So every time you compile the code, + +40 +00:01:39,000 --> 00:01:40,000 +it will also create a class files for you. + +41 +00:01:40,000 --> 00:01:42,000 +Let me show you that. + +42 +00:01:42,000 --> 00:01:45,000 +So I will say javac, and if I mention the file name, + +43 +00:01:45,000 --> 00:01:49,000 +which is Demo.java, the moment I say Enter, + +44 +00:01:49,000 --> 00:01:50,000 +it will create two class files + +45 +00:01:50,000 --> 00:01:51,000 +because we have two classes. + +46 +00:01:51,000 --> 00:01:55,000 +If I go to Explorer, you can see we got Demo.class, + +47 +00:01:55,000 --> 00:01:57,000 +and we also have Calc.class. + +48 +00:01:57,000 --> 00:01:59,000 +Now when you run this code, if you say java, + +49 +00:01:59,000 --> 00:02:02,000 +and if you say Demo, it will just run the class files. + +50 +00:02:02,000 --> 00:02:04,000 +And as you can see, + +51 +00:02:04,000 --> 00:02:07,000 +we don't have any code here for calculator. + +52 +00:02:07,000 --> 00:02:11,000 +It will only run the Demo class, and that's how it works. + +53 +00:02:11,000 --> 00:02:14,000 +So if I just try to write this code here, + +54 +00:02:14,000 --> 00:02:18,000 +which we already have, and if I try to create object of it, + +55 +00:02:18,000 --> 00:02:19,000 +let's try creating object of it. + +56 +00:02:19,000 --> 00:02:22,000 +In fact, let me do one more thing before we start. + +57 +00:02:22,000 --> 00:02:25,000 +For this example, let me cut this part, + +58 +00:02:25,000 --> 00:02:30,000 +and create a new file and let me say this is Calc.java. + +59 +00:02:31,000 --> 00:02:35,000 +And yes, we have this code here. + +60 +00:02:35,000 --> 00:02:37,000 +So you can see now we have two files, and before I continue, + +61 +00:02:37,000 --> 00:02:40,000 +I will just delete these class files again. + +62 +00:02:40,000 --> 00:02:41,000 +Okay, class file deleted. + +63 +00:02:41,000 --> 00:02:45,000 +And now let me just compile this code once again. + +64 +00:02:45,000 --> 00:02:47,000 +So if you can see when I'm compiling it, + +65 +00:02:47,000 --> 00:02:50,000 +it is only compiling the Demo file. + +66 +00:02:50,000 --> 00:02:53,000 +It is not combining the class.java, right? + +67 +00:02:53,000 --> 00:02:55,000 +But when I create an object of it, + +68 +00:02:55,000 --> 00:02:58,000 +if I create object of Calc obj = new Calc, + +69 +00:02:58,000 --> 00:03:00,000 +of course if you want to use it, + +70 +00:03:00,000 --> 00:03:02,000 +we have to create object first. + +71 +00:03:02,000 --> 00:03:04,000 +And now if I recompile this, + +72 +00:03:04,000 --> 00:03:06,000 +and let's look at the class files. + +73 +00:03:06,000 --> 00:03:08,000 +We got the class file for class as well. + +74 +00:03:08,000 --> 00:03:12,000 +So if you are not using Calc, it will not compile it. + +75 +00:03:12,000 --> 00:03:13,000 +It'll just say, "Hey, + +76 +00:03:13,000 --> 00:03:15,000 +my responsibility is to compile the Java file. + +77 +00:03:15,000 --> 00:03:16,000 +I'm doing it." + +78 +00:03:16,000 --> 00:03:18,000 +But if you want to work with the class, + +79 +00:03:18,000 --> 00:03:21,000 +the Calc.class as well, or Calc.java as well, + +80 +00:03:21,000 --> 00:03:25,000 +you have to create object and that's how it compiles. + +81 +00:03:25,000 --> 00:03:26,000 +Now once the compile is done, + +82 +00:03:26,000 --> 00:03:30,000 +you can simply save the value in r1 or you can say obj.add. + +83 +00:03:30,000 --> 00:03:33,000 +It will add two numbers, let's say four and five. + +84 +00:03:33,000 --> 00:03:35,000 +And then we can also find r2, + +85 +00:03:35,000 --> 00:03:37,000 +which will be result of subtraction. + +86 +00:03:37,000 --> 00:03:41,000 +So I can say sub, I will say seven and three, okay? + +87 +00:03:41,000 --> 00:03:43,000 +So we basically, you can see we got these two methods, + +88 +00:03:43,000 --> 00:03:45,000 +and when you try to print the values, + +89 +00:03:45,000 --> 00:03:48,000 +I will print r1 and I will give us space, + +90 +00:03:48,000 --> 00:03:49,000 +and I'll also print r2. + +91 +00:03:49,000 --> 00:03:51,000 +So basically, I'm just printing two values back to back. + +92 +00:03:51,000 --> 00:03:55,000 +And if I say compile again because we have made some changes + +93 +00:03:55,000 --> 00:03:57,000 +and run, you can see we got nine and four. + +94 +00:03:57,000 --> 00:04:00,000 +So 4 + 5 is nine, 7 - 3 is four, + +95 +00:04:00,000 --> 00:04:02,000 +and that's what we are getting here. + +96 +00:04:02,000 --> 00:04:03,000 +Everything is working fine, right? + +97 +00:04:03,000 --> 00:04:05,000 +Now there are two conditions here, + +98 +00:04:05,000 --> 00:04:06,000 +why we are going for inheritance. + +99 +00:04:06,000 --> 00:04:09,000 +The first one is what if you want + +100 +00:04:09,000 --> 00:04:10,000 +to add some extra features? + +101 +00:04:10,000 --> 00:04:13,000 +You want, so let's say if someone is asking you for, + +102 +00:04:13,000 --> 00:04:14,000 +"Hey, you know, Navin, + +103 +00:04:14,000 --> 00:04:17,000 +I want a calculator and I want two features, + +104 +00:04:17,000 --> 00:04:19,000 +addition and subtraction. + +105 +00:04:19,000 --> 00:04:20,000 +What will be the cost?" + +106 +00:04:20,000 --> 00:04:22,000 +I would say, "Okay, I will charge you $10." + +107 +00:04:22,000 --> 00:04:24,000 +Now let's say the same person come back and say, + +108 +00:04:24,000 --> 00:04:27,000 +"Hey, you know, I want a calculator which has four features, + +109 +00:04:27,000 --> 00:04:29,000 +additional, subtraction, division, multiplication." + +110 +00:04:29,000 --> 00:04:33,000 +And maybe at this point, I want to charge $15. + +111 +00:04:33,000 --> 00:04:35,000 +Now of course, I can add those two methods here + +112 +00:04:35,000 --> 00:04:38,000 +or I can create a new class, you know why? + +113 +00:04:38,000 --> 00:04:41,000 +It's because if I edit the same file, + +114 +00:04:41,000 --> 00:04:42,000 +if I add more features here, + +115 +00:04:42,000 --> 00:04:45,000 +I can't sell calculator for $10 now. + +116 +00:04:45,000 --> 00:04:47,000 +I have to make a new product for $15. + +117 +00:04:47,000 --> 00:04:49,000 +I don't have calculator, + +118 +00:04:49,000 --> 00:04:51,000 +I have advanced calculator which does four operations. + +119 +00:04:51,000 --> 00:04:52,000 +I want both the models. + +120 +00:04:52,000 --> 00:04:57,000 +I want to sell the $10 calculator and a $15 calculator. + +121 +00:04:57,000 --> 00:04:59,000 +That's one reason. Okay, so let me just do that. + +122 +00:04:59,000 --> 00:05:01,000 +So let me create a new calculator, + +123 +00:05:01,000 --> 00:05:04,000 +and I will say this is advanced calculator, + +124 +00:05:04,000 --> 00:05:07,000 +which has all the features, .java. + +125 +00:05:07,000 --> 00:05:10,000 +And in this, I will basically have two more methods, + +126 +00:05:10,000 --> 00:05:15,000 +which is public int multi, which will take two values, + +127 +00:05:15,000 --> 00:05:19,000 +and it should return n1 into n2. + +128 +00:05:19,000 --> 00:05:21,000 +And likewise, we'll have one more method here for division. + +129 +00:05:21,000 --> 00:05:26,000 +So this is div and division, we have to say slash. + +130 +00:05:26,000 --> 00:05:28,000 +That's it, so we got an advanced calculator, + +131 +00:05:28,000 --> 00:05:29,000 +which has two features. + +132 +00:05:29,000 --> 00:05:33,000 +But now if I create an object of advanced calculator now, + +133 +00:05:33,000 --> 00:05:35,000 +instead of normal calculator, + +134 +00:05:35,000 --> 00:05:37,000 +I can access multiplication and division. + +135 +00:05:37,000 --> 00:05:39,000 +I can't access add and sub. + +136 +00:05:39,000 --> 00:05:41,000 +And that's why you can see we got an error here. + +137 +00:05:41,000 --> 00:05:44,000 +It says, "The method add is undefined + +138 +00:05:44,000 --> 00:05:46,000 +for type advanced calculator." + +139 +00:05:46,000 --> 00:05:48,000 +That means in this, we don't have add and subtract. + +140 +00:05:48,000 --> 00:05:50,000 +So what do you think? What could be solution here? + +141 +00:05:50,000 --> 00:05:52,000 +Think about this, so one solution, + +142 +00:05:52,000 --> 00:05:56,000 +which I can see is just write those two methods here. + +143 +00:05:56,000 --> 00:05:57,000 +Of course, we can do that. + +144 +00:05:57,000 --> 00:05:59,000 +Okay, we can just copy and we can paste it here. + +145 +00:05:59,000 --> 00:06:01,000 +But don't you think we are writing + +146 +00:06:01,000 --> 00:06:04,000 +the same code again and again? + +147 +00:06:04,000 --> 00:06:05,000 +Now when you repeat the same code, + +148 +00:06:05,000 --> 00:06:07,000 +it is called redundancy, right? + +149 +00:06:07,000 --> 00:06:12,000 +We have same code multiple times, and redundancy is a crime. + +150 +00:06:12,000 --> 00:06:13,000 +So make sure that you don't repeat the values, + +151 +00:06:13,000 --> 00:06:17,000 +you don't repeat the methods, definition, and classes. + +152 +00:06:17,000 --> 00:06:19,000 +The point is if I don't define it here, + +153 +00:06:19,000 --> 00:06:21,000 +how will I access them? + +154 +00:06:21,000 --> 00:06:24,000 +That's where the in inheritance concept comes into picture. + +155 +00:06:24,000 --> 00:06:26,000 +So in inheritance says, "Hey, don't worry. + +156 +00:06:26,000 --> 00:06:28,000 +If you already have a class called Calc, + +157 +00:06:28,000 --> 00:06:31,000 +you can simply say extends Calc, your job is done." + +158 +00:06:31,000 --> 00:06:34,000 +Okay, so you simply say advanced calculator + +159 +00:06:34,000 --> 00:06:37,000 +extends calculator, and now all the features + +160 +00:06:37,000 --> 00:06:40,000 +of calculator comes in here. + +161 +00:06:40,000 --> 00:06:41,000 +And there's one more beauty. + +162 +00:06:41,000 --> 00:06:44,000 +Even if you don't have this Java file, that's fine. + +163 +00:06:44,000 --> 00:06:48,000 +For inheritance, you need a class file, not a Java file. + +164 +00:06:48,000 --> 00:06:50,000 +So even if you delete this, it will still work, okay? + +165 +00:06:50,000 --> 00:06:51,000 +That's the first reason. + +166 +00:06:51,000 --> 00:06:53,000 +Even if you don't have a code ready with you, + +167 +00:06:53,000 --> 00:06:55,000 +even if you don't have a Java file, + +168 +00:06:55,000 --> 00:06:57,000 +you can still use a class file. + +169 +00:06:57,000 --> 00:06:59,000 +Okay, so that's the beauty of inheritance. + +170 +00:06:59,000 --> 00:07:01,000 +We are simply saying extends calculator, + +171 +00:07:01,000 --> 00:07:02,000 +which is this class. + +172 +00:07:02,000 --> 00:07:05,000 +Now what do you think, will it work? Let's try. + +173 +00:07:05,000 --> 00:07:06,000 +So you can see there's no problem, + +174 +00:07:06,000 --> 00:07:07,000 +add and subtraction working. + +175 +00:07:07,000 --> 00:07:09,000 +We can add some more variables here. + +176 +00:07:09,000 --> 00:07:13,000 +I can just copy this code and paste, paste, + +177 +00:07:13,000 --> 00:07:14,000 +not on the same line. + +178 +00:07:15,000 --> 00:07:18,000 +This is my r3, this is my r4, + +179 +00:07:18,000 --> 00:07:21,000 +and I can just say this is multiplication. + +180 +00:07:21,000 --> 00:07:23,000 +Okay, so multiplying number, + +181 +00:07:23,000 --> 00:07:27,000 +like let's say is five and three, okay? + +182 +00:07:27,000 --> 00:07:29,000 +And then dividing the number, + +183 +00:07:29,000 --> 00:07:33,000 +let's say 15 by 4. + +184 +00:07:33,000 --> 00:07:36,000 +Let's do this, and now when I perform this operation, + +185 +00:07:36,000 --> 00:07:39,000 +of course, I want to print r3 and r4 as well + +186 +00:07:39,000 --> 00:07:40,000 +on the same line. + +187 +00:07:40,000 --> 00:07:43,000 +I'm lazy to write multiple lines of code here, and r4. + +188 +00:07:43,000 --> 00:07:46,000 +So we are printing everything in the same line. + +189 +00:07:46,000 --> 00:07:48,000 +Let's try what happens. + +190 +00:07:48,000 --> 00:07:51,000 +Compile, no problem. Run, it works. + +191 +00:07:51,000 --> 00:07:54,000 +Can see that addition is nine, subtraction is four, + +192 +00:07:54,000 --> 00:07:56,000 +multiplication is 15, if I'm not wrong, yeah, + +193 +00:07:56,000 --> 00:07:58,000 +and division is three. + +194 +00:07:58,000 --> 00:08:02,000 +So 15 divided by four will give you a quotient of three. + +195 +00:08:02,000 --> 00:08:03,000 +Okay, cool, this is working right? + +196 +00:08:03,000 --> 00:08:06,000 +So this is how we use inheritance. + +197 +00:08:06,000 --> 00:08:10,000 +So important thing here is this advanced calculator + +198 +00:08:10,000 --> 00:08:11,000 +here is called a subclass. + +199 +00:08:11,000 --> 00:08:14,000 +This calculator here is called a superclass. + +200 +00:08:14,000 --> 00:08:18,000 +And to inherit, we use a keyword called extends, okay? + +201 +00:08:18,000 --> 00:08:22,000 +And this is basically a relationship of is-a. + +202 +00:08:22,000 --> 00:08:26,000 +Advanced calculator is a calculator, + +203 +00:08:26,000 --> 00:08:28,000 +and that's why we say is, an important word there. + +204 +00:08:28,000 --> 00:08:31,000 +Cool, can I have multiple classes in hitting each other? + +205 +00:08:31,000 --> 00:08:33,000 +We can, that's we'll see in the upcoming videos. + +206 +00:08:33,000 --> 00:08:36,000 +At this point, we just went for a simple inheritance + +207 +00:08:36,000 --> 00:08:39,000 +where we just use the feature of a superclass. + +208 +00:08:39,000 --> 00:08:41,000 +You can say parent has all these two features, + +209 +00:08:41,000 --> 00:08:44,000 +child is just extending it or using it. + +210 +00:08:44,000 --> 00:08:46,000 +I can say this is nepotism. + +211 +00:08:46,000 --> 00:08:47,000 +(laughs) So that's it from this video. + +212 +00:08:47,000 --> 00:08:50,000 +I hope you enjoyed and see you in the next video. + diff --git a/02 - Core Java/052 Need of Inheritance_en.srt b/02 - Core Java/052 Need of Inheritance_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..9ef9e3f5c50b2793ab0b43b98b44c161f17e2665 --- /dev/null +++ b/02 - Core Java/052 Need of Inheritance_en.srt @@ -0,0 +1,664 @@ +1 +00:00:00,000 --> 00:00:03,000 +(bright music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now let's talk more about OOPs concept. + +3 +00:00:05,000 --> 00:00:09,000 +Now, OOPs basically stands for Object Oriented Programming. + +4 +00:00:09,000 --> 00:00:10,000 +Now, in Object Oriented Programming, + +5 +00:00:10,000 --> 00:00:14,000 +we have talked about Object Class Encapsulation, right? + +6 +00:00:14,000 --> 00:00:16,000 +Now let's talk more concepts. + +7 +00:00:16,000 --> 00:00:17,000 +Now, one of the most important thing + +8 +00:00:17,000 --> 00:00:21,000 +in the concept of OOPs is called inheritance. + +9 +00:00:21,000 --> 00:00:23,000 +Now, what is inheritance? + +10 +00:00:23,000 --> 00:00:25,000 +Now, there are two words actually here, + +11 +00:00:25,000 --> 00:00:30,000 +which will confuse people is, "is" and "has." + +12 +00:00:30,000 --> 00:00:32,000 +Now what these two words are. + +13 +00:00:32,000 --> 00:00:33,000 +Now, if we talk about a computer, + +14 +00:00:33,000 --> 00:00:36,000 +now, of course, computer itself is abstract concept, right? + +15 +00:00:36,000 --> 00:00:39,000 +Now a computer can be a laptop, a computer can be a desktop, + +16 +00:00:39,000 --> 00:00:41,000 +a computer can be a mobile phone. + +17 +00:00:41,000 --> 00:00:42,000 +That's right. + +18 +00:00:42,000 --> 00:00:43,000 +So even if you talk about this phone, + +19 +00:00:43,000 --> 00:00:47,000 +this is a computer in the form of mobile phone, of course. + +20 +00:00:47,000 --> 00:00:49,000 +And the computer itself is an abstract concept. + +21 +00:00:49,000 --> 00:00:50,000 +No one owns a computer. + +22 +00:00:50,000 --> 00:00:52,000 +You basically buy a hardware, + +23 +00:00:52,000 --> 00:00:54,000 +which can be a desktop, laptop, or a phone. + +24 +00:00:54,000 --> 00:00:57,000 +Now, can I say that if you talk about a desktop, + +25 +00:00:57,000 --> 00:00:58,000 +so I'm talking about a desktop, which is this big, + +26 +00:00:58,000 --> 00:01:01,000 +and it will have certain parts, right? + +27 +00:01:01,000 --> 00:01:02,000 +A desktop will have a motherboard, + +28 +00:01:02,000 --> 00:01:05,000 +a desktop will have a hard drive, CPU, ram. + +29 +00:01:05,000 --> 00:01:06,000 +So when you talk about this thing, + +30 +00:01:06,000 --> 00:01:10,000 +it is desktop has a motherboard, + +31 +00:01:10,000 --> 00:01:14,000 +desktop has a hard drive, it has a CPU, right? + +32 +00:01:14,000 --> 00:01:16,000 +So the important keyword there is "has." + +33 +00:01:16,000 --> 00:01:19,000 +If we talk about a house, a house will have a TV, + +34 +00:01:19,000 --> 00:01:22,000 +a house will have a sofa, AC, bed. + +35 +00:01:22,000 --> 00:01:25,000 +So all those things, what a house has, right? + +36 +00:01:25,000 --> 00:01:27,000 +So "has" is an important word. + +37 +00:01:27,000 --> 00:01:28,000 +But at this point, let's not focus on "has." + +38 +00:01:28,000 --> 00:01:30,000 +In this video, let's talk about "is." + +39 +00:01:30,000 --> 00:01:32,000 +Now what is, is, is. + +40 +00:01:32,000 --> 00:01:34,000 +Okay, so example, if we talk about a laptop, + +41 +00:01:34,000 --> 00:01:38,000 +so can we say a laptop is a computer? + +42 +00:01:38,000 --> 00:01:42,000 +So what it means is, laptop got it features from a computer. + +43 +00:01:42,000 --> 00:01:44,000 +So can we take another example here? + +44 +00:01:44,000 --> 00:01:46,000 +If we talk about a car, let's say. + +45 +00:01:46,000 --> 00:01:48,000 +Let's say if we talk about a Fortuner, + +46 +00:01:48,000 --> 00:01:49,000 +which is very famous in India. + +47 +00:01:49,000 --> 00:01:51,000 +So this car is actually a car, right? + +48 +00:01:51,000 --> 00:01:54,000 +So Fortuner is a car. + +49 +00:01:54,000 --> 00:01:55,000 +So that's how you represent something. + +50 +00:01:55,000 --> 00:01:58,000 +You represent a device or a physical entity + +51 +00:01:58,000 --> 00:02:00,000 +by referring to its upper model, + +52 +00:02:00,000 --> 00:02:02,000 +or you try to take the features of it. + +53 +00:02:02,000 --> 00:02:04,000 +Now, coming back to the Inheritance concept, + +54 +00:02:04,000 --> 00:02:06,000 +in real world example, + +55 +00:02:06,000 --> 00:02:08,000 +let's say if we talk about the human world, + +56 +00:02:08,000 --> 00:02:10,000 +you know, when I was in my 10th standard, + +57 +00:02:10,000 --> 00:02:12,000 +so just for the example, okay, not that important, + +58 +00:02:12,000 --> 00:02:16,000 +but when I was in my 10th standard, I did my 10th in 2005. + +59 +00:02:16,000 --> 00:02:20,000 +At that point, Nokia released a lot of smartphones, right? + +60 +00:02:20,000 --> 00:02:21,000 +Of course, Nokia is not there anymore, + +61 +00:02:21,000 --> 00:02:25,000 +but Nokia was one of the most important factor + +62 +00:02:25,000 --> 00:02:26,000 +in the smartphone industry. + +63 +00:02:26,000 --> 00:02:28,000 +Now, when they came up with a smartphone, + +64 +00:02:28,000 --> 00:02:31,000 +one of the most famous smartphone which they built is 6600. + +65 +00:02:31,000 --> 00:02:33,000 +I'm not sure if you have seen that. + +66 +00:02:33,000 --> 00:02:34,000 +I'm not even sure if you're that old. + +67 +00:02:34,000 --> 00:02:37,000 +Maybe you were not even born in that year. + +68 +00:02:37,000 --> 00:02:39,000 +But yeah, so it was 6600. Amazing phone. + +69 +00:02:39,000 --> 00:02:42,000 +If you can just search on Google, Nokia 6600, + +70 +00:02:42,000 --> 00:02:44,000 +you will see that phone. + +71 +00:02:44,000 --> 00:02:46,000 +Now, a lot of my friends were having that phone. + +72 +00:02:46,000 --> 00:02:47,000 +It was costly phone at that point. + +73 +00:02:47,000 --> 00:02:49,000 +It was around 10,000 rupees. + +74 +00:02:49,000 --> 00:02:51,000 +And I was not having any phone. + +75 +00:02:51,000 --> 00:02:52,000 +So they used to ask me, "Hey, which phone you have?" + +76 +00:02:52,000 --> 00:02:54,000 +I used to say, "Hey, I don't have a phone." + +77 +00:02:54,000 --> 00:02:58,000 +But then I realized, hey, till when I will say that. + +78 +00:02:58,000 --> 00:02:59,000 +The thing is, + +79 +00:02:59,000 --> 00:03:01,000 +next time when people used to ask me which phone I have, + +80 +00:03:01,000 --> 00:03:04,000 +I used to say I have 3310. + +81 +00:03:04,000 --> 00:03:05,000 +Now, I was not having any phone. + +82 +00:03:05,000 --> 00:03:07,000 +Now, this is my father's phone, + +83 +00:03:07,000 --> 00:03:09,000 +and we know the concept, right? + +84 +00:03:09,000 --> 00:03:10,000 +Father's phone is yours phone. + +85 +00:03:10,000 --> 00:03:12,000 +In fact, in general, we say, + +86 +00:03:12,000 --> 00:03:14,000 +our parents' property, it's your property. + +87 +00:03:14,000 --> 00:03:16,000 +Whatever they own, it's yours. + +88 +00:03:16,000 --> 00:03:17,000 +I don't believe in that, okay? + +89 +00:03:17,000 --> 00:03:20,000 +I'm not a big fan of inheritance or nepotism, + +90 +00:03:20,000 --> 00:03:22,000 +but yeah, you get everything. + +91 +00:03:22,000 --> 00:03:24,000 +So I used to say I have that phone. + +92 +00:03:24,000 --> 00:03:25,000 +Okay, so this is inheritance. + +93 +00:03:25,000 --> 00:03:27,000 +Now, how will you use this concept in programming? + +94 +00:03:27,000 --> 00:03:29,000 +Now in Java, what you have + +95 +00:03:29,000 --> 00:03:30,000 +is you have different classes, right? + +96 +00:03:30,000 --> 00:03:32,000 +Example, if you create a simple class here. + +97 +00:03:32,000 --> 00:03:33,000 +Let me get a very simple class. + +98 +00:03:33,000 --> 00:03:36,000 +Let's say we have a class here called Calculator. + +99 +00:03:36,000 --> 00:03:38,000 +Now what features a calculator will have? + +100 +00:03:38,000 --> 00:03:41,000 +A calculator will have some variables here, + +101 +00:03:41,000 --> 00:03:42,000 +and then it will have some methods. + +102 +00:03:42,000 --> 00:03:46,000 +Let's say method is add, a method is sub, okay? + +103 +00:03:46,000 --> 00:03:48,000 +And then method is multi. + +104 +00:03:48,000 --> 00:03:50,000 +So we have different methods here. + +105 +00:03:50,000 --> 00:03:51,000 +And let's also have division. + +106 +00:03:51,000 --> 00:03:53,000 +Why division should be left there? + +107 +00:03:53,000 --> 00:03:56,000 +So we have all these methods, right? And it is working. + +108 +00:03:56,000 --> 00:03:58,000 +So you can basically create object of Calculator, + +109 +00:03:58,000 --> 00:03:59,000 +and you can access all these methods. + +110 +00:03:59,000 --> 00:04:00,000 +No harm in that. + +111 +00:04:00,000 --> 00:04:04,000 +But what if you want to create a advanced calculator? + +112 +00:04:04,000 --> 00:04:06,000 +Now of course, let's say if someone comes to you + +113 +00:04:06,000 --> 00:04:09,000 +and say, "Hey, you know, you made a awesome calculator + +114 +00:04:09,000 --> 00:04:12,000 +and now I want you to create a scientific calculator + +115 +00:04:12,000 --> 00:04:14,000 +or advanced calculator." + +116 +00:04:14,000 --> 00:04:15,000 +Now you have a choice. + +117 +00:04:15,000 --> 00:04:18,000 +You can edit the same class, but there's only one thing. + +118 +00:04:18,000 --> 00:04:20,000 +What if you enhance the same class? + +119 +00:04:20,000 --> 00:04:21,000 +You add some more methods here. + +120 +00:04:21,000 --> 00:04:25,000 +Now you want to give Calculator to someone, + +121 +00:04:25,000 --> 00:04:27,000 +someone asking you for the Calculator object. + +122 +00:04:27,000 --> 00:04:28,000 +They basically access all the basic features + +123 +00:04:28,000 --> 00:04:30,000 +and advanced features, as well. + +124 +00:04:30,000 --> 00:04:33,000 +So it is good to create a separate class, okay? + +125 +00:04:33,000 --> 00:04:34,000 +So you can get a separate class, + +126 +00:04:34,000 --> 00:04:37,000 +and you can say Advanced Calculator, right? + +127 +00:04:37,000 --> 00:04:40,000 +And then this will have all the advanced features. + +128 +00:04:40,000 --> 00:04:41,000 +Of course, it will have all the basic features, + +129 +00:04:41,000 --> 00:04:43,000 +and it will have the advanced features, as well. + +130 +00:04:43,000 --> 00:04:46,000 +So we have basic features, advanced features both. + +131 +00:04:46,000 --> 00:04:47,000 +But don't you think + +132 +00:04:47,000 --> 00:04:51,000 +you need to define these basic features again and again? + +133 +00:04:51,000 --> 00:04:54,000 +Why? We already have those features here, right? + +134 +00:04:54,000 --> 00:04:56,000 +We already have those features here. + +135 +00:04:56,000 --> 00:05:01,000 +Can I just use those features in my new Advanced Calculator? + +136 +00:05:02,000 --> 00:05:04,000 +So what I'm saying is basically + +137 +00:05:04,000 --> 00:05:07,000 +this Advanced Calculator is going to inherit + +138 +00:05:07,000 --> 00:05:12,000 +the properties and features from Calculator. + +139 +00:05:12,000 --> 00:05:15,000 +So I can also say Advanced Calculator is a Calculator, + +140 +00:05:15,000 --> 00:05:17,000 +and it will get all the features of Calculator. + +141 +00:05:17,000 --> 00:05:20,000 +Okay, it will make much more sense once we start coding. + +142 +00:05:20,000 --> 00:05:24,000 +But what we are doing here is, we are doing inheritance. + +143 +00:05:24,000 --> 00:05:27,000 +So basically, inheritance is a very important feature, + +144 +00:05:27,000 --> 00:05:29,000 +using which you can just copy the feature + +145 +00:05:29,000 --> 00:05:31,000 +from the upper class. + +146 +00:05:31,000 --> 00:05:32,000 +Can I say this is a parent class? + +147 +00:05:32,000 --> 00:05:37,000 +So this is a parent class, and this is a child class + +148 +00:05:37,000 --> 00:05:39,000 +because child class is accessing + +149 +00:05:39,000 --> 00:05:41,000 +the feature of parent class, okay? + +150 +00:05:41,000 --> 00:05:44,000 +So whatever I earn, it's my money, + +151 +00:05:44,000 --> 00:05:45,000 +plus my parent's money is my money. + +152 +00:05:45,000 --> 00:05:46,000 +That's how the system works, right? + +153 +00:05:46,000 --> 00:05:49,000 +We can also say this is a super class. + +154 +00:05:49,000 --> 00:05:51,000 +We have different words for it. That's it. + +155 +00:05:51,000 --> 00:05:54,000 +And we can say this is a subclass. + +156 +00:05:54,000 --> 00:05:57,000 +We can also say this is a base class, base. + +157 +00:05:57,000 --> 00:06:00,000 +And this is a derived class. + +158 +00:06:00,000 --> 00:06:02,000 +So it depends upon how you represent that. + +159 +00:06:02,000 --> 00:06:04,000 +If you say this is a parent class, + +160 +00:06:04,000 --> 00:06:06,000 +then you have to say it's a child class. + +161 +00:06:06,000 --> 00:06:09,000 +If you say that's a super class, then it becomes a subclass. + +162 +00:06:09,000 --> 00:06:09,000 +If you say this is a base class, + +163 +00:06:09,000 --> 00:06:11,000 +you have to say this is a derived class. + +164 +00:06:11,000 --> 00:06:13,000 +That's how it works, okay? + +165 +00:06:13,000 --> 00:06:16,000 +Now, this is what inheritance is. + +166 +00:06:16,000 --> 00:06:19,000 +Now we'll try to implement this in code in the next video. + diff --git a/02 - Core Java/052 Source-code.url b/02 - Core Java/052 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/052 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/053 Single and Multilevel inheritance_en.srt b/02 - Core Java/053 Single and Multilevel inheritance_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..dd9ea35144928186d03647e650cb7e93fc926e39 --- /dev/null +++ b/02 - Core Java/053 Single and Multilevel inheritance_en.srt @@ -0,0 +1,396 @@ +1 +00:00:00,000 --> 00:00:03,000 +(piano music opening) + +2 +00:00:03,000 --> 00:00:06,000 +-: So basically we were able to do advanced calculator, + +3 +00:00:06,000 --> 00:00:08,000 +which uses a feature of calculator, right? + +4 +00:00:08,000 --> 00:00:10,000 +So this is extends. + +5 +00:00:10,000 --> 00:00:12,000 +Now what if in future, + +6 +00:00:12,000 --> 00:00:15,000 +let's say we got one more requirement, we want to build a, + +7 +00:00:15,000 --> 00:00:16,000 +let's say scientific calculator + +8 +00:00:16,000 --> 00:00:17,000 +and I don't know the operation, + +9 +00:00:17,000 --> 00:00:20,000 +so I will just say Very Advanced Calculator. + +10 +00:00:20,000 --> 00:00:23,000 +I'm very bad with names, so I will just use this + +11 +00:00:23,000 --> 00:00:26,000 +and this advanced calculator will have extra feature. + +12 +00:00:26,000 --> 00:00:27,000 +Let's say it also has a feature + +13 +00:00:27,000 --> 00:00:31,000 +of finding the power of a number. + +14 +00:00:31,000 --> 00:00:33,000 +Okay? So let's say public int, + +15 +00:00:33,000 --> 00:00:34,000 +I can simply say, yeah, power. + +16 +00:00:34,000 --> 00:00:37,000 +Now what we power, let's say three is two. I wanna do that. + +17 +00:00:37,000 --> 00:00:38,000 +So it'll take two numbers. + +18 +00:00:38,000 --> 00:00:40,000 +The first value N1, + +19 +00:00:40,000 --> 00:00:43,000 +and I also want a power, let's say N2 + +20 +00:00:43,000 --> 00:00:44,000 +and let's say this will return, okay? + +21 +00:00:44,000 --> 00:00:47,000 +How do we do the power? So basically we have a math class. + +22 +00:00:47,000 --> 00:00:49,000 +I can just simply use it. + +23 +00:00:49,000 --> 00:00:52,000 +Math., yeah, we have a function of power. + +24 +00:00:52,000 --> 00:00:55,000 +Just take double value, but that's fine. I will just pass. + +25 +00:00:55,000 --> 00:00:57,000 +Integer double also takes integer value. + +26 +00:00:57,000 --> 00:00:58,000 +N1, N2. + +27 +00:00:58,000 --> 00:01:01,000 +And the problem is it also returns the double value. + +28 +00:01:01,000 --> 00:01:03,000 +If you can see, power returns are double value. + +29 +00:01:03,000 --> 00:01:06,000 +So what I will do is, let me return double value, okay? + +30 +00:01:06,000 --> 00:01:08,000 +So you have a choice, you can return double value + +31 +00:01:08,000 --> 00:01:12,000 +or you can just typecast it with int and you can do that. + +32 +00:01:12,000 --> 00:01:13,000 +Otherwise double is okay. + +33 +00:01:13,000 --> 00:01:14,000 +So you can see we have a power function, + +34 +00:01:14,000 --> 00:01:17,000 +which takes this method and it calculates. + +35 +00:01:17,000 --> 00:01:19,000 +Now the thing is, if I want to create + +36 +00:01:19,000 --> 00:01:21,000 +object of power, well I will do that. + +37 +00:01:21,000 --> 00:01:22,000 +I will just create that in demo. + +38 +00:01:22,000 --> 00:01:24,000 +I will say, hey, if someone wants + +39 +00:01:24,000 --> 00:01:28,000 +to try very advanced calculator, they can do that. + +40 +00:01:28,000 --> 00:01:30,000 +Okay? It is just that very advanced calculator. + +41 +00:01:30,000 --> 00:01:33,000 +If you can see, we don't have any of the methods. Okay? + +42 +00:01:33,000 --> 00:01:36,000 +Now that's an issue right? + +43 +00:01:36,000 --> 00:01:38,000 +Now I know in your mind you have a solution, right? + +44 +00:01:38,000 --> 00:01:40,000 +You have a choice. You can just add, + +45 +00:01:40,000 --> 00:01:42,000 +you can just type all these methods here + +46 +00:01:42,000 --> 00:01:46,000 +or we can come back here and say extends. + +47 +00:01:46,000 --> 00:01:47,000 +Okay? Now it'll extends what?. + +48 +00:01:47,000 --> 00:01:49,000 +We can also say calculator, + +49 +00:01:49,000 --> 00:01:52,000 +but then this will only have addition, subtraction, + +50 +00:01:52,000 --> 00:01:53,000 +division, multiplication, + +51 +00:01:53,000 --> 00:01:55,000 +but not the feature of advanced calculator, + +52 +00:01:55,000 --> 00:01:57,000 +which is, oh, it'll not have this two as well, + +53 +00:01:57,000 --> 00:02:00,000 +because this two is there in advanced calculator, + +54 +00:02:00,000 --> 00:02:01,000 +it'll only have add and sub. + +55 +00:02:01,000 --> 00:02:04,000 +You can see multi and division not found. + +56 +00:02:04,000 --> 00:02:05,000 +In fact, you know, I should also have R3. + +57 +00:02:05,000 --> 00:02:09,000 +In fact, this should be of type double, double R5 equal + +58 +00:02:09,000 --> 00:02:14,000 +to OBJ.power by passing two values. + +59 +00:02:14,000 --> 00:02:16,000 +One is, let's say four (indistinct), + +60 +00:02:16,000 --> 00:02:19,000 +at least we know the output should be 16. + +61 +00:02:19,000 --> 00:02:22,000 +And I also want to print R5. + +62 +00:02:22,000 --> 00:02:24,000 +Oh, we are printing everything in one line. Great. + +63 +00:02:24,000 --> 00:02:26,000 +And you can see multi and division are not working. + +64 +00:02:26,000 --> 00:02:28,000 +So if you want to make it work, + +65 +00:02:28,000 --> 00:02:29,000 +you can't just extends calculator, you have + +66 +00:02:29,000 --> 00:02:32,000 +to extends advanced calculator. + +67 +00:02:32,000 --> 00:02:34,000 +So you can see we have multiple hierarchy here. + +68 +00:02:34,000 --> 00:02:35,000 +So very advanced calculator is + +69 +00:02:35,000 --> 00:02:37,000 +extending advanced calculator. + +70 +00:02:37,000 --> 00:02:39,000 +Advanced calculator is extending calculator. + +71 +00:02:39,000 --> 00:02:42,000 +So if you try to compile this code, let's say if this works, + +72 +00:02:42,000 --> 00:02:45,000 +compile and run, you can see it's working. + +73 +00:02:45,000 --> 00:02:48,000 +You got 16 at the end, you got 0.0 + +74 +00:02:48,000 --> 00:02:51,000 +because we are returning a double value. + +75 +00:02:51,000 --> 00:02:53,000 +This works. So if you're trying to imagine this, + +76 +00:02:53,000 --> 00:02:57,000 +what we are doing is, so we have a calculator + +77 +00:02:57,000 --> 00:03:00,000 +and then advanced calculator is extending calculator. + +78 +00:03:00,000 --> 00:03:04,000 +And then we got very advanced calculator is extending + +79 +00:03:04,000 --> 00:03:05,000 +from advanced calculator. + +80 +00:03:05,000 --> 00:03:08,000 +Now, this is when you are saying multiple, + +81 +00:03:08,000 --> 00:03:10,000 +where we are doing inheritance, right? + +82 +00:03:10,000 --> 00:03:14,000 +So this is called multilevel inheritance. + +83 +00:03:14,000 --> 00:03:17,000 +So basically we have multilevel inheritance here + +84 +00:03:17,000 --> 00:03:18,000 +because we have multiple level, + +85 +00:03:18,000 --> 00:03:22,000 +but in the early example we were having a just two, right? + +86 +00:03:22,000 --> 00:03:23,000 +So we were having this two. + +87 +00:03:23,000 --> 00:03:26,000 +Now when you say only two classes, this is called, + +88 +00:03:27,000 --> 00:03:30,000 +single level inheritance, + +89 +00:03:30,000 --> 00:03:32,000 +and I know I became a doctor there, + +90 +00:03:32,000 --> 00:03:34,000 +but you got the point, right? + +91 +00:03:34,000 --> 00:03:35,000 +So if you have multiple layers, + +92 +00:03:35,000 --> 00:03:37,000 +that is multi-level inheritance. + +93 +00:03:37,000 --> 00:03:39,000 +And when you say single, that is single, + +94 +00:03:39,000 --> 00:03:40,000 +when you have only two classes. + +95 +00:03:40,000 --> 00:03:44,000 +Now the very advanced calculator here is taking the feature + +96 +00:03:44,000 --> 00:03:45,000 +from advanced calculator. + +97 +00:03:45,000 --> 00:03:47,000 +Advanced calculator is taking the feature from calculator. + +98 +00:03:47,000 --> 00:03:50,000 +So by default, all the calc. stuff will come to + +99 +00:03:50,000 --> 00:03:52,000 +very advanced and that is multi-level. + diff --git a/02 - Core Java/053 Source-code.url b/02 - Core Java/053 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/053 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/054 Multiple Inheritance_en.srt b/02 - Core Java/054 Multiple Inheritance_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..603d0126f08cad19306cacb231143a2c27e50734 --- /dev/null +++ b/02 - Core Java/054 Multiple Inheritance_en.srt @@ -0,0 +1,656 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle piano music) + +2 +00:00:03,000 --> 00:00:04,000 +-: So in the last video we have talked + +3 +00:00:04,000 --> 00:00:06,000 +about what is single level inheritance, + +4 +00:00:06,000 --> 00:00:08,000 +and then we have talked about multi-level, right? + +5 +00:00:08,000 --> 00:00:09,000 +Now what is multi-level? + +6 +00:00:09,000 --> 00:00:11,000 +So when you have the class, + +7 +00:00:11,000 --> 00:00:13,000 +which is getting inherited by someone else + +8 +00:00:13,000 --> 00:00:15,000 +and then something else is getting inherited + +9 +00:00:15,000 --> 00:00:16,000 +by something else. + +10 +00:00:16,000 --> 00:00:18,000 +It is multi-level. + +11 +00:00:18,000 --> 00:00:21,000 +Now, can we have a different structure here? + +12 +00:00:21,000 --> 00:00:22,000 +Let's take one more example. + +13 +00:00:22,000 --> 00:00:26,000 +So if you say we have A, we have B and we have C. + +14 +00:00:26,000 --> 00:00:28,000 +Now this is multi-level, right? + +15 +00:00:28,000 --> 00:00:31,000 +And of course when you have A and B, that's it. + +16 +00:00:31,000 --> 00:00:33,000 +This is single level. + +17 +00:00:33,000 --> 00:00:37,000 +But what if you have A and B, and then you have C, + +18 +00:00:37,000 --> 00:00:41,000 +where your A is your parent. + +19 +00:00:41,000 --> 00:00:42,000 +In fact, in this situation as well, + +20 +00:00:42,000 --> 00:00:44,000 +if you try to expand this one. + +21 +00:00:44,000 --> 00:00:49,000 +Here as well, we have this A as a parent, + +22 +00:00:50,000 --> 00:00:53,000 +and then this B becomes a child. + +23 +00:00:53,000 --> 00:00:55,000 +Now, B becomes child for A, + +24 +00:00:55,000 --> 00:00:59,000 +but then B is also a parent for C, right? + +25 +00:00:59,000 --> 00:01:00,000 +So C becomes child here. + +26 +00:01:00,000 --> 00:01:04,000 +Now, in the same way, if we talk about this A, B, C here. + +27 +00:01:04,000 --> 00:01:06,000 +We are saying both are parent, + +28 +00:01:06,000 --> 00:01:10,000 +so we have A as parent and B as parent, + +29 +00:01:10,000 --> 00:01:14,000 +and then C becomes a child. + +30 +00:01:14,000 --> 00:01:17,000 +So C is from A and B, both. + +31 +00:01:17,000 --> 00:01:19,000 +Now is it possible in Java? + +32 +00:01:19,000 --> 00:01:20,000 +Let's try. + +33 +00:01:20,000 --> 00:01:22,000 +In fact, it is possible in other language like C++. + +34 +00:01:22,000 --> 00:01:24,000 +Let's try in Java. + +35 +00:01:24,000 --> 00:01:26,000 +What I will do is I will just go back to the same class, + +36 +00:01:26,000 --> 00:01:28,000 +okay, same file, demo. + +37 +00:01:28,000 --> 00:01:30,000 +And here let me get a class called A. + +38 +00:01:30,000 --> 00:01:32,000 +And of course A will have some methods. + +39 +00:01:32,000 --> 00:01:34,000 +This should be capital A + +40 +00:01:34,000 --> 00:01:38,000 +and then class B and class C. + +41 +00:01:38,000 --> 00:01:42,000 +Okay, we have this three classes here, okay? + +42 +00:01:42,000 --> 00:01:46,000 +And let's say, okay, let's try this first. + +43 +00:01:46,000 --> 00:01:51,000 +If B extends from A and C extends from B, okay, this works. + +44 +00:01:54,000 --> 00:01:56,000 +We can see there's no problem here, + +45 +00:01:56,000 --> 00:02:01,000 +but what if I want the C to extends from A and B both. + +46 +00:02:01,000 --> 00:02:04,000 +As you can see, this syntax is not working. + +47 +00:02:04,000 --> 00:02:05,000 +It gives you an error as well. + +48 +00:02:05,000 --> 00:02:09,000 +You can't put a comma when you say extends A comma B, okay? + +49 +00:02:09,000 --> 00:02:12,000 +Does it means we can use extend B? + +50 +00:02:12,000 --> 00:02:13,000 +No, even this will not work. + +51 +00:02:13,000 --> 00:02:16,000 +So it doesn't matter what combination you do, + +52 +00:02:16,000 --> 00:02:20,000 +multiple inheritance in Java will not work. + +53 +00:02:20,000 --> 00:02:20,000 +Yeah. + +54 +00:02:20,000 --> 00:02:21,000 +So this thing will not work. + +55 +00:02:21,000 --> 00:02:24,000 +Multiple inheritance will not work. + +56 +00:02:24,000 --> 00:02:25,000 +But the question is why? + +57 +00:02:25,000 --> 00:02:27,000 +So let's try to understand that here. + +58 +00:02:27,000 --> 00:02:28,000 +Now, let's say if we go back + +59 +00:02:28,000 --> 00:02:30,000 +to this particular example here, + +60 +00:02:30,000 --> 00:02:33,000 +let's say you create object of B, okay? + +61 +00:02:33,000 --> 00:02:36,000 +In this scenario, if you create object of B, let's say obj. + +62 +00:02:36,000 --> 00:02:41,000 +Now, obj can call all the methods of B and A both. + +63 +00:02:41,000 --> 00:02:43,000 +So let's say in B, we have a method called show. + +64 +00:02:43,000 --> 00:02:45,000 +Okay, let's say there's a show method, + +65 +00:02:45,000 --> 00:02:49,000 +and then from obj, you are trying to call show. + +66 +00:02:49,000 --> 00:02:50,000 +Will this work? + +67 +00:02:50,000 --> 00:02:51,000 +Yes, this will work. + +68 +00:02:51,000 --> 00:02:55,000 +Let's say in A, we have a method called abc. + +69 +00:02:55,000 --> 00:02:59,000 +Now, if you try to call obj.abc, yes, this will work, + +70 +00:02:59,000 --> 00:03:02,000 +but what happens, you know when you say obj.abc, + +71 +00:03:02,000 --> 00:03:06,000 +it'll try to search for it first in the B class. + +72 +00:03:06,000 --> 00:03:07,000 +In B class, we don't have it. + +73 +00:03:07,000 --> 00:03:09,000 +So it'll say, okay, I don't have that method. + +74 +00:03:09,000 --> 00:03:11,000 +Maybe my parent class will have, + +75 +00:03:11,000 --> 00:03:13,000 +so it'll go to A by checking, + +76 +00:03:13,000 --> 00:03:14,000 +"Hey, do you have abc?" + +77 +00:03:14,000 --> 00:03:16,000 +Parent will say, "Yes, I have." + +78 +00:03:16,000 --> 00:03:18,000 +Now, in this case it'll work. + +79 +00:03:18,000 --> 00:03:20,000 +The same thing is applicable for C as well. + +80 +00:03:20,000 --> 00:03:23,000 +Let's say if you create object of C, which is obj again. + +81 +00:03:23,000 --> 00:03:26,000 +And then if you try to call a method, + +82 +00:03:26,000 --> 00:03:28,000 +let's say, A has a method called x, + +83 +00:03:28,000 --> 00:03:31,000 +B has a method called y and C has a method called z. + +84 +00:03:31,000 --> 00:03:33,000 +So let's say we have z method. + +85 +00:03:33,000 --> 00:03:35,000 +and then when you try to call z, this will work. + +86 +00:03:35,000 --> 00:03:37,000 +Because in C itself, we have the method. + +87 +00:03:37,000 --> 00:03:42,000 +If you say obj.y, will this work? + +88 +00:03:42,000 --> 00:03:44,000 +Yes, because it is coming from B. + +89 +00:03:44,000 --> 00:03:47,000 +Now, when you say obj.x, + +90 +00:03:47,000 --> 00:03:50,000 +it'll also work because it'll call from A. + +91 +00:03:50,000 --> 00:03:51,000 +So first it'll search in C. + +92 +00:03:51,000 --> 00:03:53,000 +In C, we don't have x, it'll go to B. + +93 +00:03:53,000 --> 00:03:55,000 +In B, also we don't have x. + +94 +00:03:55,000 --> 00:03:58,000 +It'll go to A, yes in A, we have x. + +95 +00:03:58,000 --> 00:04:00,000 +Okay, it works. + +96 +00:04:00,000 --> 00:04:02,000 +But now if you come back to this example, + +97 +00:04:02,000 --> 00:04:04,000 +let's say if you get object of C as obj. + +98 +00:04:04,000 --> 00:04:07,000 +So let's say this is C object, okay? + +99 +00:04:07,000 --> 00:04:10,000 +And then let's say in this A, + +100 +00:04:10,000 --> 00:04:12,000 +we have a method called show, okay? + +101 +00:04:12,000 --> 00:04:14,000 +And that's how we're working. + +102 +00:04:14,000 --> 00:04:17,000 +So let's say if you call show now, + +103 +00:04:17,000 --> 00:04:20,000 +now of course C will say, "Okay, I don't have a method show. + +104 +00:04:20,000 --> 00:04:22,000 +Will this work? Let me ask my parent." + +105 +00:04:22,000 --> 00:04:23,000 +So it'll go to A, + +106 +00:04:23,000 --> 00:04:26,000 +A will say, "Okay, I have show, problem solved." + +107 +00:04:26,000 --> 00:04:27,000 +This will work, right? + +108 +00:04:27,000 --> 00:04:28,000 +In general, I'm not talking about Java. + +109 +00:04:28,000 --> 00:04:30,000 +Let's say in general, this is how it works. + +110 +00:04:30,000 --> 00:04:34,000 +Now, let's say we have, in B, we have a method called X. + +111 +00:04:34,000 --> 00:04:35,000 +Okay? + +112 +00:04:35,000 --> 00:04:38,000 +Now it'll say obj.x, will this work? + +113 +00:04:38,000 --> 00:04:39,000 +Now it'll search and C. + +114 +00:04:39,000 --> 00:04:40,000 +In C, we don't have it. + +115 +00:04:40,000 --> 00:04:42,000 +In A, also we don't have it, in B we have it. + +116 +00:04:42,000 --> 00:04:44,000 +So this will also work. + +117 +00:04:44,000 --> 00:04:47,000 +Okay, but what if there's a method called y, + +118 +00:04:47,000 --> 00:04:51,000 +which is there in A and B, both, okay? + +119 +00:04:51,000 --> 00:04:55,000 +And now, if you're trying to say obj.y, + +120 +00:04:55,000 --> 00:04:59,000 +the problem is from C, we don't have it, + +121 +00:04:59,000 --> 00:05:03,000 +and A and B both has this method, which one he will choose? + +122 +00:05:03,000 --> 00:05:05,000 +That's a confusion, right? + +123 +00:05:05,000 --> 00:05:07,000 +It is almost same like let's say if you want + +124 +00:05:07,000 --> 00:05:09,000 +to call someone, your phone is dead, + +125 +00:05:09,000 --> 00:05:11,000 +and then now you want to call. + +126 +00:05:11,000 --> 00:05:12,000 +So you can ask your parents. + +127 +00:05:12,000 --> 00:05:13,000 +And both the parents has a phone, + +128 +00:05:13,000 --> 00:05:15,000 +which phone you will choose? + +129 +00:05:15,000 --> 00:05:18,000 +Now, if you chose your dad's phone, + +130 +00:05:18,000 --> 00:05:19,000 +your mom will feel bad. + +131 +00:05:19,000 --> 00:05:22,000 +If you choose your mom's phone, your dad will feel bad. + +132 +00:05:22,000 --> 00:05:25,000 +Again, I'm just giving an example here. + +133 +00:05:25,000 --> 00:05:26,000 +I'm not saying, it's literally happens, + +134 +00:05:26,000 --> 00:05:27,000 +but, in fact, + +135 +00:05:27,000 --> 00:05:29,000 +they'll be happy that you're not using their phone. + +136 +00:05:29,000 --> 00:05:32,000 +But yeah, that's a confusion we have here right now. + +137 +00:05:32,000 --> 00:05:36,000 +This problem is called ambiguity problem. + +138 +00:05:36,000 --> 00:05:37,000 +Okay? + +139 +00:05:37,000 --> 00:05:38,000 +So ambiguity simply means, + +140 +00:05:38,000 --> 00:05:40,000 +we have a confusion here, which one to choose? + +141 +00:05:40,000 --> 00:05:42,000 +Now, this problem is there in multiple languages, + +142 +00:05:42,000 --> 00:05:44,000 +which supports multiple inheritance. + +143 +00:05:44,000 --> 00:05:47,000 +Java says, "Oh, that's a problem?" + +144 +00:05:47,000 --> 00:05:49,000 +Let's remove the future itself. + +145 +00:05:49,000 --> 00:05:50,000 +Okay? I know. + +146 +00:05:50,000 --> 00:05:52,000 +That's a weird way of solving the problem, + +147 +00:05:52,000 --> 00:05:55,000 +but yeah, they have removed the problem itself. + +148 +00:05:55,000 --> 00:05:57,000 +So it's not like they found a solution. + +149 +00:05:57,000 --> 00:05:58,000 +They said, + +150 +00:05:58,000 --> 00:06:00,000 +"Instead of finding a solution, let's remove the feature." + +151 +00:06:00,000 --> 00:06:05,000 +That's right, in Java, multiple inheritance does not work. + +152 +00:06:05,000 --> 00:06:08,000 +Okay, so it's a very famous interview question, + +153 +00:06:08,000 --> 00:06:10,000 +do we support multiple inheritance in Java? + +154 +00:06:10,000 --> 00:06:11,000 +And the answer is no. + +155 +00:06:11,000 --> 00:06:13,000 +There is a internet way, + +156 +00:06:13,000 --> 00:06:15,000 +not actually a way, + +157 +00:06:15,000 --> 00:06:18,000 +just that it is possible by going all through steps. + +158 +00:06:18,000 --> 00:06:21,000 +But yeah, that's how Java is implemented in new feed, + +159 +00:06:21,000 --> 00:06:25,000 +in new versions, they have done something which changes it, + +160 +00:06:25,000 --> 00:06:26,000 +with the help of interfaces + +161 +00:06:26,000 --> 00:06:27,000 +but we don't know interfaces yet. + +162 +00:06:27,000 --> 00:06:32,000 +So, let's say, Java does not support multiple inheritance. + +163 +00:06:33,000 --> 00:06:35,000 +Oh, okay, this makes sense. + +164 +00:06:35,000 --> 00:06:36,000 +See you in the next video. + diff --git a/02 - Core Java/054 Source-code.url b/02 - Core Java/054 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/054 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/055 Source-code.url b/02 - Core Java/055 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/055 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/055 This and super method_en.srt b/02 - Core Java/055 This and super method_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..78d7da59bd4e4ff435375f509288de3dae3f3531 --- /dev/null +++ b/02 - Core Java/055 This and super method_en.srt @@ -0,0 +1,1204 @@ +1 +00:00:00,000 --> 00:00:03,000 +(soft music plays) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, let's talk about this and super. + +3 +00:00:06,000 --> 00:00:09,000 +Now this is a very super concept, so let's focus, so + +4 +00:00:09,000 --> 00:00:10,000 +what I'll do is I'll just remove all this thing just + +5 +00:00:10,000 --> 00:00:13,000 +to make it simple and clear this part as well. + +6 +00:00:13,000 --> 00:00:18,000 +And for this, I don't even need this particular classes, + +7 +00:00:18,000 --> 00:00:21,000 +so let me just close these files and keep it simple. + +8 +00:00:21,000 --> 00:00:22,000 +Okay, so we have a very simple code here. + +9 +00:00:22,000 --> 00:00:25,000 +Now what I'll do is just to understand this + +10 +00:00:25,000 --> 00:00:28,000 +and Super, I will create a class called A, + +11 +00:00:28,000 --> 00:00:29,000 +I know this is a very simple name, + +12 +00:00:29,000 --> 00:00:32,000 +but just for the example we can take them + +13 +00:00:32,000 --> 00:00:34,000 +and then we got a class B. + +14 +00:00:34,000 --> 00:00:36,000 +Okay, we got two classes + +15 +00:00:36,000 --> 00:00:41,000 +and then I'm also saying that this B class extends A, + +16 +00:00:41,000 --> 00:00:44,000 +of course we are doing inheritance here and it makes sense + +17 +00:00:44,000 --> 00:00:46,000 +because we just talked about inheritance. + +18 +00:00:46,000 --> 00:00:48,000 +And then what I will do is I will create + +19 +00:00:48,000 --> 00:00:49,000 +a constructor for both. + +20 +00:00:49,000 --> 00:00:51,000 +Of course we can do that, right? + +21 +00:00:51,000 --> 00:00:53,000 +Now before creating a constructor, + +22 +00:00:53,000 --> 00:00:55,000 +let me say if I create object of B, + +23 +00:00:55,000 --> 00:00:57,000 +of course we can do that, right? + +24 +00:00:57,000 --> 00:01:00,000 +So this B class, we got an object of B class here + +25 +00:01:00,000 --> 00:01:01,000 +and we can do that. + +26 +00:01:01,000 --> 00:01:04,000 +Let me create a constructor of B here. + +27 +00:01:04,000 --> 00:01:06,000 +Now how do we create a constructor? It's very simple. + +28 +00:01:06,000 --> 00:01:09,000 +You specify public and then you say B, bracket open, + +29 +00:01:09,000 --> 00:01:13,000 +and then here I will type in B. + +30 +00:01:13,000 --> 00:01:14,000 +I'm just trying to keep it as simple as possible. + +31 +00:01:14,000 --> 00:01:16,000 +That's why we have taken class A and B + +32 +00:01:16,000 --> 00:01:18,000 +and then a very simple statement. + +33 +00:01:18,000 --> 00:01:20,000 +Now what I will do is I will try to compile this code + +34 +00:01:20,000 --> 00:01:22,000 +and you tell me what will be the output. + +35 +00:01:22,000 --> 00:01:23,000 +Compression done, right? + +36 +00:01:23,000 --> 00:01:26,000 +If I run this code, there will be some output. Right? + +37 +00:01:26,000 --> 00:01:28,000 +Now think about this: We are creating the object of B. + +38 +00:01:28,000 --> 00:01:32,000 +We are instantiating it and we have learned about this that + +39 +00:01:32,000 --> 00:01:33,000 +whenever you create the object, + +40 +00:01:33,000 --> 00:01:35,000 +it'll call the constructor of a class. + +41 +00:01:35,000 --> 00:01:37,000 +So the object we are getting is of B, + +42 +00:01:37,000 --> 00:01:40,000 +and we have a constructor, which is B here, + +43 +00:01:40,000 --> 00:01:42,000 +which prints a statement. + +44 +00:01:42,000 --> 00:01:43,000 +So of course I'm expecting + +45 +00:01:43,000 --> 00:01:46,000 +that it'll execute the constructor and it will. + +46 +00:01:46,000 --> 00:01:48,000 +That's what you got in B. + +47 +00:01:48,000 --> 00:01:49,000 +That perfectly makes sense and we have talked + +48 +00:01:49,000 --> 00:01:50,000 +about this from a long time. + +49 +00:01:50,000 --> 00:01:54,000 +The twist here is, what if I have a constructor + +50 +00:01:54,000 --> 00:01:56,000 +in A as well? + +51 +00:01:56,000 --> 00:01:59,000 +And here I'm printing in A. + +52 +00:01:59,000 --> 00:02:01,000 +Now what do you think will be the output? + +53 +00:02:01,000 --> 00:02:05,000 +Because of course we are grading the object of B, not A, + +54 +00:02:05,000 --> 00:02:09,000 +so it should call the constructor of B and that makes sense. + +55 +00:02:09,000 --> 00:02:10,000 +But if you compile this code, + +56 +00:02:10,000 --> 00:02:12,000 +you will see something strange. + +57 +00:02:12,000 --> 00:02:13,000 +If I compile this code + +58 +00:02:13,000 --> 00:02:17,000 +and run, oh, it is also executing the constructor of A. + +59 +00:02:17,000 --> 00:02:19,000 +Now that's weird for us, right? + +60 +00:02:19,000 --> 00:02:22,000 +Because we are creating object of B, + +61 +00:02:22,000 --> 00:02:24,000 +it should only call the construct of B, + +62 +00:02:24,000 --> 00:02:26,000 +but it is also calling the construct of A, + +63 +00:02:26,000 --> 00:02:28,000 +and that sounds weird. + +64 +00:02:28,000 --> 00:02:31,000 +Now we understand that, but at this point, just remember + +65 +00:02:31,000 --> 00:02:32,000 +whenever you get object of a class, + +66 +00:02:32,000 --> 00:02:36,000 +it'll call the constructor of subclass and superclass both. + +67 +00:02:36,000 --> 00:02:39,000 +A is a superclass here, B is subclass. + +68 +00:02:39,000 --> 00:02:41,000 +It'll call the constructor of both. + +69 +00:02:41,000 --> 00:02:43,000 +Why? We'll talk about it in some time, + +70 +00:02:43,000 --> 00:02:45,000 +but it calls the constructor of both. + +71 +00:02:45,000 --> 00:02:46,000 +Now let me just add a twist here, + +72 +00:02:46,000 --> 00:02:50,000 +in B, let me create a parameterize constructor, + +73 +00:02:50,000 --> 00:02:51,000 +and we have seen that, right? + +74 +00:02:51,000 --> 00:02:53,000 +So what is parameterize constructor? + +75 +00:02:53,000 --> 00:02:55,000 +A constructor which takes a value. + +76 +00:02:55,000 --> 00:02:56,000 +Let's say this is N, okay? + +77 +00:02:56,000 --> 00:03:00,000 +And here, I'll print "in B int" + +78 +00:03:00,000 --> 00:03:03,000 +because we have a constructor which takes + +79 +00:03:03,000 --> 00:03:04,000 +a int n parameter. + +80 +00:03:04,000 --> 00:03:07,000 +I'm just printing just int just to differentiate + +81 +00:03:07,000 --> 00:03:08,000 +between these two statements. + +82 +00:03:08,000 --> 00:03:11,000 +Now with this, if I compile this code and run, + +83 +00:03:11,000 --> 00:03:11,000 +what do you think? + +84 +00:03:11,000 --> 00:03:13,000 +What will be the output? + +85 +00:03:13,000 --> 00:03:14,000 +Compile and run. + +86 +00:03:14,000 --> 00:03:16,000 +Still it is in A and in B. + +87 +00:03:16,000 --> 00:03:18,000 +Why not in B int is because + +88 +00:03:18,000 --> 00:03:20,000 +when you are creating the object, + +89 +00:03:20,000 --> 00:03:22,000 +you are not passing any value here. + +90 +00:03:22,000 --> 00:03:25,000 +Basically you are calling a default constructor, + +91 +00:03:25,000 --> 00:03:27,000 +but if you pass a value, let's say five, + +92 +00:03:27,000 --> 00:03:30,000 +in this case you are specifically mentioning, "Hey, + +93 +00:03:30,000 --> 00:03:32,000 +I'm creating an object of B + +94 +00:03:32,000 --> 00:03:34,000 +and I'm passing a integer here, + +95 +00:03:34,000 --> 00:03:38,000 +so it'll call the constructor of B, which takes integer. + +96 +00:03:38,000 --> 00:03:41,000 +Now by doing this, if I run this code, + +97 +00:03:41,000 --> 00:03:43,000 +or you can see it is printing in A + +98 +00:03:43,000 --> 00:03:45,000 +and in B, int. + +99 +00:03:45,000 --> 00:03:47,000 +Okay, that actually makes sense. + +100 +00:03:47,000 --> 00:03:49,000 +It'll call the construct of A, + +101 +00:03:49,000 --> 00:03:53,000 +but what if I also have a parameterize constructor in + +102 +00:03:53,000 --> 00:03:55,000 +A as well, which takes a N + +103 +00:03:55,000 --> 00:03:59,000 +and it also prints in A int. + +104 +00:03:59,000 --> 00:04:02,000 +Now, since we were not having a parameterize constructor, + +105 +00:04:02,000 --> 00:04:03,000 +it was called a (indistinct) constructor. + +106 +00:04:03,000 --> 00:04:05,000 +That's what I'm assuming at this point. + +107 +00:04:05,000 --> 00:04:08,000 +Now, when you have a parameterize constructor for both, + +108 +00:04:08,000 --> 00:04:10,000 +for A and B both, + +109 +00:04:10,000 --> 00:04:12,000 +now there is an expectation + +110 +00:04:12,000 --> 00:04:15,000 +that it'll print in A int and in B int. + +111 +00:04:15,000 --> 00:04:16,000 +And that's what I'm thinking. + +112 +00:04:16,000 --> 00:04:18,000 +I'm not sure what you are thinking. + +113 +00:04:18,000 --> 00:04:20,000 +Let's try, let's see if this works. + +114 +00:04:20,000 --> 00:04:23,000 +Oh no, what is happening here? + +115 +00:04:23,000 --> 00:04:26,000 +It is calling the the parameters constructor of B. + +116 +00:04:26,000 --> 00:04:27,000 +That actually makes sense, but why? + +117 +00:04:27,000 --> 00:04:29,000 +The default construct of A, + +118 +00:04:29,000 --> 00:04:32,000 +it should have been called this one, right? + +119 +00:04:32,000 --> 00:04:34,000 +Let's understand why it is first of all, + +120 +00:04:34,000 --> 00:04:36,000 +calling the constructor of parent class. + +121 +00:04:36,000 --> 00:04:37,000 +Why in the first place? + +122 +00:04:37,000 --> 00:04:40,000 +The thing is, and remember this point, + +123 +00:04:40,000 --> 00:04:45,000 +every constructor in Java has a method which is there, + +124 +00:04:47,000 --> 00:04:49,000 +even if you don't mention, okay? + +125 +00:04:49,000 --> 00:04:52,000 +And that method is super. + +126 +00:04:52,000 --> 00:04:56,000 +Now this method is there, even if you don't mention, + +127 +00:04:56,000 --> 00:04:57,000 +you can imagine that's like a John Cena. + +128 +00:04:57,000 --> 00:04:59,000 +I don't know if you know the John Cena memes. + +129 +00:04:59,000 --> 00:05:02,000 +It's just that you can't see it. Okay? + +130 +00:05:02,000 --> 00:05:03,000 +So super is there. You can't see it. + +131 +00:05:03,000 --> 00:05:06,000 +Even if you don't mention super, it is there. + +132 +00:05:06,000 --> 00:05:08,000 +So by default, every constructor, + +133 +00:05:08,000 --> 00:05:11,000 +the first statement is super + +134 +00:05:11,000 --> 00:05:14,000 +and every constructor not just this one, + +135 +00:05:14,000 --> 00:05:16,000 +every constructor, here as well. + +136 +00:05:16,000 --> 00:05:17,000 +Super, okay? + +137 +00:05:17,000 --> 00:05:20,000 +So every constructor will have super. + +138 +00:05:20,000 --> 00:05:22,000 +Same goes for A as well. + +139 +00:05:22,000 --> 00:05:23,000 +It'll also have super + +140 +00:05:24,000 --> 00:05:26,000 +and super, + +141 +00:05:26,000 --> 00:05:27,000 +irrespective to which class it is. + +142 +00:05:27,000 --> 00:05:29,000 +It'll have this statement super, + +143 +00:05:29,000 --> 00:05:31,000 +even if you don't mention it, is there, okay? + +144 +00:05:31,000 --> 00:05:32,000 +That's a point to remember. + +145 +00:05:32,000 --> 00:05:35,000 +Now let's try to understand how it is working. + +146 +00:05:35,000 --> 00:05:36,000 +Let's do a dry run. + +147 +00:05:36,000 --> 00:05:39,000 +The moment you execute this statement, what is happening? + +148 +00:05:39,000 --> 00:05:43,000 +It is calling the constructor of B, which is parametrized. + +149 +00:05:43,000 --> 00:05:46,000 +So it is calling this program method. + +150 +00:05:46,000 --> 00:05:47,000 +So when you execute this method, + +151 +00:05:47,000 --> 00:05:52,000 +the first thing it sees is super, not the system.println + +152 +00:05:52,000 --> 00:05:54,000 +What it is saying is super. Now what is super? + +153 +00:05:54,000 --> 00:05:57,000 +Now super method simply means call the constructor + +154 +00:05:57,000 --> 00:05:59,000 +of a super class. + +155 +00:05:59,000 --> 00:06:00,000 +So let me repeat. + +156 +00:06:00,000 --> 00:06:05,000 +Super means: call the constructor of a super class. + +157 +00:06:05,000 --> 00:06:07,000 +Now what's the in super class? + +158 +00:06:07,000 --> 00:06:10,000 +This is a super class, it is calling the constructor + +159 +00:06:10,000 --> 00:06:12,000 +of a super class, but which one? + +160 +00:06:12,000 --> 00:06:14,000 +The parameterized one. + +161 +00:06:14,000 --> 00:06:18,000 +So the default one, if you can see we are saying default, + +162 +00:06:18,000 --> 00:06:20,000 +we are not passing any parameter here. + +163 +00:06:20,000 --> 00:06:24,000 +So it is calling this particular constructor + +164 +00:06:24,000 --> 00:06:27,000 +and that's why it's printing in A, + +165 +00:06:27,000 --> 00:06:29,000 +and that's why if you can see the output, + +166 +00:06:29,000 --> 00:06:30,000 +it is first printing in A, + +167 +00:06:30,000 --> 00:06:34,000 +and then once it executes the A constructor, + +168 +00:06:34,000 --> 00:06:37,000 +it'll execute the remaining part of B constructor, + +169 +00:06:37,000 --> 00:06:40,000 +which is this part, in B int. + +170 +00:06:40,000 --> 00:06:42,000 +And that's why we got in B int, okay? + +171 +00:06:42,000 --> 00:06:46,000 +That means, if I don't want to call this constructor, + +172 +00:06:46,000 --> 00:06:49,000 +if I want to call the parameterized one, we have + +173 +00:06:49,000 --> 00:06:51,000 +to do only one change, which is + +174 +00:06:51,000 --> 00:06:55,000 +what if in this super you pass a number? + +175 +00:06:55,000 --> 00:06:57,000 +So any is a value, right? It'll have a value, + +176 +00:06:57,000 --> 00:07:00,000 +the same value you are passing to this super. + +177 +00:07:00,000 --> 00:07:02,000 +Now what we are saying is, "Hey Java, I know + +178 +00:07:02,000 --> 00:07:05,000 +you are going to execute this super class constructor, + +179 +00:07:05,000 --> 00:07:06,000 +but I'm passing a parameter there." + +180 +00:07:06,000 --> 00:07:08,000 +So now this time you have to call + +181 +00:07:08,000 --> 00:07:10,000 +the parameterized constructor. + +182 +00:07:10,000 --> 00:07:13,000 +By doing those changes, let's compile and run. + +183 +00:07:13,000 --> 00:07:14,000 +Here we go. + +184 +00:07:14,000 --> 00:07:17,000 +It is printing in A int and in B int. + +185 +00:07:17,000 --> 00:07:20,000 +So super is a very handy tool. It's there already. + +186 +00:07:20,000 --> 00:07:21,000 +You don't have to mention that, + +187 +00:07:21,000 --> 00:07:23,000 +but if you want to make some changes, of course you will + +188 +00:07:23,000 --> 00:07:24,000 +but you have to mention. + +189 +00:07:24,000 --> 00:07:26,000 +So if you want to pass a parametrized constructor, + +190 +00:07:26,000 --> 00:07:30,000 +then explicitly you have to mention super. + +191 +00:07:30,000 --> 00:07:33,000 +I hope that made some sense. Okay? + +192 +00:07:33,000 --> 00:07:35,000 +Now what I want is, I want to call + +193 +00:07:35,000 --> 00:07:38,000 +the default constructor of B. + +194 +00:07:38,000 --> 00:07:40,000 +Okay? I'm not passing any parameter here. + +195 +00:07:40,000 --> 00:07:42,000 +I'm calling a object of B. + +196 +00:07:42,000 --> 00:07:46,000 +So of course if I execute this, if I compile and run, + +197 +00:07:46,000 --> 00:07:49,000 +you can see it's printing in A and in B. + +198 +00:07:49,000 --> 00:07:50,000 +Why? + +199 +00:07:50,000 --> 00:07:52,000 +It's because when you are calling + +200 +00:07:52,000 --> 00:07:55,000 +the default constructor here in default construct of B, + +201 +00:07:55,000 --> 00:07:56,000 +it'll execute first. + +202 +00:07:56,000 --> 00:07:58,000 +It is calling this super, + +203 +00:07:58,000 --> 00:08:00,000 +that means it is calling this part constructor, + +204 +00:08:00,000 --> 00:08:05,000 +which is printing in A, and then it is printing in B. + +205 +00:08:05,000 --> 00:08:08,000 +It'll continue with this execution of B, right? + +206 +00:08:08,000 --> 00:08:11,000 +But what I want now is I know you got the point, + +207 +00:08:11,000 --> 00:08:14,000 +what I want is I want to execute in A int + +208 +00:08:14,000 --> 00:08:18,000 +and then I want to print in B. + +209 +00:08:18,000 --> 00:08:21,000 +So the output which I want is, in A INT in B. + +210 +00:08:21,000 --> 00:08:23,000 +So how can we play with that? + +211 +00:08:23,000 --> 00:08:26,000 +So what if here, if I pass a value at say five + +212 +00:08:26,000 --> 00:08:27,000 +or any number, doesn't matter. + +213 +00:08:27,000 --> 00:08:30,000 +If I pass a number, it'll try to create object of B + +214 +00:08:30,000 --> 00:08:31,000 +with a default constructor, + +215 +00:08:31,000 --> 00:08:33,000 +it'll execute a default constructor. + +216 +00:08:33,000 --> 00:08:36,000 +But now we are calling a super class constructor + +217 +00:08:36,000 --> 00:08:38,000 +by passing a value. + +218 +00:08:38,000 --> 00:08:41,000 +So which constructor will be called? + +219 +00:08:41,000 --> 00:08:46,000 +Of course the second one and it'll print in A int in B. + +220 +00:08:46,000 --> 00:08:49,000 +Let's try, compile, run. + +221 +00:08:49,000 --> 00:08:50,000 +And that's what we got. + +222 +00:08:50,000 --> 00:08:54,000 +So that's how we can play with super and that is fun. Right? + +223 +00:08:54,000 --> 00:08:55,000 +Now I have a question for you. + +224 +00:08:55,000 --> 00:08:57,000 +Maybe you have observed it or not. + +225 +00:08:57,000 --> 00:08:59,000 +See in B class, when we are saying super, + +226 +00:08:59,000 --> 00:09:02,000 +it actually makes sense because we have a super class for B. + +227 +00:09:02,000 --> 00:09:05,000 +Okay? This is A here is a super class. + +228 +00:09:05,000 --> 00:09:08,000 +But when you say super for A, what is a super class for A? + +229 +00:09:08,000 --> 00:09:10,000 +We have not mentioned anything here, right? + +230 +00:09:10,000 --> 00:09:12,000 +Okay, now that's why we have to introduce + +231 +00:09:12,000 --> 00:09:13,000 +a new concept here. + +232 +00:09:13,000 --> 00:09:16,000 +Every class in Java, let me repeat. + +233 +00:09:16,000 --> 00:09:21,000 +Every class in Java extends the object class. + +234 +00:09:22,000 --> 00:09:27,000 +So object is a class in Java, which has a lot of methods. + +235 +00:09:27,000 --> 00:09:28,000 +We'll talk about those methods later. + +236 +00:09:28,000 --> 00:09:32,000 +But every class in Java extends this class. + +237 +00:09:32,000 --> 00:09:34,000 +Even if you don't mention, so even + +238 +00:09:34,000 --> 00:09:36,000 +if you don't mention this, it is extending it. + +239 +00:09:36,000 --> 00:09:39,000 +Again, you can imagine this as a John Cena, + +240 +00:09:39,000 --> 00:09:40,000 +it's there, you can't see it. + +241 +00:09:40,000 --> 00:09:43,000 +Okay? So even if you don't mention, or you mention, + +242 +00:09:43,000 --> 00:09:44,000 +it's the same thing. + +243 +00:09:44,000 --> 00:09:46,000 +So when you say super here, + +244 +00:09:46,000 --> 00:09:50,000 +you're basically calling a constructor of the object class. + +245 +00:09:50,000 --> 00:09:53,000 +Okay? In object class, also we have a constructor. + +246 +00:09:53,000 --> 00:09:56,000 +This is what you are calling, cool, right? + +247 +00:09:56,000 --> 00:09:58,000 +So even if you don't mention it, it's there. + +248 +00:09:58,000 --> 00:10:01,000 +Now you might be thinking the B class must be + +249 +00:10:01,000 --> 00:10:02,000 +extending A and object both? + +250 +00:10:02,000 --> 00:10:06,000 +No, B class extends A, A class extends objects. + +251 +00:10:06,000 --> 00:10:08,000 +So this is multi-level inheritance, okay? + +252 +00:10:08,000 --> 00:10:10,000 +There's no multiples till now, okay? + +253 +00:10:10,000 --> 00:10:13,000 +So yeah, that's how it works. We have object class. + +254 +00:10:13,000 --> 00:10:16,000 +So this super represents the constructor + +255 +00:10:16,000 --> 00:10:17,000 +of the object class. + +256 +00:10:17,000 --> 00:10:20,000 +So we have talked about super, but what is this? + +257 +00:10:20,000 --> 00:10:22,000 +Okay, now let me introduce this here. + +258 +00:10:22,000 --> 00:10:25,000 +The output, which I want now is, I want to execute + +259 +00:10:25,000 --> 00:10:28,000 +both the constructor of B. + +260 +00:10:28,000 --> 00:10:29,000 +Okay? So let me just keep it default now, + +261 +00:10:29,000 --> 00:10:30,000 +let me say super, + +262 +00:10:30,000 --> 00:10:33,000 +I want to execute both the constructor of B. + +263 +00:10:33,000 --> 00:10:35,000 +So I want only one object, okay? + +264 +00:10:35,000 --> 00:10:38,000 +With one object, can I execute both the constructor? + +265 +00:10:38,000 --> 00:10:40,000 +Of course I want to execute the constructor of A, + +266 +00:10:40,000 --> 00:10:44,000 +which is default, but I want to execute the constructor + +267 +00:10:44,000 --> 00:10:46,000 +of both B. + +268 +00:10:46,000 --> 00:10:50,000 +So what I'm saying is, I want in A, I want in B + +269 +00:10:50,000 --> 00:10:53,000 +and in B int, is it possible? + +270 +00:10:53,000 --> 00:10:55,000 +For that, what we can do is, + +271 +00:10:55,000 --> 00:10:57,000 +see we are creating the constructor. + +272 +00:10:57,000 --> 00:11:00,000 +Let me pass a value here, let me pass five. + +273 +00:11:00,000 --> 00:11:01,000 +So of course by doing that, + +274 +00:11:01,000 --> 00:11:03,000 +what we are doing is we are calling the constructor, + +275 +00:11:03,000 --> 00:11:06,000 +which is the parametized one, and we are calling super + +276 +00:11:06,000 --> 00:11:09,000 +and that's what's calling the super class constructor. + +277 +00:11:09,000 --> 00:11:11,000 +But then we have one more method called this. + +278 +00:11:11,000 --> 00:11:13,000 +We have talked about this keyword, right? + +279 +00:11:13,000 --> 00:11:15,000 +Now, what this will do is, + +280 +00:11:15,000 --> 00:11:18,000 +this will execute the construct of a same class. + +281 +00:11:18,000 --> 00:11:19,000 +Let me repeat: + +282 +00:11:19,000 --> 00:11:23,000 +This will execute the constructor of a same class, + +283 +00:11:23,000 --> 00:11:24,000 +which is this one. + +284 +00:11:24,000 --> 00:11:26,000 +We are not passing in a parameter. + +285 +00:11:26,000 --> 00:11:29,000 +So it'll call the construct of a same class. + +286 +00:11:29,000 --> 00:11:32,000 +And then this constructor is calling the construct + +287 +00:11:32,000 --> 00:11:32,000 +of a super class. + +288 +00:11:32,000 --> 00:11:36,000 +Let's try this works, compile, run. + +289 +00:11:36,000 --> 00:11:40,000 +And you can see that we got in A, in B, in B int. + +290 +00:11:40,000 --> 00:11:43,000 +I mean of course this will execute first, this method, + +291 +00:11:43,000 --> 00:11:46,000 +but this particular method B constructor is + +292 +00:11:46,000 --> 00:11:49,000 +executing this, which is the default constructor. + +293 +00:11:49,000 --> 00:11:52,000 +The default constructor executes the super which goes here, + +294 +00:11:52,000 --> 00:11:57,000 +it'll execute in A, it'll execute in B and then in B int. + +295 +00:11:57,000 --> 00:12:00,000 +So that's what is, which is this keyword + +296 +00:12:00,000 --> 00:12:02,000 +and also this method and super method. + +297 +00:12:02,000 --> 00:12:05,000 +So this method execute the construct of a same class, + +298 +00:12:05,000 --> 00:12:08,000 +super method executes the construct of a super class. + +299 +00:12:08,000 --> 00:12:09,000 +So yeah, that's it from this video. + +300 +00:12:09,000 --> 00:12:10,000 +I hope you enjoyed it. + +301 +00:12:10,000 --> 00:12:13,000 +Let's see what we can do in the next video. + diff --git a/02 - Core Java/056 Method Overriding_en.srt b/02 - Core Java/056 Method Overriding_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..f57c7185cacbe7f98db8e8e91ade1cb659aa451d --- /dev/null +++ b/02 - Core Java/056 Method Overriding_en.srt @@ -0,0 +1,808 @@ +1 +00:00:00,000 --> 00:00:02,000 +(bright music) + +2 +00:00:03,000 --> 00:00:05,000 +-: So once we talked about inheritance + +3 +00:00:05,000 --> 00:00:07,000 +and method overloading as well, right? + +4 +00:00:07,000 --> 00:00:08,000 +We have talked about method overloading. + +5 +00:00:08,000 --> 00:00:09,000 +In this video, + +6 +00:00:09,000 --> 00:00:12,000 +let's talk about method overriding. That's right. + +7 +00:00:12,000 --> 00:00:14,000 +So we have two different method, + +8 +00:00:14,000 --> 00:00:16,000 +overloading and method overriding. + +9 +00:00:16,000 --> 00:00:17,000 +So let's talk about overriding here. + +10 +00:00:17,000 --> 00:00:18,000 +So for that, what I will do is + +11 +00:00:18,000 --> 00:00:20,000 +I will just try to keep it simple again + +12 +00:00:20,000 --> 00:00:22,000 +and let's use the class A + +13 +00:00:22,000 --> 00:00:25,000 +and inside this class A, let's say we have a method. + +14 +00:00:25,000 --> 00:00:27,000 +So I will just remove this part. + +15 +00:00:27,000 --> 00:00:31,000 +And here I would say public void show. + +16 +00:00:31,000 --> 00:00:32,000 +Just to keep it simple, + +17 +00:00:32,000 --> 00:00:33,000 +I'm using a very simple example here, + +18 +00:00:33,000 --> 00:00:36,000 +which is show which prints something. + +19 +00:00:36,000 --> 00:00:39,000 +Let's say it prints in show, okay. + +20 +00:00:39,000 --> 00:00:42,000 +Now if you have a method inside a class, + +21 +00:00:42,000 --> 00:00:43,000 +of course you can create the object + +22 +00:00:43,000 --> 00:00:45,000 +and you can call the method, right, + +23 +00:00:45,000 --> 00:00:46,000 +and we have seen that. + +24 +00:00:46,000 --> 00:00:48,000 +So let's say if I go back to my main method, + +25 +00:00:48,000 --> 00:00:51,000 +if I say A obj = new A, + +26 +00:00:51,000 --> 00:00:56,000 +and I can simply say obj.show and it'll work, right. + +27 +00:00:56,000 --> 00:00:57,000 +And it should work. + +28 +00:00:57,000 --> 00:00:59,000 +That's what we have done from a long time. + +29 +00:00:59,000 --> 00:01:02,000 +So let's compile this code and run. + +30 +00:01:02,000 --> 00:01:04,000 +And you can see it prints in show. + +31 +00:01:04,000 --> 00:01:06,000 +Just to keep it simple, we have went + +32 +00:01:06,000 --> 00:01:08,000 +with a very simple example. + +33 +00:01:08,000 --> 00:01:09,000 +Now what I wanna do is + +34 +00:01:09,000 --> 00:01:12,000 +I want to get a class which is class B, + +35 +00:01:12,000 --> 00:01:15,000 +and I want this class B to extends class A. + +36 +00:01:15,000 --> 00:01:18,000 +And of course when you talk about inheritance, + +37 +00:01:18,000 --> 00:01:19,000 +this method of show, + +38 +00:01:19,000 --> 00:01:21,000 +it also will be available inside Class B, right. + +39 +00:01:21,000 --> 00:01:25,000 +So if I create object of class B instead of A, + +40 +00:01:25,000 --> 00:01:28,000 +it'll also work because that's how inheritance works, right. + +41 +00:01:28,000 --> 00:01:31,000 +So if I go back, compile and run, it works. + +42 +00:01:31,000 --> 00:01:33,000 +You can see we got in show. + +43 +00:01:33,000 --> 00:01:37,000 +What I do now is I will also print in A show + +44 +00:01:37,000 --> 00:01:40,000 +just to differentiate that this is getting printed + +45 +00:01:40,000 --> 00:01:41,000 +in the A class, okay. + +46 +00:01:41,000 --> 00:01:43,000 +Now the thing is, + +47 +00:01:43,000 --> 00:01:47,000 +what if you want to have a different definition of it? + +48 +00:01:47,000 --> 00:01:48,000 +Okay, why do we need different definition? + +49 +00:01:48,000 --> 00:01:52,000 +Let's say we got two methods here, public void config. + +50 +00:01:52,000 --> 00:01:54,000 +So let's say we have this method which is called config. + +51 +00:01:54,000 --> 00:01:55,000 +I don't know what it'll do. + +52 +00:01:55,000 --> 00:01:58,000 +Just to keep it simple, I will just say S out + +53 +00:01:58,000 --> 00:02:01,000 +and I will say this is in a config, okay. + +54 +00:02:01,000 --> 00:02:03,000 +And let's say they have different implementation, + +55 +00:02:03,000 --> 00:02:04,000 +they have different statements. + +56 +00:02:04,000 --> 00:02:06,000 +At this point, we are not focusing on statements. + +57 +00:02:06,000 --> 00:02:08,000 +We are here to understand the concept, right? + +58 +00:02:08,000 --> 00:02:10,000 +So let's say it does something, + +59 +00:02:10,000 --> 00:02:12,000 +so method and config does something + +60 +00:02:12,000 --> 00:02:15,000 +and I can actually call both the methods, not just one. + +61 +00:02:15,000 --> 00:02:17,000 +I can call config as well, okay? + +62 +00:02:17,000 --> 00:02:21,000 +And if I compile and run this code, it works. + +63 +00:02:21,000 --> 00:02:24,000 +You can see it says in A show in A config. + +64 +00:02:24,000 --> 00:02:28,000 +Now, let's say I want a different definition for show, okay, + +65 +00:02:28,000 --> 00:02:29,000 +not for config. + +66 +00:02:29,000 --> 00:02:32,000 +So basically what I want is when it comes to B, + +67 +00:02:32,000 --> 00:02:36,000 +and when I call show, it should print in B show. + +68 +00:02:36,000 --> 00:02:39,000 +How do I do that? How do I change the execution? + +69 +00:02:39,000 --> 00:02:41,000 +So what I want is when I call show, + +70 +00:02:41,000 --> 00:02:43,000 +it should not call the show of A + +71 +00:02:43,000 --> 00:02:46,000 +because show of A will print in A show. + +72 +00:02:46,000 --> 00:02:47,000 +I want to use config as it is. + +73 +00:02:47,000 --> 00:02:49,000 +So I can't even remove this inheritance. + +74 +00:02:49,000 --> 00:02:52,000 +If I remove this, and if I define the methods, + +75 +00:02:52,000 --> 00:02:53,000 +of course I can define show, + +76 +00:02:53,000 --> 00:02:57,000 +but then I want this config to be coming from A. + +77 +00:02:57,000 --> 00:02:58,000 +So of course you have to keep it. + +78 +00:02:58,000 --> 00:03:02,000 +You have to say Class B extends A, because we want config. + +79 +00:03:02,000 --> 00:03:05,000 +What we don't want of A is show + +80 +00:03:05,000 --> 00:03:07,000 +because it is printing in A show. + +81 +00:03:07,000 --> 00:03:08,000 +I wanna do something else. + +82 +00:03:08,000 --> 00:03:11,000 +In that scenario, you can say public void show, + +83 +00:03:11,000 --> 00:03:13,000 +you can write the same method name + +84 +00:03:13,000 --> 00:03:14,000 +or maybe a different name. + +85 +00:03:14,000 --> 00:03:17,000 +You can say in show1, I mean show1 + +86 +00:03:17,000 --> 00:03:21,000 +and you can print here in B show. + +87 +00:03:21,000 --> 00:03:22,000 +I mean of course you can do this. + +88 +00:03:22,000 --> 00:03:24,000 +The only thing is + +89 +00:03:24,000 --> 00:03:26,000 +if you execute this code now, + +90 +00:03:26,000 --> 00:03:28,000 +it'll still print in A show. + +91 +00:03:28,000 --> 00:03:30,000 +You know why? is because we are calling show, + +92 +00:03:30,000 --> 00:03:32,000 +and both these methods are different. + +93 +00:03:32,000 --> 00:03:36,000 +One with name show and one with name show1, okay. + +94 +00:03:36,000 --> 00:03:37,000 +That means if I call show1, then it'll work, + +95 +00:03:37,000 --> 00:03:40,000 +then it'll print in B show. + +96 +00:03:40,000 --> 00:03:41,000 +But what I want is + +97 +00:03:41,000 --> 00:03:43,000 +I want them to have a same name. + +98 +00:03:43,000 --> 00:03:46,000 +I want my this method, which is show + +99 +00:03:46,000 --> 00:03:49,000 +should override the method here. + +100 +00:03:49,000 --> 00:03:50,000 +Is it possible? + +101 +00:03:50,000 --> 00:03:51,000 +And the answer is yes. + +102 +00:03:51,000 --> 00:03:53,000 +In Java we get this amazing feature where + +103 +00:03:53,000 --> 00:03:56,000 +you can basically override the methods + +104 +00:03:56,000 --> 00:03:57,000 +of the patent class. + +105 +00:03:57,000 --> 00:04:02,000 +So of course you want everything except this part. Okay. + +106 +00:04:02,000 --> 00:04:03,000 +I mean, except this show method. + +107 +00:04:03,000 --> 00:04:05,000 +I want config, but I don't want show. + +108 +00:04:05,000 --> 00:04:06,000 +Now, let me give you one more example here. + +109 +00:04:06,000 --> 00:04:08,000 +Remember when we talked about inheritance, + +110 +00:04:08,000 --> 00:04:09,000 +I have used an example + +111 +00:04:09,000 --> 00:04:12,000 +where I was not having any phone in my 10 standard + +112 +00:04:12,000 --> 00:04:15,000 +and my father had a phone, which is 3310. + +113 +00:04:15,000 --> 00:04:18,000 +Now if you ask me at that point, which phone I have, + +114 +00:04:18,000 --> 00:04:22,000 +I would just say 3310 because that's my father's phone. + +115 +00:04:22,000 --> 00:04:24,000 +But after two years, when I was in my 12 standard, + +116 +00:04:24,000 --> 00:04:25,000 +I got my own phone. + +117 +00:04:25,000 --> 00:04:27,000 +I got Motorola C168. + +118 +00:04:27,000 --> 00:04:29,000 +Again, not a smart phone, + +119 +00:04:29,000 --> 00:04:32,000 +but it was good, it was a very slim phone. + +120 +00:04:32,000 --> 00:04:35,000 +So when now if you ask me which phone I have, + +121 +00:04:35,000 --> 00:04:37,000 +I will say I have Motorola C168. + +122 +00:04:37,000 --> 00:04:40,000 +My father still had 3310. + +123 +00:04:40,000 --> 00:04:41,000 +So here, if you can see, + +124 +00:04:41,000 --> 00:04:43,000 +if someone ask my father, + +125 +00:04:43,000 --> 00:04:45,000 +father will say, I have 3310. + +126 +00:04:45,000 --> 00:04:48,000 +If you ask me, I have a choice of saying 3310, + +127 +00:04:48,000 --> 00:04:52,000 +but I will go for C168, which is my phone, right? + +128 +00:04:52,000 --> 00:04:55,000 +So you always give preference to your own features first. + +129 +00:04:55,000 --> 00:04:58,000 +Example, if you call show here, + +130 +00:04:58,000 --> 00:04:59,000 +it'll execute the method of B + +131 +00:04:59,000 --> 00:05:01,000 +instead of executing the method of A. + +132 +00:05:01,000 --> 00:05:04,000 +So basically this method show + +133 +00:05:04,000 --> 00:05:07,000 +overrides the method of A here. + +134 +00:05:07,000 --> 00:05:10,000 +And we can simply execute, compile, and run. + +135 +00:05:10,000 --> 00:05:12,000 +You can see it prints in B show. + +136 +00:05:12,000 --> 00:05:13,000 +Now this will make much more sense + +137 +00:05:13,000 --> 00:05:16,000 +when we say we are creating not A, + +138 +00:05:16,000 --> 00:05:18,000 +but we are creating a calculator + +139 +00:05:18,000 --> 00:05:20,000 +which has a method called let's say, + +140 +00:05:20,000 --> 00:05:22,000 +let me remove config for some time. + +141 +00:05:22,000 --> 00:05:25,000 +Let's say that we have a method called add, + +142 +00:05:25,000 --> 00:05:28,000 +and this will return a value, okay. + +143 +00:05:28,000 --> 00:05:30,000 +And here, instead of printing this, + +144 +00:05:30,000 --> 00:05:31,000 +I can simply accept two values. + +145 +00:05:31,000 --> 00:05:35,000 +I can say int n1, int n2, + +146 +00:05:35,000 --> 00:05:37,000 +and here we have done this before + +147 +00:05:37,000 --> 00:05:38,000 +and that's why I'm writing this code very fast. + +148 +00:05:38,000 --> 00:05:43,000 +I can say return and n1+n2. Okay. + +149 +00:05:43,000 --> 00:05:46,000 +So if your class, which is instead of saying B, + +150 +00:05:46,000 --> 00:05:50,000 +I can say Advanced Calc, which is extending calc. + +151 +00:05:50,000 --> 00:05:52,000 +It'll also have some features. + +152 +00:05:52,000 --> 00:05:56,000 +But let's say I want to add two numbers, + +153 +00:05:56,000 --> 00:05:57,000 +which will also take int + +154 +00:05:57,000 --> 00:05:59,000 +and it'll have the same parameter, okay, + +155 +00:05:59,000 --> 00:06:01,000 +because in method overloading we have seen + +156 +00:06:01,000 --> 00:06:03,000 +it can have the same method name + +157 +00:06:03,000 --> 00:06:04,000 +but different parameters. + +158 +00:06:04,000 --> 00:06:06,000 +Here in method overriding, + +159 +00:06:06,000 --> 00:06:08,000 +it'll be same name, same parameters as well. + +160 +00:06:08,000 --> 00:06:10,000 +I mean same type of parameters, + +161 +00:06:10,000 --> 00:06:12,000 +not the name, doesn't matter name, + +162 +00:06:12,000 --> 00:06:13,000 +let's say n1, n2 + +163 +00:06:13,000 --> 00:06:16,000 +and it'll have the same name, same number of parameters, + +164 +00:06:16,000 --> 00:06:18,000 +same type of parameters, + +165 +00:06:18,000 --> 00:06:19,000 +but it'll have a different behavior. + +166 +00:06:19,000 --> 00:06:20,000 +Let's say n1+n2+1. + +167 +00:06:20,000 --> 00:06:22,000 +I'm just adding extra one there. + +168 +00:06:22,000 --> 00:06:27,000 +So what I want is if I comment this section, okay, + +169 +00:06:27,000 --> 00:06:28,000 +at this point I'm not overriding it, + +170 +00:06:28,000 --> 00:06:31,000 +I just have add method inside calc, + +171 +00:06:31,000 --> 00:06:34,000 +and if I create the object of advanced calculator + +172 +00:06:34,000 --> 00:06:38,000 +by saying Advanced Calculator obj = new Advanced Calculator, + +173 +00:06:38,000 --> 00:06:42,000 +and if I try to call the add method by passing 3 and 4, + +174 +00:06:42,000 --> 00:06:44,000 +of course 3 and 4 is 7. + +175 +00:06:44,000 --> 00:06:46,000 +So if I get this value somewhere, + +176 +00:06:46,000 --> 00:06:48,000 +if I say int r1 =, + +177 +00:06:48,000 --> 00:06:51,000 +and if I print the value for r1, + +178 +00:06:51,000 --> 00:06:53,000 +and again, since we have done this code before, + +179 +00:06:53,000 --> 00:06:55,000 +I'm just typing it fast. + +180 +00:06:55,000 --> 00:06:57,000 +Now, what do you think? What will the output? + +181 +00:06:57,000 --> 00:06:58,000 +I'm passing 3 and 4. + +182 +00:06:58,000 --> 00:07:01,000 +So of course it'll first search for this method add + +183 +00:07:01,000 --> 00:07:03,000 +inside the same class. + +184 +00:07:03,000 --> 00:07:05,000 +Of course, we don't have that method. + +185 +00:07:05,000 --> 00:07:07,000 +So it'll go to the upper class by saying, + +186 +00:07:07,000 --> 00:07:10,000 +Hey, you know calc, you are my parent class, + +187 +00:07:10,000 --> 00:07:14,000 +I'm searching for add with you and yes, it is available. + +188 +00:07:14,000 --> 00:07:17,000 +So basically we can just compile this code and run. + +189 +00:07:17,000 --> 00:07:20,000 +You can see we got 7 and that actually works. + +190 +00:07:20,000 --> 00:07:23,000 +But now I want a different definition for add, + +191 +00:07:23,000 --> 00:07:24,000 +I just want some changes. + +192 +00:07:24,000 --> 00:07:26,000 +Instead of adding those two numbers, + +193 +00:07:26,000 --> 00:07:29,000 +it should also add 1 at the end, okay. + +194 +00:07:29,000 --> 00:07:33,000 +So if I compile this code now and run, it'll print 8. + +195 +00:07:33,000 --> 00:07:37,000 +So this add is overriding the add method of calculator. + +196 +00:07:37,000 --> 00:07:40,000 +Okay, just remove this statement, that's extra. + +197 +00:07:40,000 --> 00:07:44,000 +So this concept is called method overriding. + +198 +00:07:44,000 --> 00:07:47,000 +So this method is overriding the method here. + +199 +00:07:48,000 --> 00:07:50,000 +I hope that makes sense. + +200 +00:07:50,000 --> 00:07:52,000 +So this is method overriding. + +201 +00:07:52,000 --> 00:07:54,000 +In fact, there is one more trick here, + +202 +00:07:54,000 --> 00:07:56,000 +which we'll discuss in the next video. + diff --git a/02 - Core Java/056 Source-code.url b/02 - Core Java/056 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/056 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/057 Packages_en.srt b/02 - Core Java/057 Packages_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..dc6caddf47e887e266155abe5fd3eda5dae151fc --- /dev/null +++ b/02 - Core Java/057 Packages_en.srt @@ -0,0 +1,1252 @@ +1 +00:00:00,000 --> 00:00:03,000 +(soft instrumental music) + +2 +00:00:03,000 --> 00:00:05,000 +-: In this video let's talk about packages. + +3 +00:00:05,000 --> 00:00:07,000 +The thing is, whenever you have a file, + +4 +00:00:07,000 --> 00:00:11,000 +it doesn't matter is it music files or documents or code, + +5 +00:00:11,000 --> 00:00:13,000 +we normally put them in a folder, right? + +6 +00:00:13,000 --> 00:00:16,000 +And then we create a folder structure as well. + +7 +00:00:16,000 --> 00:00:19,000 +In fact, I remember in my college days when we used + +8 +00:00:19,000 --> 00:00:20,000 +to store different songs + +9 +00:00:20,000 --> 00:00:22,000 +we used to create different folders. + +10 +00:00:22,000 --> 00:00:26,000 +Like we have a Songs folder, then inside that we have + +11 +00:00:26,000 --> 00:00:30,000 +Rock Songs, DJ Songs, Sad Songs, Romantic Songs. + +12 +00:00:30,000 --> 00:00:33,000 +And in fact in that as well, when you go to Sad Songs + +13 +00:00:33,000 --> 00:00:36,000 +we can have Normal Sad Songs, Ultimate Sad Songs. + +14 +00:00:36,000 --> 00:00:38,000 +Okay, now that's exaggeration, but we do that, right? + +15 +00:00:38,000 --> 00:00:39,000 +We create a structure there. + +16 +00:00:39,000 --> 00:00:42,000 +In the same way, when you talk about Java project + +17 +00:00:42,000 --> 00:00:44,000 +you will not be creating one or two files, right? + +18 +00:00:44,000 --> 00:00:46,000 +It will be having multiple files. + +19 +00:00:46,000 --> 00:00:48,000 +Sometimes your project can have + +20 +00:00:48,000 --> 00:00:51,000 +let's say 200 files, 300 files. + +21 +00:00:51,000 --> 00:00:52,000 +Okay, maybe most of the time + +22 +00:00:52,000 --> 00:00:54,000 +when you have a complex project. + +23 +00:00:54,000 --> 00:00:57,000 +Now, how will you put all these files in one folder? + +24 +00:00:57,000 --> 00:00:59,000 +You can actually, you can put everything in one folder. + +25 +00:00:59,000 --> 00:01:02,000 +Now, if you see our project structure here, + +26 +00:01:02,000 --> 00:01:04,000 +what we did is we have this folder called CODES. + +27 +00:01:04,000 --> 00:01:07,000 +And in this CODES we have all of the files. + +28 +00:01:07,000 --> 00:01:09,000 +In fact, we only have three files now, + +29 +00:01:09,000 --> 00:01:11,000 +but just imagine we have more files. + +30 +00:01:11,000 --> 00:01:13,000 +Imagine if we have 100 files. + +31 +00:01:13,000 --> 00:01:15,000 +It's okay to put them in one folder, + +32 +00:01:15,000 --> 00:01:18,000 +but it's always better if you can create a structure + +33 +00:01:18,000 --> 00:01:21,000 +where you can have few files in one folder + +34 +00:01:21,000 --> 00:01:23,000 +and few files in another folder. + +35 +00:01:23,000 --> 00:01:26,000 +And then you can have a folder inside a folder. + +36 +00:01:26,000 --> 00:01:27,000 +Now, normally this type of pattern + +37 +00:01:27,000 --> 00:01:29,000 +in Java is called packages. + +38 +00:01:29,000 --> 00:01:32,000 +Normally you store files in a package. + +39 +00:01:32,000 --> 00:01:35,000 +So, yeah, that's right, the folder is package here. + +40 +00:01:35,000 --> 00:01:38,000 +And of course one package can be a part of another package + +41 +00:01:38,000 --> 00:01:40,000 +the way you have folders inside a folder. + +42 +00:01:40,000 --> 00:01:42,000 +So, how do we do that here in Java? + +43 +00:01:42,000 --> 00:01:44,000 +So, just to make you understand, + +44 +00:01:44,000 --> 00:01:45,000 +let's say we have these two files, + +45 +00:01:45,000 --> 00:01:47,000 +AdvancedCalculator and Calculator. + +46 +00:01:47,000 --> 00:01:49,000 +Now, these two files are related, right? + +47 +00:01:49,000 --> 00:01:51,000 +They both work for the calculator, + +48 +00:01:51,000 --> 00:01:55,000 +so it is a good idea to put them in one folder. + +49 +00:01:55,000 --> 00:01:56,000 +Now, how will you do that? + +50 +00:01:56,000 --> 00:01:58,000 +You have an option here of creating a new folder. + +51 +00:01:58,000 --> 00:01:59,000 +That's one way. + +52 +00:01:59,000 --> 00:02:01,000 +Or you can, what you can do is you can just come back + +53 +00:02:01,000 --> 00:02:04,000 +on top of your class and say you can create a package. + +54 +00:02:04,000 --> 00:02:06,000 +So, the keyword which you use + +55 +00:02:06,000 --> 00:02:07,000 +to create a package is called package, + +56 +00:02:07,000 --> 00:02:10,000 +and you can put this class in that particular package. + +57 +00:02:10,000 --> 00:02:13,000 +Let's say the package name is tools, right? + +58 +00:02:13,000 --> 00:02:15,000 +So, of course you can create any package name, + +59 +00:02:15,000 --> 00:02:17,000 +but it should have an, it should make some sense. + +60 +00:02:17,000 --> 00:02:21,000 +So, you can see I'm saying that they are tools, okay? + +61 +00:02:21,000 --> 00:02:24,000 +And the thing is you can see we are getting an error. + +62 +00:02:24,000 --> 00:02:26,000 +It says that declared package tools + +63 +00:02:26,000 --> 00:02:29,000 +does not match the expected package. + +64 +00:02:29,000 --> 00:02:33,000 +The thing is you don't have Calc inside the package. + +65 +00:02:33,000 --> 00:02:35,000 +In fact, most of the IDs will give you an option + +66 +00:02:35,000 --> 00:02:39,000 +of directly, if I click on Quick Fix, yeah. + +67 +00:02:39,000 --> 00:02:40,000 +So, you can see most of the IDs, + +68 +00:02:40,000 --> 00:02:42,000 +doesn't matter which ID you're using, + +69 +00:02:42,000 --> 00:02:44,000 +in VS Code we have this option. + +70 +00:02:44,000 --> 00:02:46,000 +In most of the famous ID you will get this option + +71 +00:02:46,000 --> 00:02:50,000 +of doing something, some red symbol or something like that. + +72 +00:02:50,000 --> 00:02:52,000 +Here we have an option of Quick Fix. + +73 +00:02:52,000 --> 00:02:57,000 +If I click on that it says Move Calc.java to package tools. + +74 +00:02:57,000 --> 00:02:59,000 +Okay, that is a good idea. + +75 +00:02:59,000 --> 00:03:00,000 +We can click on this, + +76 +00:03:00,000 --> 00:03:04,000 +and you can see it created a folder called tools. + +77 +00:03:04,000 --> 00:03:07,000 +And now we can actually move this to tools as well. + +78 +00:03:07,000 --> 00:03:09,000 +The only thing is when you do that + +79 +00:03:09,000 --> 00:03:13,000 +and when you go to this particular class, okay, + +80 +00:03:13,000 --> 00:03:14,000 +it has already done the changes. + +81 +00:03:14,000 --> 00:03:16,000 +You can see I have not written this. + +82 +00:03:16,000 --> 00:03:19,000 +It is already there thanks to VS Code and the IDE, + +83 +00:03:20,000 --> 00:03:21,000 +so basically you get this here. + +84 +00:03:21,000 --> 00:03:23,000 +So, it's always a good idea + +85 +00:03:23,000 --> 00:03:25,000 +to put your classes in a package. + +86 +00:03:25,000 --> 00:03:28,000 +You can create a complex structure. + +87 +00:03:28,000 --> 00:03:31,000 +If you want you can create one more folder here. + +88 +00:03:31,000 --> 00:03:35,000 +Let's say we got a folder called database, + +89 +00:03:35,000 --> 00:03:37,000 +or maybe dbconnections or dbclasses, + +90 +00:03:37,000 --> 00:03:39,000 +or maybe you can just simply say db. + +91 +00:03:39,000 --> 00:03:41,000 +Now, this db will have all the database connections. + +92 +00:03:41,000 --> 00:03:44,000 +So, let's say if you want to create some files + +93 +00:03:44,000 --> 00:03:47,000 +which will connect with MySQL, Oracle or, + +94 +00:03:47,000 --> 00:03:48,000 +it does something with database, + +95 +00:03:48,000 --> 00:03:52,000 +so you can put all those classes in database folder, okay? + +96 +00:03:52,000 --> 00:03:55,000 +Now, that's how you create a project structure here, okay? + +97 +00:03:55,000 --> 00:03:57,000 +The only thing is if you want, + +98 +00:03:57,000 --> 00:03:58,000 +if you have this structure here. + +99 +00:03:58,000 --> 00:04:01,000 +Let's say if you have Calc and Demo, AdvancedCalc, + +100 +00:04:01,000 --> 00:04:03,000 +and if you can see both are in a package now. + +101 +00:04:03,000 --> 00:04:07,000 +The only problem we will face is if you go to Demo, + +102 +00:04:07,000 --> 00:04:10,000 +and you can see Demo is not a part of any package. + +103 +00:04:10,000 --> 00:04:13,000 +Normally you put the main class, + +104 +00:04:13,000 --> 00:04:16,000 +so the class which has a main method separately, + +105 +00:04:16,000 --> 00:04:17,000 +outside the package so that you can + +106 +00:04:17,000 --> 00:04:19,000 +easily access other classes. + +107 +00:04:19,000 --> 00:04:21,000 +Okay, when I say easily access other classes can I do that? + +108 +00:04:21,000 --> 00:04:24,000 +Can I create object of Calc here? + +109 +00:04:24,000 --> 00:04:26,000 +So, if I say Calc obj equal to new Calc, + +110 +00:04:26,000 --> 00:04:28,000 +which was working earlier, + +111 +00:04:28,000 --> 00:04:31,000 +if you remember our previous examples. + +112 +00:04:31,000 --> 00:04:32,000 +It's not working now. + +113 +00:04:32,000 --> 00:04:34,000 +You can see it says Calc cannot be resolved. + +114 +00:04:34,000 --> 00:04:35,000 +What's wrong? + +115 +00:04:35,000 --> 00:04:38,000 +The problem is Demo and Calc, + +116 +00:04:38,000 --> 00:04:40,000 +now they are in two different folders. + +117 +00:04:40,000 --> 00:04:42,000 +You can't directly access it. + +118 +00:04:42,000 --> 00:04:44,000 +Okay, so when I say you cannot directly access it + +119 +00:04:44,000 --> 00:04:46,000 +do we have an indirect way? + +120 +00:04:46,000 --> 00:04:48,000 +Yes, we have an indirect way. + +121 +00:04:48,000 --> 00:04:51,000 +So, what you can do is if you see this bubble + +122 +00:04:51,000 --> 00:04:53,000 +it says Import Calc, that's right. + +123 +00:04:53,000 --> 00:04:56,000 +If you want to use Calc in your particular class + +124 +00:04:56,000 --> 00:04:58,000 +you have to say import Calc. + +125 +00:04:58,000 --> 00:05:00,000 +But then Calc belong where? + +126 +00:05:00,000 --> 00:05:03,000 +Calc belongs to tools.Calc. + +127 +00:05:03,000 --> 00:05:05,000 +So, basically you have to mention the package name + +128 +00:05:05,000 --> 00:05:07,000 +and then you have to mention the class name, + +129 +00:05:07,000 --> 00:05:08,000 +whichever you want to import. + +130 +00:05:08,000 --> 00:05:10,000 +And now you can see there's no issue. + +131 +00:05:10,000 --> 00:05:12,000 +How about AdvancedCalculator? + +132 +00:05:12,000 --> 00:05:14,000 +If I want to use AdvancedCalculator, + +133 +00:05:14,000 --> 00:05:17,000 +obj1 is equal to new AdvancedCalculator. + +134 +00:05:17,000 --> 00:05:20,000 +Okay, now you cannot access this one, you know why? + +135 +00:05:20,000 --> 00:05:23,000 +It's because we have only imported Calc, + +136 +00:05:23,000 --> 00:05:25,000 +not AdvancedCalculator. + +137 +00:05:25,000 --> 00:05:26,000 +How will you import this? + +138 +00:05:26,000 --> 00:05:27,000 +You're right. + +139 +00:05:27,000 --> 00:05:30,000 +Just come back here and say import tools.AdvancedCalculator. + +140 +00:05:31,000 --> 00:05:33,000 +So, whichever class you want to use + +141 +00:05:33,000 --> 00:05:35,000 +you can just import them. + +142 +00:05:35,000 --> 00:05:38,000 +In fact, Java internally has a lot of classes. + +143 +00:05:38,000 --> 00:05:39,000 +Example, let's say one of the classes, + +144 +00:05:39,000 --> 00:05:42,000 +which we're going to see in future, which is let's say list. + +145 +00:05:42,000 --> 00:05:46,000 +Now, when we use list, which is inbuilt class, + +146 +00:05:46,000 --> 00:05:47,000 +or inbuilt interface, what is interface? + +147 +00:05:47,000 --> 00:05:48,000 +We'll talk about that later. + +148 +00:05:48,000 --> 00:05:50,000 +Or maybe at this point we can talk about + +149 +00:05:50,000 --> 00:05:53,000 +let's say ArrayList, which is a class. + +150 +00:05:53,000 --> 00:05:56,000 +When we use ArrayList and when you create object of it, + +151 +00:05:56,000 --> 00:05:59,000 +I will say list is equal to new ArrayList. + +152 +00:06:00,000 --> 00:06:01,000 +Now, the moment you do that + +153 +00:06:01,000 --> 00:06:03,000 +it will also come from a package. + +154 +00:06:03,000 --> 00:06:06,000 +You can see it is now given by the IDE, + +155 +00:06:06,000 --> 00:06:08,000 +it is auto-completing it. + +156 +00:06:08,000 --> 00:06:10,000 +But if I remove this line you can see + +157 +00:06:10,000 --> 00:06:10,000 +it will give you error. + +158 +00:06:10,000 --> 00:06:12,000 +It says ArrayList cannot be found. + +159 +00:06:12,000 --> 00:06:14,000 +So, we have to import this one as well, + +160 +00:06:14,000 --> 00:06:16,000 +and this belongs to a package. + +161 +00:06:16,000 --> 00:06:21,000 +Now, that package is import java.util.ArrayList. + +162 +00:06:22,000 --> 00:06:23,000 +This you have to import. + +163 +00:06:23,000 --> 00:06:26,000 +Now, you might be thinking why we have two dots here, + +164 +00:06:26,000 --> 00:06:28,000 +java.util, how it looks like. + +165 +00:06:28,000 --> 00:06:30,000 +So, basically we have a Java folder. + +166 +00:06:30,000 --> 00:06:33,000 +Inside Java folder we have a util folder. + +167 +00:06:33,000 --> 00:06:36,000 +So, it's the folder inside the folder, + +168 +00:06:36,000 --> 00:06:38,000 +and Java has multiple folders if you can see. + +169 +00:06:38,000 --> 00:06:41,000 +Java., we have applet folder, + +170 +00:06:41,000 --> 00:06:46,000 +we have awt folder, beans folder, lang folder, io folder, + +171 +00:06:46,000 --> 00:06:47,000 +and all those things. + +172 +00:06:47,000 --> 00:06:51,000 +So, in general whatever class you use in Java + +173 +00:06:51,000 --> 00:06:53,000 +belongs to a package. + +174 +00:06:53,000 --> 00:06:55,000 +Let me repeat, whichever class, + +175 +00:06:55,000 --> 00:06:59,000 +inbuilt class which you use in Java, belongs to a package. + +176 +00:06:59,000 --> 00:07:02,000 +Now you'll be saying okay, everything makes sense, + +177 +00:07:02,000 --> 00:07:05,000 +but don't you think String is also a class? + +178 +00:07:05,000 --> 00:07:09,000 +And one of our favorite class, which is System. + +179 +00:07:09,000 --> 00:07:11,000 +System is also a class, right? + +180 +00:07:11,000 --> 00:07:14,000 +So, when we are using System we have never imported it. + +181 +00:07:14,000 --> 00:07:16,000 +So, when I say System.out.println, + +182 +00:07:17,000 --> 00:07:19,000 +there was no need to import the package. + +183 +00:07:19,000 --> 00:07:21,000 +But does System belong to a package? + +184 +00:07:21,000 --> 00:07:22,000 +And the answer is yes. + +185 +00:07:22,000 --> 00:07:27,000 +If I go to System and if I go on top here, can you see that? + +186 +00:07:27,000 --> 00:07:28,000 +It says package. + +187 +00:07:28,000 --> 00:07:30,000 +So, this class, System class, + +188 +00:07:30,000 --> 00:07:32,000 +belongs to our package java.lang, + +189 +00:07:32,000 --> 00:07:33,000 +but we never imported it. + +190 +00:07:33,000 --> 00:07:37,000 +The thing is, by default every Java file will have + +191 +00:07:37,000 --> 00:07:40,000 +java.lang by default imported. + +192 +00:07:40,000 --> 00:07:42,000 +So, even if you don't mention that it's there. + +193 +00:07:42,000 --> 00:07:45,000 +This line is there even if you don't mention. + +194 +00:07:45,000 --> 00:07:48,000 +Now, the thing is it's actually this, not this line. + +195 +00:07:48,000 --> 00:07:52,000 +What is by default is this, java.lang.*. + +196 +00:07:52,000 --> 00:07:53,000 +Now, what is this star? + +197 +00:07:53,000 --> 00:07:56,000 +Now, in one particular folder if you have multiple files, + +198 +00:07:56,000 --> 00:08:00,000 +and if you want all the files you can simply say star + +199 +00:08:00,000 --> 00:08:01,000 +just like we have tools here. + +200 +00:08:01,000 --> 00:08:03,000 +In tools we only have two files, + +201 +00:08:03,000 --> 00:08:05,000 +and we are importing both. + +202 +00:08:05,000 --> 00:08:06,000 +So, can we just do that? + +203 +00:08:06,000 --> 00:08:07,000 +Yes, we can. + +204 +00:08:07,000 --> 00:08:10,000 +So, we can simply put a star here, our job is done. + +205 +00:08:10,000 --> 00:08:13,000 +And by doing that I also removed the list. + +206 +00:08:13,000 --> 00:08:15,000 +We don't even need that, you know that. + +207 +00:08:15,000 --> 00:08:17,000 +So, basically if you want to import all the files + +208 +00:08:17,000 --> 00:08:19,000 +you can say star, okay? + +209 +00:08:19,000 --> 00:08:22,000 +So, that's how you can create a package in Java. + +210 +00:08:22,000 --> 00:08:24,000 +As I mentioned, you can create a folder inside a folder. + +211 +00:08:24,000 --> 00:08:27,000 +Example, let's say if I have a folder called other, + +212 +00:08:27,000 --> 00:08:29,000 +so let's say we have other folder. + +213 +00:08:29,000 --> 00:08:32,000 +And this tools actually belongs to other, + +214 +00:08:32,000 --> 00:08:34,000 +so now if I move this you can see + +215 +00:08:34,000 --> 00:08:36,000 +we have tools inside other. + +216 +00:08:36,000 --> 00:08:40,000 +Now, how it will look like when you talk about your Calc, + +217 +00:08:40,000 --> 00:08:43,000 +so you can see it says other.Calc. + +218 +00:08:43,000 --> 00:08:45,000 +So, you have to, if you want to go inside the package, + +219 +00:08:45,000 --> 00:08:48,000 +normally in folder structure we use slash, here we use dot. + +220 +00:08:48,000 --> 00:08:52,000 +So, it is other.tools.Calc, okay? + +221 +00:08:52,000 --> 00:08:54,000 +That's how you have to import it. + +222 +00:08:54,000 --> 00:08:57,000 +And of course you can say star, it will import everything. + +223 +00:08:57,000 --> 00:09:01,000 +Now, my question to you is if I say import other.*, + +224 +00:09:02,000 --> 00:09:07,000 +now is these two line same or can I remove this? + +225 +00:09:07,000 --> 00:09:09,000 +Of course, when you say star it should import everything. + +226 +00:09:09,000 --> 00:09:12,000 +But no, if you can see, it is giving you error. + +227 +00:09:12,000 --> 00:09:15,000 +The reason is when you say other.* it, + +228 +00:09:15,000 --> 00:09:18,000 +so star simply means all the files + +229 +00:09:18,000 --> 00:09:20,000 +not all the folders, okay? + +230 +00:09:20,000 --> 00:09:22,000 +So, in other if you have some files, + +231 +00:09:22,000 --> 00:09:24,000 +let's say if I create a new file here. + +232 +00:09:24,000 --> 00:09:25,000 +I will name this as A.java. + +233 +00:09:25,000 --> 00:09:28,000 +The moment you do that you can see this A class + +234 +00:09:28,000 --> 00:09:29,000 +belongs to other package. + +235 +00:09:29,000 --> 00:09:32,000 +Now you can easily use A but not Calc. + +236 +00:09:32,000 --> 00:09:37,000 +So, if you say A obj2 is equal to new A, this will work. + +237 +00:09:37,000 --> 00:09:40,000 +It's because you are saying other.*, + +238 +00:09:40,000 --> 00:09:41,000 +it will import all the classes, + +239 +00:09:41,000 --> 00:09:44,000 +which are all the files in the other folder, + +240 +00:09:44,000 --> 00:09:46,000 +not inside the folder, which is tools. + +241 +00:09:46,000 --> 00:09:49,000 +So, if you want other, I mean tools as well, + +242 +00:09:49,000 --> 00:09:52,000 +so you have to mention other.tools.*. + +243 +00:09:53,000 --> 00:09:55,000 +So, that's how basically you use a package structure. + +244 +00:09:55,000 --> 00:09:57,000 +Now, there's one thing you know, + +245 +00:09:57,000 --> 00:09:58,000 +not important at this point, + +246 +00:09:58,000 --> 00:10:01,000 +but since we're talking about package let's talk about it + +247 +00:10:01,000 --> 00:10:02,000 +because you will be seeing + +248 +00:10:02,000 --> 00:10:04,000 +those type of structure multiple times. + +249 +00:10:04,000 --> 00:10:06,000 +When you create a project, a big project, + +250 +00:10:06,000 --> 00:10:08,000 +normally you deploy your project + +251 +00:10:08,000 --> 00:10:10,000 +on the server somewhere, right? + +252 +00:10:10,000 --> 00:10:12,000 +And maybe you're creating a library, that's right. + +253 +00:10:12,000 --> 00:10:14,000 +What you do is when you create a project + +254 +00:10:14,000 --> 00:10:17,000 +it's not always for your use. + +255 +00:10:17,000 --> 00:10:18,000 +Sometime you create libraries + +256 +00:10:18,000 --> 00:10:20,000 +so that others can use your files. + +257 +00:10:20,000 --> 00:10:23,000 +Now, in this case as well you will create a package, + +258 +00:10:23,000 --> 00:10:26,000 +and then you will give your package to others. + +259 +00:10:26,000 --> 00:10:28,000 +The only thing is when you are sharing your package + +260 +00:10:28,000 --> 00:10:31,000 +to the entire world it should be unique, + +261 +00:10:31,000 --> 00:10:32,000 +it should have a unique name. + +262 +00:10:32,000 --> 00:10:33,000 +Let me show you something. + +263 +00:10:33,000 --> 00:10:35,000 +I will go to a website. + +264 +00:10:35,000 --> 00:10:36,000 +Now, this is one of the website + +265 +00:10:36,000 --> 00:10:39,000 +where you can get the libraries. + +266 +00:10:39,000 --> 00:10:42,000 +Now, library simply means if you want to do something, + +267 +00:10:42,000 --> 00:10:45,000 +and if you don't want to write the code entirely by yourself + +268 +00:10:45,000 --> 00:10:47,000 +you can use some libraries. + +269 +00:10:47,000 --> 00:10:49,000 +And most of the time we do that. + +270 +00:10:49,000 --> 00:10:51,000 +Example, let's say if I want to connect my application + +271 +00:10:51,000 --> 00:10:53,000 +with MongoDB, which is a database. + +272 +00:10:53,000 --> 00:10:55,000 +So, I can simply search for mongodb-driver-sync. + +273 +00:10:55,000 --> 00:10:58,000 +This is a driver which we use to connect Java with MongoDB. + +274 +00:10:58,000 --> 00:11:01,000 +So, you can use this package in your project. + +275 +00:11:01,000 --> 00:11:03,000 +Okay, let me select a version. + +276 +00:11:03,000 --> 00:11:07,000 +So, this is the package, okay? + +277 +00:11:07,000 --> 00:11:10,000 +Now, the thing is when we talk about this package + +278 +00:11:10,000 --> 00:11:11,000 +what should be name of it? + +279 +00:11:11,000 --> 00:11:14,000 +So, this name should be unique on the internet. + +280 +00:11:14,000 --> 00:11:16,000 +Now, how will you make something unique? + +281 +00:11:16,000 --> 00:11:17,000 +That's the tricky point, right? + +282 +00:11:17,000 --> 00:11:20,000 +So, when you are creating a project + +283 +00:11:20,000 --> 00:11:21,000 +how will you make it unique? + +284 +00:11:21,000 --> 00:11:24,000 +So, one way to do that is by reversing your domain name. + +285 +00:11:24,000 --> 00:11:24,000 +That's right. + +286 +00:11:24,000 --> 00:11:26,000 +Of course, right, when you're building a library + +287 +00:11:26,000 --> 00:11:29,000 +you will also have your website, your company website maybe. + +288 +00:11:29,000 --> 00:11:31,000 +So, let's say if we talk about a company like Google. + +289 +00:11:31,000 --> 00:11:34,000 +So, if I'm working for Google and if I'm creating a library, + +290 +00:11:34,000 --> 00:11:36,000 +so what I will do is I will name, + +291 +00:11:36,000 --> 00:11:38,000 +I will say this belongs to a package. + +292 +00:11:38,000 --> 00:11:41,000 +So, if I'm making a project for let's say Google + +293 +00:11:41,000 --> 00:11:44,000 +and I'm making a calculator or advanced calculator, + +294 +00:11:44,000 --> 00:11:49,000 +I will say com.google.calculation. + +295 +00:11:49,000 --> 00:11:50,000 +Now, this is my package name, okay? + +296 +00:11:50,000 --> 00:11:53,000 +So, by making com.google I'm making sure + +297 +00:11:53,000 --> 00:11:54,000 +it is unique, right? + +298 +00:11:54,000 --> 00:11:56,000 +Only Google can own this domain, right? + +299 +00:11:56,000 --> 00:11:57,000 +It's their domain. + +300 +00:11:57,000 --> 00:11:58,000 +So, that's how you do that. + +301 +00:11:58,000 --> 00:12:00,000 +So, depending upon your project + +302 +00:12:00,000 --> 00:12:01,000 +you will see these type of packages. + +303 +00:12:01,000 --> 00:12:04,000 +In fact, in future videos we will be using some packages + +304 +00:12:04,000 --> 00:12:06,000 +which have com. something. + +305 +00:12:06,000 --> 00:12:08,000 +It means they have just reverse the domain name + +306 +00:12:08,000 --> 00:12:09,000 +just to make it unique. + +307 +00:12:09,000 --> 00:12:10,000 +But again, for learning purpose + +308 +00:12:10,000 --> 00:12:12,000 +you can name your package anything you want. + +309 +00:12:12,000 --> 00:12:15,000 +Example, the way we are doing here, package other, + +310 +00:12:15,000 --> 00:12:17,000 +but it should have a meaning that's important. + +311 +00:12:17,000 --> 00:12:18,000 +So, yeah, that's about packages. + +312 +00:12:18,000 --> 00:12:20,000 +Don't worry, we'll use packages more. + +313 +00:12:20,000 --> 00:12:23,000 +In the upcoming videos it will make much more sense. + diff --git a/02 - Core Java/057 Source-code.url b/02 - Core Java/057 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/057 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/058 Access Modifiers_en.srt b/02 - Core Java/058 Access Modifiers_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..a9fc993d8b838780f523737e7ab652900f988a78 --- /dev/null +++ b/02 - Core Java/058 Access Modifiers_en.srt @@ -0,0 +1,860 @@ +1 +00:00:00,000 --> 00:00:03,000 +(upbeat instrumental music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now once we talked about packages, it's a good time + +3 +00:00:05,000 --> 00:00:08,000 +to talk about access modifiers. + +4 +00:00:08,000 --> 00:00:09,000 +Now one of the access modifier we have seen, + +5 +00:00:09,000 --> 00:00:13,000 +in fact we have seen two, private and public. + +6 +00:00:13,000 --> 00:00:16,000 +Apart from this we have more, we have protected + +7 +00:00:16,000 --> 00:00:17,000 +and default as well. + +8 +00:00:17,000 --> 00:00:19,000 +So in this video let's try to explore them + +9 +00:00:19,000 --> 00:00:20,000 +or one or two videos. + +10 +00:00:20,000 --> 00:00:23,000 +So what I'll do is initially I will start with public. + +11 +00:00:23,000 --> 00:00:24,000 +Now what is public basically? + +12 +00:00:24,000 --> 00:00:25,000 +Let's go to demo + +13 +00:00:25,000 --> 00:00:28,000 +and we were trying to input the packages + +14 +00:00:28,000 --> 00:00:30,000 +and of course we should do that, right? + +15 +00:00:30,000 --> 00:00:31,000 +Anyway, we don't have to do that. + +16 +00:00:31,000 --> 00:00:33,000 +Java languages, it comes already. + +17 +00:00:33,000 --> 00:00:35,000 +So we don't have to worry about it. + +18 +00:00:35,000 --> 00:00:39,000 +And now what I will do is I will just go back to my class A. + +19 +00:00:39,000 --> 00:00:41,000 +Let's work with class A now. + +20 +00:00:41,000 --> 00:00:42,000 +And in this class A, + +21 +00:00:42,000 --> 00:00:45,000 +you can see the class A is public, okay? + +22 +00:00:45,000 --> 00:00:47,000 +And it belongs to a different package called other. + +23 +00:00:47,000 --> 00:00:52,000 +And in this class I will just create a variable called int, + +24 +00:00:52,000 --> 00:00:53,000 +let's say Marx. + +25 +00:00:53,000 --> 00:00:55,000 +So we have this variable called marks, + +26 +00:00:55,000 --> 00:00:56,000 +and the initial value for marks is six + +27 +00:00:56,000 --> 00:00:58,000 +because we just want to access this, + +28 +00:00:58,000 --> 00:00:59,000 +is it working or not? + +29 +00:00:59,000 --> 00:01:01,000 +Now how will you access this variable? + +30 +00:01:01,000 --> 00:01:03,000 +So you can just go back to demo. + +31 +00:01:03,000 --> 00:01:06,000 +So we have add, we have A, right, which has this code, + +32 +00:01:06,000 --> 00:01:07,000 +which is simple class. + +33 +00:01:07,000 --> 00:01:09,000 +And then we have our marks. + +34 +00:01:09,000 --> 00:01:10,000 +Let me go back to demo. + +35 +00:01:10,000 --> 00:01:12,000 +And in this, I want to access it. + +36 +00:01:12,000 --> 00:01:15,000 +So I will just remove all this lines, just want to work. + +37 +00:01:15,000 --> 00:01:17,000 +In fact, tools is also not a need now, + +38 +00:01:17,000 --> 00:01:19,000 +let's only work with other. + +39 +00:01:19,000 --> 00:01:21,000 +And in this, let me create a object of A. + +40 +00:01:21,000 --> 00:01:23,000 +Now can I create object of A? + +41 +00:01:23,000 --> 00:01:25,000 +Yes we can because we can access it. + +42 +00:01:25,000 --> 00:01:30,000 +And then I'm just trying to say obj.marks. + +43 +00:01:30,000 --> 00:01:31,000 +Now, trying to do that, + +44 +00:01:31,000 --> 00:01:33,000 +you can see it gives you an error. + +45 +00:01:33,000 --> 00:01:37,000 +It says, "The field A.marks is not visible." + +46 +00:01:37,000 --> 00:01:38,000 +I mean that's weird. + +47 +00:01:38,000 --> 00:01:39,000 +It's there, right? + +48 +00:01:39,000 --> 00:01:40,000 +Marks is there. + +49 +00:01:40,000 --> 00:01:42,000 +The thing is, it is not public. + +50 +00:01:42,000 --> 00:01:43,000 +Now, when it is not public, + +51 +00:01:43,000 --> 00:01:45,000 +you cannot access it outside the package. + +52 +00:01:45,000 --> 00:01:47,000 +You can see that's the issue we got. + +53 +00:01:47,000 --> 00:01:50,000 +Now what if you have a class in the same file + +54 +00:01:50,000 --> 00:01:54,000 +or maybe in the same package, let's say in the same package, + +55 +00:01:54,000 --> 00:01:55,000 +which is of demo, + +56 +00:01:55,000 --> 00:01:58,000 +I will create a file called B.java. + +57 +00:01:58,000 --> 00:02:01,000 +And in B.java, let me just say + +58 +00:02:01,000 --> 00:02:02,000 +because they're the same package now. + +59 +00:02:02,000 --> 00:02:05,000 +So no package is same package basically. + +60 +00:02:05,000 --> 00:02:10,000 +I will try to create a variable called int let's say marks. + +61 +00:02:10,000 --> 00:02:12,000 +Let's see if I can access this marks and demo. + +62 +00:02:12,000 --> 00:02:13,000 +I will just go back here + +63 +00:02:13,000 --> 00:02:16,000 +and I will repeat the same sentence, same line. + +64 +00:02:16,000 --> 00:02:20,000 +In fact, two, same statement with the help of B object now. + +65 +00:02:20,000 --> 00:02:21,000 +This is B. + +66 +00:02:21,000 --> 00:02:23,000 +The only thing is we have to use a different object name + +67 +00:02:23,000 --> 00:02:24,000 +because we cannot have same. + +68 +00:02:24,000 --> 00:02:27,000 +And using this OBJ can access marks. + +69 +00:02:27,000 --> 00:02:31,000 +So you see there is no issue, even if marks is not public, + +70 +00:02:31,000 --> 00:02:34,000 +it's no issue because they are in the same package. + +71 +00:02:34,000 --> 00:02:36,000 +The moment you have a different package + +72 +00:02:36,000 --> 00:02:39,000 +and if you don't mention public, it'll not work. + +73 +00:02:39,000 --> 00:02:40,000 +Okay, that's the issue. + +74 +00:02:40,000 --> 00:02:41,000 +You can see it is not working. + +75 +00:02:41,000 --> 00:02:44,000 +Now if you want something to be accessed + +76 +00:02:44,000 --> 00:02:47,000 +outside the package, always make it public. + +77 +00:02:47,000 --> 00:02:50,000 +And that's why methods, most of the time they're public, + +78 +00:02:50,000 --> 00:02:52,000 +variables should not be public. + +79 +00:02:52,000 --> 00:02:53,000 +But methods should be public. + +80 +00:02:53,000 --> 00:02:56,000 +Example, if I say public wide show here, + +81 +00:02:56,000 --> 00:02:58,000 +now show can, show is accessible. + +82 +00:02:58,000 --> 00:02:59,000 +I can use show from anywhere. + +83 +00:02:59,000 --> 00:03:04,000 +Example, if I can come back here obj.show, + +84 +00:03:04,000 --> 00:03:05,000 +this will work because that's public. + +85 +00:03:05,000 --> 00:03:08,000 +But if I don't write public there, it'll not work. + +86 +00:03:08,000 --> 00:03:09,000 +That's one. + +87 +00:03:09,000 --> 00:03:10,000 +So public means it can be + +88 +00:03:10,000 --> 00:03:12,000 +accessed from anywhere. + +89 +00:03:12,000 --> 00:03:13,000 +Okay? + +90 +00:03:13,000 --> 00:03:17,000 +Now apart from public, we also have private. + +91 +00:03:17,000 --> 00:03:21,000 +So if I talk about private, can I use private anywhere? + +92 +00:03:21,000 --> 00:03:24,000 +Private variable can be used in the same class + +93 +00:03:24,000 --> 00:03:25,000 +irrespective of the package. + +94 +00:03:25,000 --> 00:03:27,000 +Okay, package is the secondary part. + +95 +00:03:27,000 --> 00:03:31,000 +It can be used in the same class. + +96 +00:03:31,000 --> 00:03:34,000 +I can't even use outside the class, example, + +97 +00:03:34,000 --> 00:03:36,000 +let's say if you look at this B, + +98 +00:03:36,000 --> 00:03:37,000 +and if you look at this demo, + +99 +00:03:37,000 --> 00:03:39,000 +they're the same package, right? + +100 +00:03:39,000 --> 00:03:42,000 +But now if I create a private variable that here, + +101 +00:03:42,000 --> 00:03:44,000 +if I make this as a private variable, + +102 +00:03:45,000 --> 00:03:48,000 +which you should always mark your variables as private, + +103 +00:03:48,000 --> 00:03:49,000 +the moment you make it private, + +104 +00:03:49,000 --> 00:03:52,000 +even if they're the same package, you can't access it. + +105 +00:03:52,000 --> 00:03:55,000 +So private variables can be used in the same class + +106 +00:03:55,000 --> 00:03:57,000 +irrespective of the package. + +107 +00:03:57,000 --> 00:03:58,000 +It's on the same class. + +108 +00:03:58,000 --> 00:04:00,000 +Now let me reiterate, when you talk about public, + +109 +00:04:00,000 --> 00:04:03,000 +it can be used from anywhere, okay? + +110 +00:04:03,000 --> 00:04:06,000 +You can see this table, public can be used from anywhere. + +111 +00:04:06,000 --> 00:04:10,000 +Private can be used from the same class, okay? + +112 +00:04:10,000 --> 00:04:14,000 +Now apart from public and private, we have two more, okay? + +113 +00:04:14,000 --> 00:04:15,000 +Let's talk about those now. + +114 +00:04:15,000 --> 00:04:19,000 +Now if we talk about this A, if we don't mention private, + +115 +00:04:19,000 --> 00:04:21,000 +something should be applicable, right? + +116 +00:04:21,000 --> 00:04:24,000 +If it is not private, it's not public, what is it? + +117 +00:04:24,000 --> 00:04:28,000 +So by default there is something called a default + +118 +00:04:28,000 --> 00:04:29,000 +access modifier, okay? + +119 +00:04:29,000 --> 00:04:31,000 +Which is also called private protected, + +120 +00:04:31,000 --> 00:04:33,000 +but we'll talk about that later. + +121 +00:04:33,000 --> 00:04:35,000 +But it is called default. + +122 +00:04:35,000 --> 00:04:37,000 +The default simply means it can be accessed + +123 +00:04:37,000 --> 00:04:39,000 +in the same package. + +124 +00:04:39,000 --> 00:04:40,000 +It's as simple as that. + +125 +00:04:40,000 --> 00:04:42,000 +It can be accessed in the same package. + +126 +00:04:42,000 --> 00:04:43,000 +Example, if you talk about this B, + +127 +00:04:43,000 --> 00:04:45,000 +when private was not there, it was able + +128 +00:04:45,000 --> 00:04:48,000 +to be accessed in the same package. + +129 +00:04:48,000 --> 00:04:51,000 +So we got private, which can be accessed in the same class. + +130 +00:04:51,000 --> 00:04:54,000 +We got public, which can be accessed from anywhere. + +131 +00:04:54,000 --> 00:04:56,000 +And then we got private, + +132 +00:04:56,000 --> 00:04:58,000 +which can only be accessed in the same class + +133 +00:04:58,000 --> 00:05:00,000 +and default in the same package. + +134 +00:05:00,000 --> 00:05:03,000 +Okay, apart from this, we have one more, + +135 +00:05:03,000 --> 00:05:04,000 +which is called protected. + +136 +00:05:04,000 --> 00:05:06,000 +Now what is protected? + +137 +00:05:06,000 --> 00:05:08,000 +Now, let's say we have this class A, + +138 +00:05:08,000 --> 00:05:10,000 +which has, let's say if I make this variable + +139 +00:05:10,000 --> 00:05:12,000 +as protected, okay? + +140 +00:05:12,000 --> 00:05:15,000 +And then I'm going back to demo. + +141 +00:05:15,000 --> 00:05:17,000 +And inside this demo somewhere I'm creating a class + +142 +00:05:17,000 --> 00:05:18,000 +in the same package, basically, + +143 +00:05:18,000 --> 00:05:20,000 +it doesn't have to be in the same class + +144 +00:05:20,000 --> 00:05:23,000 +or same file, in a different package, basically. + +145 +00:05:23,000 --> 00:05:25,000 +Let me create class called C. + +146 +00:05:25,000 --> 00:05:27,000 +And then I'm saying + +147 +00:05:27,000 --> 00:05:32,000 +that this class extends A. + +148 +00:05:32,000 --> 00:05:35,000 +Now in this class A, so you can see this is protected. + +149 +00:05:35,000 --> 00:05:38,000 +Can I use protected in some other package, + +150 +00:05:39,000 --> 00:05:42,000 +you can see marks is protected in the other package + +151 +00:05:42,000 --> 00:05:43,000 +like demo + +152 +00:05:43,000 --> 00:05:44,000 +again, marks is not working. + +153 +00:05:44,000 --> 00:05:48,000 +So basically if you make it protected, you can't use it + +154 +00:05:48,000 --> 00:05:51,000 +outside the package, before coming there, + +155 +00:05:51,000 --> 00:05:53,000 +in fact, you know what we should do, let's try + +156 +00:05:53,000 --> 00:05:55,000 +to create a class here itself. + +157 +00:05:55,000 --> 00:05:58,000 +Let's say a class called launch. + +158 +00:05:58,000 --> 00:05:59,000 +And in this class, + +159 +00:05:59,000 --> 00:06:03,000 +let me have a method which is public wide, A, B, C, + +160 +00:06:03,000 --> 00:06:06,000 +let's say, and let me try to create object of A + +161 +00:06:06,000 --> 00:06:08,000 +equal to new A + +162 +00:06:08,000 --> 00:06:11,000 +and see from here, can I try, if I try to use marks, + +163 +00:06:11,000 --> 00:06:15,000 +let me just print marks obj.marks, okay? + +164 +00:06:15,000 --> 00:06:18,000 +It is working, so it works in the same package, + +165 +00:06:18,000 --> 00:06:20,000 +but it doesn't work in other packages. + +166 +00:06:20,000 --> 00:06:23,000 +Okay, so you can see demo is another package, + +167 +00:06:23,000 --> 00:06:25,000 +but what about a subclass? + +168 +00:06:25,000 --> 00:06:26,000 +We have class C here. + +169 +00:06:26,000 --> 00:06:31,000 +And then if I try to create a method, public void, A, B, C. + +170 +00:06:31,000 --> 00:06:33,000 +And you can see this class C looks same + +171 +00:06:33,000 --> 00:06:34,000 +in both the packages. + +172 +00:06:34,000 --> 00:06:36,000 +The difference is this is in different package + +173 +00:06:36,000 --> 00:06:37,000 +and it is extending A now. + +174 +00:06:37,000 --> 00:06:42,000 +And now if I try to access marks, it works. + +175 +00:06:42,000 --> 00:06:45,000 +There's no issue, you cannot access marks + +176 +00:06:45,000 --> 00:06:46,000 +outside the package, + +177 +00:06:46,000 --> 00:06:49,000 +but you can access marks outside the package, + +178 +00:06:49,000 --> 00:06:51,000 +but it should be a subclass. + +179 +00:06:51,000 --> 00:06:54,000 +If you can see C is a plus of A. + +180 +00:06:54,000 --> 00:06:56,000 +Now if you see this image here, + +181 +00:06:56,000 --> 00:06:58,000 +these is how our image looks like, right? + +182 +00:06:58,000 --> 00:07:01,000 +Let's talk about private, private can be used + +183 +00:07:01,000 --> 00:07:03,000 +in the same class, nowhere else. + +184 +00:07:04,000 --> 00:07:08,000 +Protected can be used in the same class, + +185 +00:07:08,000 --> 00:07:11,000 +in the same package, in the other package, + +186 +00:07:11,000 --> 00:07:12,000 +but subclass. + +187 +00:07:12,000 --> 00:07:15,000 +Public can be used everywhere. + +188 +00:07:16,000 --> 00:07:19,000 +Default can be used in the same package, okay? + +189 +00:07:19,000 --> 00:07:21,000 +It's that simple. + +190 +00:07:21,000 --> 00:07:23,000 +So this is how you specify the modifiers. + +191 +00:07:23,000 --> 00:07:26,000 +Now how do we use and which one to use? + +192 +00:07:26,000 --> 00:07:29,000 +Basically the idea is very simple. + +193 +00:07:29,000 --> 00:07:31,000 +Try to make your classes public, which we are doing. + +194 +00:07:31,000 --> 00:07:33,000 +And of course we don't have a public here. + +195 +00:07:33,000 --> 00:07:35,000 +The thing is, you can't even make this class public + +196 +00:07:35,000 --> 00:07:37,000 +because you cannot have two public classes + +197 +00:07:37,000 --> 00:07:39,000 +in the same file. + +198 +00:07:39,000 --> 00:07:42,000 +The general idea is in one file you should + +199 +00:07:42,000 --> 00:07:44,000 +have only one class. + +200 +00:07:44,000 --> 00:07:46,000 +So try to make your class public, next, + +201 +00:07:46,000 --> 00:07:50,000 +try to keep your variables, the instance variables private. + +202 +00:07:50,000 --> 00:07:53,000 +The methods most of the time will be public. + +203 +00:07:53,000 --> 00:07:54,000 +But if you think there is a method + +204 +00:07:54,000 --> 00:07:58,000 +or a variable which should be accessed only in the subclass + +205 +00:07:58,000 --> 00:08:01,000 +of other packages, use protected, try to avoid default. + +206 +00:08:01,000 --> 00:08:03,000 +Default is not something we should be using. + +207 +00:08:03,000 --> 00:08:06,000 +I know from, if you check all this thing, + +208 +00:08:06,000 --> 00:08:08,000 +we are promoting default a lot. + +209 +00:08:08,000 --> 00:08:10,000 +But don't do that, don't use default. + +210 +00:08:10,000 --> 00:08:12,000 +So for example, this is default. + +211 +00:08:12,000 --> 00:08:15,000 +Either make it private, public, protected or public. + +212 +00:08:15,000 --> 00:08:16,000 +Don't keep it default. + +213 +00:08:16,000 --> 00:08:17,000 +Default simply means you don't know + +214 +00:08:17,000 --> 00:08:19,000 +what to give the access. + +215 +00:08:19,000 --> 00:08:21,000 +So keep it private, protected or public. + diff --git a/02 - Core Java/058 Source-code.url b/02 - Core Java/058 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/058 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/059 Polymorphism_en.srt b/02 - Core Java/059 Polymorphism_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..fc0a5046b651c24f4a89431fb9cde6227a939a60 --- /dev/null +++ b/02 - Core Java/059 Polymorphism_en.srt @@ -0,0 +1,400 @@ +1 +00:00:00,000 --> 00:00:03,000 +(light calming music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, let's talk about polymorphism. + +3 +00:00:06,000 --> 00:00:08,000 +Remember we have talked about method overriding, + +4 +00:00:08,000 --> 00:00:10,000 +and then we say there's something more? + +5 +00:00:10,000 --> 00:00:11,000 +This is where we are talking about. + +6 +00:00:11,000 --> 00:00:15,000 +So we have a concept of polymorphism. + +7 +00:00:15,000 --> 00:00:18,000 +Now this is a very important concept in terms of OOPs + +8 +00:00:18,000 --> 00:00:20,000 +and most of the languages which implements OOPs + +9 +00:00:20,000 --> 00:00:21,000 +has this concept. + +10 +00:00:21,000 --> 00:00:23,000 +Now, what is polymorphism? + +11 +00:00:23,000 --> 00:00:26,000 +Now basically as a concept, it has two words. + +12 +00:00:26,000 --> 00:00:28,000 +One is poly, second is morphism. + +13 +00:00:28,000 --> 00:00:33,000 +Now poly simply means many, and morphism means behavior. + +14 +00:00:34,000 --> 00:00:36,000 +Now what it means is there's something which behaves + +15 +00:00:36,000 --> 00:00:40,000 +differently in different situatio based on situation. + +16 +00:00:40,000 --> 00:00:41,000 +Let's say as a human, + +17 +00:00:41,000 --> 00:00:44,000 +you behave differently at different locations, right? + +18 +00:00:44,000 --> 00:00:48,000 +Example, for at home, I behave differently. + +19 +00:00:48,000 --> 00:00:49,000 +At my office, I behave differently. + +20 +00:00:49,000 --> 00:00:52,000 +When I'm with my friends, I behave differently, right? + +21 +00:00:52,000 --> 00:00:54,000 +So that situation changes me in the same way + +22 +00:00:54,000 --> 00:00:55,000 +because the name also changes right? + +23 +00:00:55,000 --> 00:01:00,000 +At home, I'm a son, husband, a father, + +24 +00:01:00,000 --> 00:01:04,000 +at office, I'm a colleague, I'm a boss, I'm an employee, + +25 +00:01:05,000 --> 00:01:06,000 +with my friends, okay? + +26 +00:01:06,000 --> 00:01:09,000 +They use different words, but then let's not mention that. + +27 +00:01:09,000 --> 00:01:11,000 +So the important thing is your behavior, + +28 +00:01:11,000 --> 00:01:12,000 +your name changes at every place, right? + +29 +00:01:12,000 --> 00:01:14,000 +Your behavior changes based on that. + +30 +00:01:14,000 --> 00:01:17,000 +In the same way in the world of object oriented, + +31 +00:01:17,000 --> 00:01:19,000 +we have a concept of many behavior, polymorphism. + +32 +00:01:19,000 --> 00:01:22,000 +So the same object or the same reference + +33 +00:01:22,000 --> 00:01:24,000 +will have different behavior + +34 +00:01:24,000 --> 00:01:26,000 +based on how you create the instance of it. + +35 +00:01:26,000 --> 00:01:28,000 +Now, to implement this, + +36 +00:01:28,000 --> 00:01:30,000 +there are multiple ways in different languages. + +37 +00:01:30,000 --> 00:01:32,000 +But before we go into the implementation, + +38 +00:01:32,000 --> 00:01:34,000 +let's talk about its type. + +39 +00:01:34,000 --> 00:01:36,000 +So there are two types of polymorphism. + +40 +00:01:36,000 --> 00:01:40,000 +One is called a compile time polymorphism, + +41 +00:01:40,000 --> 00:01:43,000 +and second is called a runtime polymorphism. + +42 +00:01:43,000 --> 00:01:44,000 +Of course, they have different names. + +43 +00:01:44,000 --> 00:01:47,000 +Compile time is also called early binding, + +44 +00:01:47,000 --> 00:01:49,000 +runtime polymorphism also called late binding. + +45 +00:01:49,000 --> 00:01:53,000 +But we have these two words, compile time and run time. + +46 +00:01:53,000 --> 00:01:54,000 +Now what do you mean by compile time? + +47 +00:01:54,000 --> 00:01:56,000 +See if you can define your behavior. + +48 +00:01:56,000 --> 00:01:59,000 +So the behavior is defined at compile time, + +49 +00:01:59,000 --> 00:02:01,000 +that is compile time polymorphism. + +50 +00:02:01,000 --> 00:02:03,000 +If the behavior will be defined at run time, + +51 +00:02:03,000 --> 00:02:06,000 +that is called a run time polymorphism. + +52 +00:02:06,000 --> 00:02:07,000 +So when you compile the file + +53 +00:02:07,000 --> 00:02:11,000 +and when you know what thing will get executed + +54 +00:02:11,000 --> 00:02:13,000 +at the compile time itself, it is compile time. + +55 +00:02:13,000 --> 00:02:15,000 +Now, which method will be executed, + +56 +00:02:15,000 --> 00:02:17,000 +will be decided at run time, that's run time. + +57 +00:02:17,000 --> 00:02:20,000 +Now we have done two examples here, + +58 +00:02:20,000 --> 00:02:23,000 +which is overloading and overriding. + +59 +00:02:23,000 --> 00:02:25,000 +So if you talk about overloading, + +60 +00:02:25,000 --> 00:02:26,000 +it's a part of compile time + +61 +00:02:26,000 --> 00:02:28,000 +because at the compile time itself, + +62 +00:02:28,000 --> 00:02:30,000 +example, let's say if you have two methods, + +63 +00:02:30,000 --> 00:02:32,000 +you have, let's say two methods, + +64 +00:02:32,000 --> 00:02:37,000 +which is add, which takes two parameters, int and int. + +65 +00:02:38,000 --> 00:02:40,000 +And then we have one more method here, which is add. + +66 +00:02:40,000 --> 00:02:43,000 +And when you say int, int, int, + +67 +00:02:43,000 --> 00:02:45,000 +which takes three parameters. + +68 +00:02:45,000 --> 00:02:48,000 +Now both this have the same name, but different parameters. + +69 +00:02:48,000 --> 00:02:50,000 +That is method overloading. + +70 +00:02:50,000 --> 00:02:51,000 +Now, which method to execute? + +71 +00:02:51,000 --> 00:02:53,000 +Will you execute the first or second method + +72 +00:02:53,000 --> 00:02:55,000 +will be decided at compile time. + +73 +00:02:55,000 --> 00:02:56,000 +Now, let's say you have two classes, + +74 +00:02:56,000 --> 00:02:59,000 +class A and class B to add two different classes. + +75 +00:02:59,000 --> 00:03:02,000 +And class B extends class A. + +76 +00:03:02,000 --> 00:03:05,000 +We have talked about this in method overriding. + +77 +00:03:05,000 --> 00:03:07,000 +And let's say you have a method which is add, + +78 +00:03:07,000 --> 00:03:08,000 +which takes two parameters. + +79 +00:03:08,000 --> 00:03:11,000 +And also here we also have the add method, + +80 +00:03:11,000 --> 00:03:13,000 +which takes two parameters. + +81 +00:03:13,000 --> 00:03:16,000 +Now, out of these two, which one will get executed, + +82 +00:03:16,000 --> 00:03:18,000 +it will be decided at run time. + +83 +00:03:18,000 --> 00:03:20,000 +So that is called run time polymorphism. + +84 +00:03:20,000 --> 00:03:21,000 +In fact, there's one more concept + +85 +00:03:21,000 --> 00:03:24,000 +which we're going to discuss in the implementation + +86 +00:03:24,000 --> 00:03:26,000 +when we talk about dynamic method dispatch, + +87 +00:03:26,000 --> 00:03:28,000 +at that point, this thing will get more clear. + +88 +00:03:28,000 --> 00:03:30,000 +But just to generalize, what is polymorphism? + +89 +00:03:30,000 --> 00:03:32,000 +Polymorphism means many behavior. + +90 +00:03:32,000 --> 00:03:35,000 +So your behavior of your reference + +91 +00:03:35,000 --> 00:03:38,000 +or your execution will change based on some situation. + +92 +00:03:38,000 --> 00:03:42,000 +If it is compile time, which is your method overloading, + +93 +00:03:42,000 --> 00:03:43,000 +where you decide + +94 +00:03:43,000 --> 00:03:45,000 +based on the parameters at compile time itself, + +95 +00:03:45,000 --> 00:03:48,000 +in runtime, you're not sure which method will be called + +96 +00:03:48,000 --> 00:03:50,000 +because both the methods have the same name, + +97 +00:03:50,000 --> 00:03:53,000 +same parameters, something which will decide that + +98 +00:03:53,000 --> 00:03:54,000 +we'll discuss in the next video. + +99 +00:03:54,000 --> 00:03:56,000 +But that is method overriding, + +100 +00:03:56,000 --> 00:03:58,000 +which is run time polymorphism. + diff --git a/02 - Core Java/059 Source-code.url b/02 - Core Java/059 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/059 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/060 Dynamic Method Dispatch_en.srt b/02 - Core Java/060 Dynamic Method Dispatch_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..e57654a45249c19b7109abc66b430ef388fc5b00 --- /dev/null +++ b/02 - Core Java/060 Dynamic Method Dispatch_en.srt @@ -0,0 +1,792 @@ +1 +00:00:00,000 --> 00:00:03,000 +(gentle piano music) + +2 +00:00:03,000 --> 00:00:04,000 +-: Now in this video, + +3 +00:00:04,000 --> 00:00:07,000 +let's talk about dynamic method dispatch. + +4 +00:00:07,000 --> 00:00:09,000 +Now, once we have went through what is polymorphism, + +5 +00:00:09,000 --> 00:00:11,000 +we have also mentioned that it is same thing, + +6 +00:00:11,000 --> 00:00:12,000 +multiple behavior, + +7 +00:00:12,000 --> 00:00:15,000 +and we have two types of polymorphism, + +8 +00:00:15,000 --> 00:00:18,000 +which is compile time polymorphism run time polymorphism. + +9 +00:00:18,000 --> 00:00:20,000 +Now to implement run time polymorphism, + +10 +00:00:20,000 --> 00:00:22,000 +let's try one more example here. + +11 +00:00:22,000 --> 00:00:26,000 +And we do that with the help of dynamic method dispatch. + +12 +00:00:26,000 --> 00:00:28,000 +Again, to keep it simple, we'll use a simple class + +13 +00:00:28,000 --> 00:00:31,000 +like class A and then we got class B. + +14 +00:00:31,000 --> 00:00:34,000 +And let's say class B extends class A. + +15 +00:00:34,000 --> 00:00:37,000 +Okay, and let's say we have class C. + +16 +00:00:37,000 --> 00:00:39,000 +And again, we have discussed most of the topics. + +17 +00:00:39,000 --> 00:00:43,000 +So let's make it quick. So we got class C. + +18 +00:00:43,000 --> 00:00:45,000 +So if you can see we have class A, + +19 +00:00:45,000 --> 00:00:48,000 +which is getting extended by B and C both. + +20 +00:00:48,000 --> 00:00:51,000 +So you can say class A is parent, + +21 +00:00:51,000 --> 00:00:54,000 +and we have two child there, one is B and one is C. + +22 +00:00:54,000 --> 00:00:56,000 +Now what we'll do is, in this A, + +23 +00:00:56,000 --> 00:00:59,000 +I will create public white show, + +24 +00:00:59,000 --> 00:01:02,000 +okay, and this show will print, + +25 +00:01:02,000 --> 00:01:04,000 +let's say in A show. + +26 +00:01:04,000 --> 00:01:07,000 +Okay, and we'll be using classy, but later. + +27 +00:01:07,000 --> 00:01:11,000 +As of now, let's only focus on class A and class B. + +28 +00:01:11,000 --> 00:01:14,000 +Now, my question to everyone is, I mean, okay, + +29 +00:01:14,000 --> 00:01:16,000 +let's remove this class A just to remove the confusion. + +30 +00:01:16,000 --> 00:01:17,000 +Let's go step by step. + +31 +00:01:17,000 --> 00:01:18,000 +So let's say we have these two classes, + +32 +00:01:18,000 --> 00:01:20,000 +class B extends class A. + +33 +00:01:20,000 --> 00:01:22,000 +Now my question to you is, + +34 +00:01:22,000 --> 00:01:24,000 +how do I create an object of class B? + +35 +00:01:24,000 --> 00:01:26,000 +And of course we have talked about this from a long time. + +36 +00:01:26,000 --> 00:01:29,000 +I can say we B obj equal to new B, + +37 +00:01:29,000 --> 00:01:31,000 +and that's how you get the object, right? + +38 +00:01:31,000 --> 00:01:32,000 +And of course, using this OBJ, + +39 +00:01:32,000 --> 00:01:35,000 +I can call show there's no harm. + +40 +00:01:35,000 --> 00:01:39,000 +Compile this code and run. You can see we got in A show. + +41 +00:01:39,000 --> 00:01:41,000 +Now this is how you create the object right? + +42 +00:01:41,000 --> 00:01:43,000 +Now there is one more thing. + +43 +00:01:43,000 --> 00:01:47,000 +Can I create a reference of A, an object of B? + +44 +00:01:47,000 --> 00:01:48,000 +Is it possible? + +45 +00:01:48,000 --> 00:01:49,000 +Since B + +46 +00:01:49,000 --> 00:01:50,000 +extends A. + +47 +00:01:50,000 --> 00:01:53,000 +So you can say B is a child of A. + +48 +00:01:53,000 --> 00:01:56,000 +So can I refer this OBJ as A? + +49 +00:01:56,000 --> 00:01:58,000 +And the object is B. + +50 +00:01:58,000 --> 00:02:00,000 +Now when you look at this OBJ, what is this OBJ? + +51 +00:02:00,000 --> 00:02:03,000 +It's a variable, right? And what is a type of a variable? + +52 +00:02:03,000 --> 00:02:06,000 +The type is A here, right? + +53 +00:02:06,000 --> 00:02:10,000 +So the OBJ type is A, but the implementation is B. + +54 +00:02:10,000 --> 00:02:12,000 +Okay, that's the point remember. + +55 +00:02:12,000 --> 00:02:13,000 +And this perfectly works. + +56 +00:02:13,000 --> 00:02:15,000 +Now if you want to imagine this, + +57 +00:02:15,000 --> 00:02:17,000 +let's go with this one example here, + +58 +00:02:17,000 --> 00:02:18,000 +which I will write here. + +59 +00:02:18,000 --> 00:02:20,000 +Let's say if I create a class called computer, + +60 +00:02:20,000 --> 00:02:23,000 +and of course class computer has some methods, okay? + +61 +00:02:23,000 --> 00:02:25,000 +And then class, laptop. + +62 +00:02:25,000 --> 00:02:28,000 +And we all know that laptop is a computer. + +63 +00:02:28,000 --> 00:02:31,000 +So I can also say laptop extends computer. + +64 +00:02:31,000 --> 00:02:33,000 +Yeah, we can say that, right? + +65 +00:02:33,000 --> 00:02:34,000 +And now + +66 +00:02:34,000 --> 00:02:36,000 +if I say laptop is a computer, + +67 +00:02:36,000 --> 00:02:39,000 +if I want to create object of a laptop, + +68 +00:02:39,000 --> 00:02:40,000 +of course I will do it. + +69 +00:02:40,000 --> 00:02:42,000 +It's very simple. You will say laptop. + +70 +00:02:42,000 --> 00:02:45,000 +Let's say lap is equal to new laptop. + +71 +00:02:45,000 --> 00:02:48,000 +So let's say, instead of saying a lap, you say OBJ 1, + +72 +00:02:48,000 --> 00:02:51,000 +and then I can just call all the methods. + +73 +00:02:51,000 --> 00:02:54,000 +But my question is can I refer this OBJ 1 as laptop? + +74 +00:02:54,000 --> 00:02:55,000 +Yes, it is a type of laptop, + +75 +00:02:55,000 --> 00:02:57,000 +and I can also say this is a computer. + +76 +00:02:57,000 --> 00:02:59,000 +And that's what you do, right? When you look at the laptop, + +77 +00:02:59,000 --> 00:03:01,000 +you can say, hey, that is a laptop, + +78 +00:03:01,000 --> 00:03:03,000 +but that's also a computer. + +79 +00:03:03,000 --> 00:03:05,000 +We are referring that as a computer. + +80 +00:03:05,000 --> 00:03:08,000 +So we are referring this OBJ, which is an object, + +81 +00:03:08,000 --> 00:03:12,000 +as a computer, but technically it's a laptop, right? + +82 +00:03:12,000 --> 00:03:16,000 +In the same way we can refer this OBJ of type A. + +83 +00:03:16,000 --> 00:03:20,000 +So we can mention the type as parent, + +84 +00:03:20,000 --> 00:03:22,000 +but the object can be of child, okay? + +85 +00:03:22,000 --> 00:03:24,000 +So I've just created this laptop computer + +86 +00:03:24,000 --> 00:03:26,000 +just to show you, let me remove that. + +87 +00:03:26,000 --> 00:03:30,000 +So yet it is possible to create a reference of a super class + +88 +00:03:30,000 --> 00:03:32,000 +and the object of a subclass. + +89 +00:03:32,000 --> 00:03:35,000 +Okay, and this will surely work. + +90 +00:03:35,000 --> 00:03:36,000 +Let's compile, there's no issue. + +91 +00:03:36,000 --> 00:03:39,000 +Let's run. And you can see it says in A show. + +92 +00:03:40,000 --> 00:03:43,000 +But the twist is, if I have the same method, + +93 +00:03:43,000 --> 00:03:46,000 +and we have talked about this in method overriding, + +94 +00:03:46,000 --> 00:03:48,000 +if I have the same method, + +95 +00:03:48,000 --> 00:03:50,000 +and if I say this is in B show, + +96 +00:03:50,000 --> 00:03:52,000 +now what it will call? + +97 +00:03:52,000 --> 00:03:54,000 +Will it call in A show or in B show? + +98 +00:03:54,000 --> 00:03:57,000 +As per the method overriding, it should call in B show. + +99 +00:03:57,000 --> 00:04:02,000 +But look at reference. Reference is of type A. + +100 +00:04:02,000 --> 00:04:04,000 +That's tricky, right? Let's try, + +101 +00:04:04,000 --> 00:04:05,000 +in fact, what I will do now, + +102 +00:04:05,000 --> 00:04:07,000 +just to first let's run this call + +103 +00:04:07,000 --> 00:04:09,000 +and I will show you something. + +104 +00:04:09,000 --> 00:04:11,000 +You can see it says in B show. + +105 +00:04:11,000 --> 00:04:14,000 +Let me initially set the object of A itself. + +106 +00:04:14,000 --> 00:04:16,000 +And then let's go step by step. + +107 +00:04:16,000 --> 00:04:18,000 +So I will say compile and run. + +108 +00:04:18,000 --> 00:04:19,000 +It says in A show, + +109 +00:04:19,000 --> 00:04:22,000 +in fact I can call this OBJ show once more, + +110 +00:04:22,000 --> 00:04:23,000 +but before calling OBJ show, + +111 +00:04:23,000 --> 00:04:26,000 +what I will do is I will make one change here. + +112 +00:04:27,000 --> 00:04:28,000 +I can just come back here and I can say + +113 +00:04:28,000 --> 00:04:30,000 +OBJ is equal to new B. That's right. + +114 +00:04:30,000 --> 00:04:33,000 +We can assign a new object to the old variable. + +115 +00:04:33,000 --> 00:04:36,000 +Okay? Now if you want to understand this with a memory, + +116 +00:04:36,000 --> 00:04:39,000 +of course we have a stack here, right? + +117 +00:04:39,000 --> 00:04:40,000 +In this stack we have different variables. + +118 +00:04:40,000 --> 00:04:43,000 +And then we have a heap memory here. + +119 +00:04:43,000 --> 00:04:46,000 +Now initially when you say OBJ, that's your object, right? + +120 +00:04:46,000 --> 00:04:48,000 +And it will create object of A. + +121 +00:04:48,000 --> 00:04:50,000 +So 1 0 1 is object of A, + +122 +00:04:50,000 --> 00:04:52,000 +I'm talking about line number 24. + +123 +00:04:52,000 --> 00:04:55,000 +Okay, line number 24, we have this line. + +124 +00:04:55,000 --> 00:04:58,000 +So that's an object here and it will have some methods. + +125 +00:04:58,000 --> 00:05:00,000 +The method name is show itself, right? + +126 +00:05:00,000 --> 00:05:02,000 +And this will have a value which is 1 0 1. + +127 +00:05:02,000 --> 00:05:05,000 +So I'm talking about line number 24, okay? + +128 +00:05:05,000 --> 00:05:07,000 +On 24, this is a link. + +129 +00:05:07,000 --> 00:05:10,000 +Now when you say obj.show, line number 25 obj.show. + +130 +00:05:10,000 --> 00:05:12,000 +At that point it will look. + +131 +00:05:12,000 --> 00:05:15,000 +Okay, so obj.show, that means I have to call the show method + +132 +00:05:15,000 --> 00:05:16,000 +of object A. + +133 +00:05:16,000 --> 00:05:18,000 +So this my friend is an object of A. + +134 +00:05:19,000 --> 00:05:22,000 +And now on line number 28, we made some changes. + +135 +00:05:22,000 --> 00:05:25,000 +That is now this particular object + +136 +00:05:25,000 --> 00:05:26,000 +is referring to the object of B. + +137 +00:05:26,000 --> 00:05:30,000 +So we got one more object here, which is let's say 1 0 3, + +138 +00:05:31,000 --> 00:05:33,000 +and this is an object of B now. + +139 +00:05:33,000 --> 00:05:35,000 +Okay, this will also have a show method + +140 +00:05:35,000 --> 00:05:37,000 +because that is what we mentioned there. + +141 +00:05:37,000 --> 00:05:41,000 +And now since we are changing the value here, + +142 +00:05:41,000 --> 00:05:43,000 +this will be replaced by 1 0 3. + +143 +00:05:43,000 --> 00:05:45,000 +There's no 1 0 1 anymore. + +144 +00:05:45,000 --> 00:05:49,000 +That means this link is broken and we got a new link. + +145 +00:05:49,000 --> 00:05:52,000 +Now on line number 29, when you say obj.show, + +146 +00:05:52,000 --> 00:05:55,000 +it will call the show method of B. + +147 +00:05:55,000 --> 00:05:59,000 +So it's not about what is a type of the object or reference. + +148 +00:05:59,000 --> 00:06:03,000 +You can say type is still A, but the object is B. + +149 +00:06:03,000 --> 00:06:05,000 +And that's what you call the B show. + +150 +00:06:05,000 --> 00:06:08,000 +Let me compile and run. + +151 +00:06:08,000 --> 00:06:11,000 +You can see at first for this line, it is printing in A show + +152 +00:06:11,000 --> 00:06:14,000 +for this line is printing in B show. + +153 +00:06:14,000 --> 00:06:17,000 +Likewise, if I create another class here, class C, + +154 +00:06:17,000 --> 00:06:19,000 +which extends A, + +155 +00:06:19,000 --> 00:06:20,000 +and if I have the same method, + +156 +00:06:20,000 --> 00:06:23,000 +I will just reuse my own code. + +157 +00:06:23,000 --> 00:06:26,000 +And if I say this is in C show, + +158 +00:06:26,000 --> 00:06:29,000 +what will happen if I create, if I just go back here + +159 +00:06:29,000 --> 00:06:32,000 +and say OBJ equal to new C. + +160 +00:06:32,000 --> 00:06:34,000 +And if I say obj.show, what do you think? + +161 +00:06:34,000 --> 00:06:36,000 +What will happen if I run the for the third time? + +162 +00:06:36,000 --> 00:06:38,000 +And that of course right, + +163 +00:06:38,000 --> 00:06:40,000 +different object will have a different behavior. + +164 +00:06:40,000 --> 00:06:42,000 +Remember run time polymorphism + +165 +00:06:42,000 --> 00:06:44,000 +and you can see we got in C show. + +166 +00:06:44,000 --> 00:06:46,000 +So the same object, which is obj.show + +167 +00:06:46,000 --> 00:06:49,000 +is behaving differently with different objects. + +168 +00:06:49,000 --> 00:06:50,000 +Polymorphism, right? + +169 +00:06:50,000 --> 00:06:53,000 +So if you say obj.show, we are not even sure. + +170 +00:06:53,000 --> 00:06:56,000 +So when you compile this code, at that point we are not sure + +171 +00:06:56,000 --> 00:06:59,000 +obj.show will call which method of which class, + +172 +00:06:59,000 --> 00:07:02,000 +I mean method of which class, it'll be decided at run time. + +173 +00:07:02,000 --> 00:07:05,000 +And that's what is called run time of polymorphism. + +174 +00:07:05,000 --> 00:07:10,000 +And all this concept is called dynamic method dispatch, + +175 +00:07:10,000 --> 00:07:11,000 +because that's dynamic. + +176 +00:07:11,000 --> 00:07:13,000 +Which method will it will call? We are not sure. + +177 +00:07:13,000 --> 00:07:15,000 +Okay, and that's how it works. + +178 +00:07:15,000 --> 00:07:17,000 +So point to remember here is, + +179 +00:07:17,000 --> 00:07:20,000 +in respect you of what type of object you have + +180 +00:07:20,000 --> 00:07:24,000 +or what type of variable you create. It's of type A, + +181 +00:07:24,000 --> 00:07:27,000 +but it all depends upon what object you have. + +182 +00:07:27,000 --> 00:07:29,000 +Right, it is only possible when you have inheritance. Okay? + +183 +00:07:29,000 --> 00:07:33,000 +You can't simply say we are getting a new class, class D, + +184 +00:07:33,000 --> 00:07:37,000 +and you will say, okay, not demo, class D. + +185 +00:07:37,000 --> 00:07:39,000 +And you can't simply go back here and say + +186 +00:07:39,000 --> 00:07:43,000 +OBJ equal to new D, this will not work + +187 +00:07:43,000 --> 00:07:45,000 +because D is not extending A. + +188 +00:07:45,000 --> 00:07:47,000 +If you can see, where if you can verify + +189 +00:07:47,000 --> 00:07:49,000 +D is not extending A. + +190 +00:07:49,000 --> 00:07:50,000 +So this will not work. + +191 +00:07:50,000 --> 00:07:53,000 +This will only work when you have inheritance. + +192 +00:07:53,000 --> 00:07:56,000 +So only you can have a child objects + +193 +00:07:56,000 --> 00:07:58,000 +for the parent + +194 +00:07:58,000 --> 00:07:59,000 +variable, + +195 +00:07:59,000 --> 00:08:01,000 +which is A OBJ, parent type variable. + +196 +00:08:01,000 --> 00:08:03,000 +So yeah, that's it from this video where we talked about + +197 +00:08:03,000 --> 00:08:04,000 +dynamic method dispatch, + +198 +00:08:04,000 --> 00:08:06,000 +which is other run time polymorphism. + diff --git a/02 - Core Java/060 Source-code.url b/02 - Core Java/060 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/060 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/061 Final keyword_en.srt b/02 - Core Java/061 Final keyword_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..c9a40ab89927985d47b3a1d1d7bfdbba3ab0fbce --- /dev/null +++ b/02 - Core Java/061 Final keyword_en.srt @@ -0,0 +1,616 @@ +1 +00:00:00,000 --> 00:00:03,000 +(peaceful piano tones ring) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, let's talk about final keyword. + +3 +00:00:06,000 --> 00:00:09,000 +Okay, so the keyword we are talking about now is final. + +4 +00:00:09,000 --> 00:00:13,000 +Now basically, final keyword can be used with a variable, + +5 +00:00:13,000 --> 00:00:16,000 +a final keyword can be used with a method, + +6 +00:00:16,000 --> 00:00:18,000 +and a final keyword can be used with a class + +7 +00:00:18,000 --> 00:00:20,000 +and we'll go step by step. + +8 +00:00:20,000 --> 00:00:23,000 +So let's start with the final with variable. + +9 +00:00:23,000 --> 00:00:25,000 +Now, when you make a variable, + +10 +00:00:25,000 --> 00:00:27,000 +let's say if I create a variable called num, + +11 +00:00:27,000 --> 00:00:30,000 +and if I say the value for num is eight. + +12 +00:00:30,000 --> 00:00:32,000 +And of course when you make a variable, + +13 +00:00:32,000 --> 00:00:34,000 +of course you can change the valuator, right? + +14 +00:00:34,000 --> 00:00:35,000 +So initially the value was eight + +15 +00:00:35,000 --> 00:00:37,000 +and now you're making it nine. + +16 +00:00:37,000 --> 00:00:38,000 +There's no problem, right? + +17 +00:00:38,000 --> 00:00:40,000 +And of course you can print the value as well. + +18 +00:00:40,000 --> 00:00:44,000 +So if I compile this code and execute, + +19 +00:00:44,000 --> 00:00:45,000 +the recent value is nine. + +20 +00:00:45,000 --> 00:00:47,000 +Now there's a concept of making constants, right? + +21 +00:00:47,000 --> 00:00:48,000 +So what is constant? + +22 +00:00:48,000 --> 00:00:50,000 +So if you make your variable constant, + +23 +00:00:50,000 --> 00:00:53,000 +you can't change the value of it. + +24 +00:00:53,000 --> 00:00:56,000 +Now the way you can do that is by using a final keyword. + +25 +00:00:56,000 --> 00:00:59,000 +Now, in different languages, we use different keywords. + +26 +00:00:59,000 --> 00:01:03,000 +I guess in C++, we use const. In Java we say final. + +27 +00:01:03,000 --> 00:01:07,000 +So the moment you make your variable as final variable, + +28 +00:01:07,000 --> 00:01:09,000 +the variable becomes constant. + +29 +00:01:09,000 --> 00:01:13,000 +So once you assign the value, you can't change it. + +30 +00:01:13,000 --> 00:01:14,000 +Okay, that's important. + +31 +00:01:14,000 --> 00:01:16,000 +And that's why you can see we've got an error here. + +32 +00:01:16,000 --> 00:01:18,000 +In fact, you can also get this error by compiling the code. + +33 +00:01:18,000 --> 00:01:21,000 +You can see it says, "Cannot assign a value + +34 +00:01:21,000 --> 00:01:23,000 +to a final variable." + +35 +00:01:23,000 --> 00:01:27,000 +So that's final, yeah, "final variable num." + +36 +00:01:27,000 --> 00:01:30,000 +So because num is final, you can't change the value. + +37 +00:01:30,000 --> 00:01:32,000 +And this is actually very important. + +38 +00:01:32,000 --> 00:01:33,000 +So whenever you have a value + +39 +00:01:33,000 --> 00:01:36,000 +which you think will not change, try to make it final. + +40 +00:01:36,000 --> 00:01:38,000 +It's a good practice to make final variables + +41 +00:01:38,000 --> 00:01:41,000 +is because you will not get any unexpected output. + +42 +00:01:41,000 --> 00:01:44,000 +Because if you think the value should be constant, + +43 +00:01:44,000 --> 00:01:45,000 +then why do you even change it? + +44 +00:01:45,000 --> 00:01:47,000 +Example, if you want to make a variable for pi. + +45 +00:01:47,000 --> 00:01:50,000 +We know the value of pi, right, which will not change. + +46 +00:01:50,000 --> 00:01:53,000 +So it's good to make that particular variable as final. + +47 +00:01:53,000 --> 00:01:55,000 +So it's always a good idea to make a final variable + +48 +00:01:55,000 --> 00:01:59,000 +because that's what a good practice is. + +49 +00:01:59,000 --> 00:02:01,000 +So we have talked about what is final variable. + +50 +00:02:01,000 --> 00:02:04,000 +So final variable simply means making constants. + +51 +00:02:04,000 --> 00:02:06,000 +Now let's talk about final method. + +52 +00:02:06,000 --> 00:02:08,000 +In fact, even before final method, + +53 +00:02:08,000 --> 00:02:09,000 +let's talk about final class. + +54 +00:02:09,000 --> 00:02:11,000 +Then final method will make much more sense. + +55 +00:02:11,000 --> 00:02:12,000 +So at this point, I will just remove this part. + +56 +00:02:12,000 --> 00:02:15,000 +So now we know what is final variable is, right? + +57 +00:02:15,000 --> 00:02:18,000 +So what I'll do here is I have a class here called, + +58 +00:02:18,000 --> 00:02:19,000 +let's say, calculator. + +59 +00:02:21,000 --> 00:02:23,000 +Now I'm making this class and in this, + +60 +00:02:23,000 --> 00:02:25,000 +or maybe let's keep it simple, let's say A. + +61 +00:02:25,000 --> 00:02:28,000 +So I've got this class A, right, and now, + +62 +00:02:28,000 --> 00:02:30,000 +in this class, I have some methods. + +63 +00:02:30,000 --> 00:02:33,000 +Let's say the method name is public void show. + +64 +00:02:33,000 --> 00:02:35,000 +And you know what, let's make it calculated, + +65 +00:02:35,000 --> 00:02:38,000 +just to make it much more sense. + +66 +00:02:38,000 --> 00:02:41,000 +And I will also say public void add. + +67 +00:02:41,000 --> 00:02:42,000 +This time I will not return the value, + +68 +00:02:42,000 --> 00:02:44,000 +I will just bring the output here itself. + +69 +00:02:44,000 --> 00:02:48,000 +I will say A into B. + +70 +00:02:49,000 --> 00:02:51,000 +And then I will just simply print A plus B. + +71 +00:02:51,000 --> 00:02:54,000 +I'm just adding it here itself, right? + +72 +00:02:54,000 --> 00:02:56,000 +And then we got the method, which is show, + +73 +00:02:56,000 --> 00:02:58,000 +and here we are saying public, + +74 +00:02:58,000 --> 00:03:02,000 +here we are saying "in calc show". + +75 +00:03:02,000 --> 00:03:05,000 +Okay, so let's say we have this class here, which is calc, + +76 +00:03:05,000 --> 00:03:07,000 +and then we have these two methods. + +77 +00:03:07,000 --> 00:03:08,000 +And if you want to make it work, + +78 +00:03:08,000 --> 00:03:10,000 +of course you have to get object of calc. + +79 +00:03:10,000 --> 00:03:13,000 +You will say Calc obj = new calc. + +80 +00:03:13,000 --> 00:03:15,000 +And with this object, + +81 +00:03:15,000 --> 00:03:16,000 +you can basically call both the methods. + +82 +00:03:16,000 --> 00:03:19,000 +You can say show, you can say add, + +83 +00:03:19,000 --> 00:03:20,000 +and you can pass two values. + +84 +00:03:20,000 --> 00:03:22,000 +4, let's say, 5. + +85 +00:03:22,000 --> 00:03:25,000 +Right, now with this, let me just compile this code + +86 +00:03:25,000 --> 00:03:28,000 +and run compile, no problem, and run. + +87 +00:03:28,000 --> 00:03:29,000 +You can see there's no issue. + +88 +00:03:29,000 --> 00:03:32,000 +It is printing in calc show and nine. + +89 +00:03:32,000 --> 00:03:35,000 +Now what if someone else is trying + +90 +00:03:35,000 --> 00:03:38,000 +to create a class called advanced calculator? + +91 +00:03:38,000 --> 00:03:43,000 +And maybe you, maybe they're trying to extend, + +92 +00:03:43,000 --> 00:03:44,000 +they're trying to inherit the features + +93 +00:03:44,000 --> 00:03:47,000 +from your class, which is calc. + +94 +00:03:47,000 --> 00:03:48,000 +And by the way, it's possible. + +95 +00:03:48,000 --> 00:03:50,000 +Anyone can inherit your method + +96 +00:03:50,000 --> 00:03:53,000 +or your classes, right, because it's available. + +97 +00:03:53,000 --> 00:03:56,000 +What I will do is I want to stop this. + +98 +00:03:56,000 --> 00:03:58,000 +I want to say, "Hey, you know, no one should be able + +99 +00:03:58,000 --> 00:04:00,000 +to extend my class." + +100 +00:04:00,000 --> 00:04:03,000 +You want to stop the inheritance. How will you do that? + +101 +00:04:03,000 --> 00:04:05,000 +So if I make this class as final, + +102 +00:04:05,000 --> 00:04:07,000 +you can see it will give you an error. + +103 +00:04:07,000 --> 00:04:11,000 +It says, "The type advanced calculator cannot be a subclass + +104 +00:04:11,000 --> 00:04:12,000 +of the final class." + +105 +00:04:12,000 --> 00:04:13,000 +That's right. + +106 +00:04:13,000 --> 00:04:16,000 +So once you make your class final, + +107 +00:04:16,000 --> 00:04:19,000 +you are basically stopping the inheritance. + +108 +00:04:19,000 --> 00:04:21,000 +So basically, when we talk about final variable, + +109 +00:04:21,000 --> 00:04:24,000 +it means you cannot change the value of it. + +110 +00:04:24,000 --> 00:04:28,000 +When you make a class final, it means no one can extend it. + +111 +00:04:28,000 --> 00:04:31,000 +You are stopping the inheritance. Okay, that makes sense. + +112 +00:04:31,000 --> 00:04:33,000 +Now what if your class is not final? + +113 +00:04:33,000 --> 00:04:35,000 +So of course anyone can use it + +114 +00:04:35,000 --> 00:04:36,000 +and maybe you don't have an issue + +115 +00:04:36,000 --> 00:04:39,000 +of people using your class for inheritance. + +116 +00:04:39,000 --> 00:04:41,000 +The problem which we have is + +117 +00:04:41,000 --> 00:04:45,000 +someone will create object of not your class, + +118 +00:04:45,000 --> 00:04:49,000 +but advanced calculator, and they will use your features. + +119 +00:04:49,000 --> 00:04:51,000 +And you are okay with it because, ultimately, + +120 +00:04:51,000 --> 00:04:53,000 +you are printing your own class name, + +121 +00:04:53,000 --> 00:04:55,000 +which is in Calc Show. + +122 +00:04:55,000 --> 00:04:58,000 +Or maybe I can say "by Navin". + +123 +00:04:58,000 --> 00:05:00,000 +Okay, so maybe this calculator is by Navin. + +124 +00:05:00,000 --> 00:05:03,000 +So if I compile this code and run, it'll print my name. + +125 +00:05:03,000 --> 00:05:05,000 +At least I'm getting the credit. + +126 +00:05:05,000 --> 00:05:07,000 +But what if someone says, "Hey, you know, + +127 +00:05:07,000 --> 00:05:09,000 +I will use your feature of addition + +128 +00:05:09,000 --> 00:05:11,000 +because there's no create mention, + +129 +00:05:11,000 --> 00:05:14,000 +but I will have my own create here. + +130 +00:05:14,000 --> 00:05:16,000 +So I will create my own show method, + +131 +00:05:16,000 --> 00:05:18,000 +method overriding, remember, + +132 +00:05:18,000 --> 00:05:22,000 +and I can simply print by John." + +133 +00:05:22,000 --> 00:05:23,000 +I'm not sure who John is, + +134 +00:05:23,000 --> 00:05:26,000 +but let's say if that person says "by John". + +135 +00:05:26,000 --> 00:05:27,000 +Now what will happen is, + +136 +00:05:27,000 --> 00:05:30,000 +when you execute the show method and add, + +137 +00:05:30,000 --> 00:05:32,000 +you can see add is my implementation + +138 +00:05:32,000 --> 00:05:36,000 +but they are just overriding show method + +139 +00:05:36,000 --> 00:05:38,000 +and it says "by John". + +140 +00:05:38,000 --> 00:05:39,000 +I don't want this. + +141 +00:05:39,000 --> 00:05:43,000 +I don't want anyone to override my method. + +142 +00:05:43,000 --> 00:05:47,000 +How do I do that? How do I stop method overriding? + +143 +00:05:47,000 --> 00:05:50,000 +In that case, you can make your method as final. + +144 +00:05:50,000 --> 00:05:52,000 +Now, when you make your method final, + +145 +00:05:52,000 --> 00:05:55,000 +which we talked about here, when you make your method final, + +146 +00:05:55,000 --> 00:05:57,000 +no one can override it. + +147 +00:05:57,000 --> 00:05:58,000 +You can see we've got an error, + +148 +00:05:58,000 --> 00:06:02,000 +cannot override the final method. + +149 +00:06:02,000 --> 00:06:04,000 +So that's how we can use final keyword. + +150 +00:06:04,000 --> 00:06:07,000 +We can use final keyword with variable to make it constant, + +151 +00:06:07,000 --> 00:06:09,000 +you can use final keyword with the class + +152 +00:06:09,000 --> 00:06:12,000 +to stop inheritance, and you can use final keyword + +153 +00:06:12,000 --> 00:06:15,000 +with method to stop method overriding. + +154 +00:06:15,000 --> 00:06:16,000 +So I hope final makes sense now. + diff --git a/02 - Core Java/061 Source-code.url b/02 - Core Java/061 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/061 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/062 Object Class equals toString hashcode_en.srt b/02 - Core Java/062 Object Class equals toString hashcode_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..3472408ec0af74da9832e581a3f31339ca9233a1 --- /dev/null +++ b/02 - Core Java/062 Object Class equals toString hashcode_en.srt @@ -0,0 +1,1292 @@ +1 +00:00:00,000 --> 00:00:03,000 +(dramatic piano music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, let's talk about object class. + +3 +00:00:06,000 --> 00:00:07,000 +Remember when we talked about inheritance, + +4 +00:00:07,000 --> 00:00:10,000 +I also mentioned that every class in Java + +5 +00:00:10,000 --> 00:00:12,000 +extends the object class. + +6 +00:00:12,000 --> 00:00:16,000 +So in this video, let's try to understand, what is that? + +7 +00:00:16,000 --> 00:00:17,000 +So what I will do here is, first of all, + +8 +00:00:17,000 --> 00:00:19,000 +I will create a simple class, let's say class A. + +9 +00:00:19,000 --> 00:00:23,000 +So every class in Java extends the object class, right? + +10 +00:00:23,000 --> 00:00:24,000 +So even if you don't mention it is there. + +11 +00:00:24,000 --> 00:00:26,000 +But what is there inside this class? + +12 +00:00:26,000 --> 00:00:28,000 +So what I will do is, first of all I will say, + +13 +00:00:28,000 --> 00:00:30,000 +let me get object of A here. + +14 +00:00:30,000 --> 00:00:34,000 +I will say A and okay, so we got the object. + +15 +00:00:34,000 --> 00:00:36,000 +But then using this object, + +16 +00:00:36,000 --> 00:00:38,000 +you can see we can call so many methods. + +17 +00:00:38,000 --> 00:00:40,000 +We have not defined any method in this class, + +18 +00:00:40,000 --> 00:00:42,000 +but still we have the access of equals method. + +19 +00:00:42,000 --> 00:00:47,000 +Then we got notify, hashCode, notifyAll, toString, wait. + +20 +00:00:48,000 --> 00:00:49,000 +So many methods, right? + +21 +00:00:49,000 --> 00:00:52,000 +Now what they do, so let's understand that. + +22 +00:00:52,000 --> 00:00:55,000 +So what I do is, in fact instead of this, + +23 +00:00:55,000 --> 00:00:58,000 +I will say Laptop class just for the example. + +24 +00:00:58,000 --> 00:01:00,000 +And here also let me get an object of Laptop here. + +25 +00:01:00,000 --> 00:01:05,000 +So I will say Laptop obj = new Laptop. + +26 +00:01:05,000 --> 00:01:09,000 +Okay, now I will try to create some variables here. + +27 +00:01:09,000 --> 00:01:11,000 +So I will say private int. + +28 +00:01:11,000 --> 00:01:14,000 +Let's say price;. + +29 +00:01:14,000 --> 00:01:15,000 +And let me also get a string variable here, + +30 +00:01:15,000 --> 00:01:18,000 +which is private String brand. + +31 +00:01:18,000 --> 00:01:20,000 +Okay, so we got these two laptops. + +32 +00:01:21,000 --> 00:01:23,000 +I can say model as well. + +33 +00:01:23,000 --> 00:01:24,000 +So we got model and price, right? + +34 +00:01:24,000 --> 00:01:28,000 +So if you talk about MacBook or Dell XPS or Lenovo laptops + +35 +00:01:28,000 --> 00:01:29,000 +or it doesn't matter which laptop it is, + +36 +00:01:29,000 --> 00:01:31,000 +we have a model and we have a price here. + +37 +00:01:31,000 --> 00:01:34,000 +Now if you talk about this particular laptop here, + +38 +00:01:34,000 --> 00:01:35,000 +let me set the values to it. + +39 +00:01:35,000 --> 00:01:37,000 +And timing, you know what I will do, + +40 +00:01:37,000 --> 00:01:38,000 +I will just remove this private + +41 +00:01:38,000 --> 00:01:41,000 +so that I can access it from outside here. + +42 +00:01:41,000 --> 00:01:42,000 +And I can say, + +43 +00:01:43,000 --> 00:01:45,000 +so I will say obj.model. + +44 +00:01:45,000 --> 00:01:49,000 +I will set the model as let's say, Lenovo, + +45 +00:01:49,000 --> 00:01:50,000 +I don't know, is it a Laptop Yoga? + +46 +00:01:50,000 --> 00:01:51,000 +Maybe. + +47 +00:01:51,000 --> 00:01:56,000 +And then I will say obj.price is equal to let's $1000. + +48 +00:01:57,000 --> 00:01:58,000 +Okay, so I got a laptop. + +49 +00:01:58,000 --> 00:02:00,000 +I've got the values, right? + +50 +00:02:00,000 --> 00:02:02,000 +What if I try to print the object? + +51 +00:02:02,000 --> 00:02:03,000 +What happens behind the scene + +52 +00:02:03,000 --> 00:02:05,000 +when I try to print the object? + +53 +00:02:05,000 --> 00:02:06,000 +Of course we tried this before + +54 +00:02:06,000 --> 00:02:09,000 +and it was giving some weird output. + +55 +00:02:09,000 --> 00:02:11,000 +Let's understand what that output is. + +56 +00:02:11,000 --> 00:02:13,000 +Now if you try to concentrate on this output, + +57 +00:02:13,000 --> 00:02:16,000 +of course, this is not the values which we set, + +58 +00:02:16,000 --> 00:02:18,000 +but it prints laptop and it actually makes sense + +59 +00:02:18,000 --> 00:02:21,000 +because obj is a laptop object. + +60 +00:02:21,000 --> 00:02:24,000 +And then we got the at rate symbol + +61 +00:02:24,000 --> 00:02:26,000 +and then we got some weird numbers. + +62 +00:02:26,000 --> 00:02:29,000 +I'm assuming they are hexidecimal values + +63 +00:02:29,000 --> 00:02:32,000 +because you can see we got a, d, f. + +64 +00:02:32,000 --> 00:02:34,000 +They are part of hexidecimal numbers. + +65 +00:02:34,000 --> 00:02:35,000 +Okay, so these are hexidecimal format. + +66 +00:02:35,000 --> 00:02:37,000 +But what is it? + +67 +00:02:37,000 --> 00:02:38,000 +Let's try to understand that. + +68 +00:02:38,000 --> 00:02:39,000 +Now what if I say, + +69 +00:02:39,000 --> 00:02:42,000 +every time you try to print the object + +70 +00:02:42,000 --> 00:02:44,000 +it by defaults behind the scene, + +71 +00:02:44,000 --> 00:02:47,000 +it will call the toString method. + +72 +00:02:47,000 --> 00:02:48,000 +Let me repeat. + +73 +00:02:48,000 --> 00:02:50,000 +Every time you try to print the object, + +74 +00:02:50,000 --> 00:02:52,000 +it will call toString method + +75 +00:02:52,000 --> 00:02:54,000 +even if you don't mention, yeah. + +76 +00:02:54,000 --> 00:02:56,000 +And okay, by doing those changes, + +77 +00:02:56,000 --> 00:02:58,000 +let me just run this code once again. + +78 +00:02:58,000 --> 00:03:00,000 +Compile and run. + +79 +00:03:00,000 --> 00:03:01,000 +You can see this, it prints the same thing. + +80 +00:03:01,000 --> 00:03:04,000 +That means yes it is getting called + +81 +00:03:04,000 --> 00:03:05,000 +but what toString is doing? + +82 +00:03:05,000 --> 00:03:08,000 +So if I go to toString, now first of all, + +83 +00:03:08,000 --> 00:03:10,000 +toString is not my method. + +84 +00:03:10,000 --> 00:03:11,000 +If you can see in laptop class + +85 +00:03:11,000 --> 00:03:14,000 +we don't have any method called toString. + +86 +00:03:14,000 --> 00:03:16,000 +But in the object class there is. + +87 +00:03:16,000 --> 00:03:18,000 +You can see we got a toString method + +88 +00:03:18,000 --> 00:03:20,000 +which returns a string, right? + +89 +00:03:20,000 --> 00:03:21,000 +That's what we are printing, right? + +90 +00:03:21,000 --> 00:03:23,000 +It returns something. + +91 +00:03:23,000 --> 00:03:24,000 +What it returns? + +92 +00:03:24,000 --> 00:03:26,000 +It returns the class name. + +93 +00:03:26,000 --> 00:03:26,000 +If you can see it says getClass. + +94 +00:03:26,000 --> 00:03:30,000 +getClass method will give you the class name. + +95 +00:03:30,000 --> 00:03:31,000 +Then it calls getName. + +96 +00:03:31,000 --> 00:03:35,000 +So class name, okay, that is laptop. + +97 +00:03:35,000 --> 00:03:36,000 +Okay is working. + +98 +00:03:36,000 --> 00:03:39,000 +And then we got at that symbol in between. + +99 +00:03:39,000 --> 00:03:41,000 +Okay, that also makes sense why we got added it there + +100 +00:03:41,000 --> 00:03:44,000 +and then it print something in hexa string. + +101 +00:03:44,000 --> 00:03:47,000 +That's why we got hexa symbol values here. + +102 +00:03:47,000 --> 00:03:48,000 +But what exactly represents? + +103 +00:03:48,000 --> 00:03:51,000 +It converts the number into hexadecimal string + +104 +00:03:51,000 --> 00:03:54,000 +and that number is actually hashCode. + +105 +00:03:54,000 --> 00:03:56,000 +Now what is hashCode? + +106 +00:03:56,000 --> 00:03:58,000 +If you go to hashCode, again it belongs to the same class + +107 +00:03:58,000 --> 00:04:00,000 +and behind the scene implementation + +108 +00:04:00,000 --> 00:04:02,000 +of a hashCode works with the, + +109 +00:04:02,000 --> 00:04:03,000 +it basically generates the hash + +110 +00:04:03,000 --> 00:04:06,000 +based on the values you have, okay. + +111 +00:04:06,000 --> 00:04:08,000 +Now hash is a very simple concept + +112 +00:04:08,000 --> 00:04:11,000 +in terms of cryptography anywhere. + +113 +00:04:11,000 --> 00:04:14,000 +It tries to create a single string + +114 +00:04:14,000 --> 00:04:16,000 +of all the data which you have. + +115 +00:04:16,000 --> 00:04:19,000 +So let's say if you have a object which has five variables, + +116 +00:04:19,000 --> 00:04:21,000 +it will apply a hash algorithm + +117 +00:04:21,000 --> 00:04:25,000 +and it will generate a string, a number string, + +118 +00:04:25,000 --> 00:04:28,000 +which will be a fixed size, okay. + +119 +00:04:28,000 --> 00:04:31,000 +To know more about hashCode, + +120 +00:04:31,000 --> 00:04:31,000 +you can just go to YouTube + +121 +00:04:31,000 --> 00:04:33,000 +and search for what is hash + +122 +00:04:33,000 --> 00:04:35,000 +or go to Google and search for what is hash. + +123 +00:04:35,000 --> 00:04:38,000 +But at this point we are returning a hashCode, okay? + +124 +00:04:38,000 --> 00:04:40,000 +That's what we got here. + +125 +00:04:40,000 --> 00:04:44,000 +Okay, now the thing is we are trying to print this, right? + +126 +00:04:44,000 --> 00:04:44,000 +So by the default is calling toString + +127 +00:04:44,000 --> 00:04:46,000 +and that's why we got this output. + +128 +00:04:46,000 --> 00:04:49,000 +But what if when I try to print the object, + +129 +00:04:49,000 --> 00:04:51,000 +it will print something which I want. + +130 +00:04:51,000 --> 00:04:53,000 +And one thing we know + +131 +00:04:53,000 --> 00:04:55,000 +that if you don't have a method in your class, + +132 +00:04:55,000 --> 00:04:56,000 +it will call the method of a super class. + +133 +00:04:56,000 --> 00:05:00,000 +But what if I create that method itself? + +134 +00:05:00,000 --> 00:05:03,000 +So the public method which returns a string, + +135 +00:05:03,000 --> 00:05:04,000 +the method name is toString. + +136 +00:05:04,000 --> 00:05:07,000 +If I implement this method + +137 +00:05:07,000 --> 00:05:11,000 +and if I return something, if I return say "Hey", that's it. + +138 +00:05:11,000 --> 00:05:15,000 +And if I clear this and compile + +139 +00:05:15,000 --> 00:05:18,000 +and run, oh it says Hey, you know. + +140 +00:05:18,000 --> 00:05:21,000 +It's not printing the laptop class name + +141 +00:05:21,000 --> 00:05:23,000 +and hexidecimal values. + +142 +00:05:23,000 --> 00:05:26,000 +It's trying to call toString, which is this. + +143 +00:05:26,000 --> 00:05:28,000 +it is written, "Hey". + +144 +00:05:28,000 --> 00:05:30,000 +It's not going for the super class here. + +145 +00:05:30,000 --> 00:05:31,000 +That is awesome. + +146 +00:05:31,000 --> 00:05:35,000 +Now let me also try by deleting this thing, okay. + +147 +00:05:35,000 --> 00:05:37,000 +Let's not call toString by ourself. + +148 +00:05:37,000 --> 00:05:39,000 +Let's see if it is getting called automatically. + +149 +00:05:39,000 --> 00:05:40,000 +It is happening, right? + +150 +00:05:40,000 --> 00:05:42,000 +Now, whatever you wanna return, you can do that here, right? + +151 +00:05:42,000 --> 00:05:44,000 +What I want to return is I want to return + +152 +00:05:44,000 --> 00:05:48,000 +the model information with colon in between + +153 +00:05:48,000 --> 00:05:50,000 +and the price, so that it will print + +154 +00:05:50,000 --> 00:05:51,000 +that particular data. + +155 +00:05:51,000 --> 00:05:52,000 +Compile and run. + +156 +00:05:52,000 --> 00:05:56,000 +You can see it says Lenovo Yoga : $1000. + +157 +00:05:56,000 --> 00:05:57,000 +Okay, so that's how we can basically + +158 +00:05:57,000 --> 00:05:59,000 +work with the object class methods. + +159 +00:05:59,000 --> 00:06:02,000 +If you don't mention it will be coming automatically + +160 +00:06:02,000 --> 00:06:04,000 +by the object class. + +161 +00:06:04,000 --> 00:06:05,000 +But there's one more thing. + +162 +00:06:05,000 --> 00:06:07,000 +Let's say if I create one more object of laptop, + +163 +00:06:07,000 --> 00:06:09,000 +and this time let's say the values are same. + +164 +00:06:09,000 --> 00:06:12,000 +Let's say this is object one, object two. + +165 +00:06:12,000 --> 00:06:15,000 +This is the object one, one. + +166 +00:06:15,000 --> 00:06:17,000 +Okay, don't give me suggestions. + +167 +00:06:17,000 --> 00:06:19,000 +Yeah, this is two and two. + +168 +00:06:19,000 --> 00:06:22,000 +Now if you compare these two objects, are they same? + +169 +00:06:22,000 --> 00:06:24,000 +Yes the values are same, + +170 +00:06:24,000 --> 00:06:26,000 +but do we have the objects which are same? + +171 +00:06:26,000 --> 00:06:27,000 +Let's try. + +172 +00:06:27,000 --> 00:06:28,000 +How will you compare this? + +173 +00:06:28,000 --> 00:06:30,000 +So what I can do is I can just create + +174 +00:06:30,000 --> 00:06:35,000 +a boolean variable here and I can say result is equal to, + +175 +00:06:36,000 --> 00:06:38,000 +how do I compare obj1.obj2 + +176 +00:06:38,000 --> 00:06:41,000 +or obj1 == obj2. + +177 +00:06:42,000 --> 00:06:43,000 +Let's try. + +178 +00:06:43,000 --> 00:06:44,000 +I mean we can use double equal to + +179 +00:06:44,000 --> 00:06:46,000 +between obj1 and obj2 as well. + +180 +00:06:46,000 --> 00:06:48,000 +Let's try to print the result + +181 +00:06:48,000 --> 00:06:49,000 +and let's see what happens. + +182 +00:06:49,000 --> 00:06:53,000 +If I compile this code and run, oh it says false. + +183 +00:06:53,000 --> 00:06:56,000 +Okay, maybe using double equal to is not a good idea. + +184 +00:06:56,000 --> 00:06:57,000 +Let's use some inbuilt methods. + +185 +00:06:57,000 --> 00:07:02,000 +If you can see obj1 actually has a method called equals. + +186 +00:07:02,000 --> 00:07:04,000 +It checks for the equals. + +187 +00:07:04,000 --> 00:07:05,000 +Okay, that's great. + +188 +00:07:05,000 --> 00:07:08,000 +It checks for the equals of obj1 and obj2. + +189 +00:07:08,000 --> 00:07:10,000 +Okay, let me try this. + +190 +00:07:10,000 --> 00:07:12,000 +I hope this will work. + +191 +00:07:12,000 --> 00:07:13,000 +Compile and run. + +192 +00:07:13,000 --> 00:07:16,000 +Oh, it is still giving you false. + +193 +00:07:16,000 --> 00:07:17,000 +Why? + +194 +00:07:17,000 --> 00:07:18,000 +The objects are same, right? + +195 +00:07:18,000 --> 00:07:21,000 +The thing is, if you go with the equals method here + +196 +00:07:21,000 --> 00:07:24,000 +you can see in our class we don't have equals + +197 +00:07:24,000 --> 00:07:27,000 +and the equals is actually coming from the object class. + +198 +00:07:27,000 --> 00:07:28,000 +It basically compares the two object + +199 +00:07:28,000 --> 00:07:31,000 +by their hexadecimal number. + +200 +00:07:31,000 --> 00:07:32,000 +We don't do that. + +201 +00:07:32,000 --> 00:07:35,000 +So we want to compare them based on their values. + +202 +00:07:35,000 --> 00:07:37,000 +So one thing we can do is we can implement + +203 +00:07:37,000 --> 00:07:39,000 +the equals method by ourself. + +204 +00:07:39,000 --> 00:07:42,000 +So we want the equals method which will + +205 +00:07:42,000 --> 00:07:43,000 +return a boolean value. + +206 +00:07:43,000 --> 00:07:45,000 +The method name is equals + +207 +00:07:45,000 --> 00:07:47,000 +and it will accept only one object. + +208 +00:07:47,000 --> 00:07:50,000 +I will say this as an object of the same type, laptop, + +209 +00:07:50,000 --> 00:07:52,000 +because we are comparing laptop with laptop. + +210 +00:07:52,000 --> 00:07:53,000 +I will say this is other. + +211 +00:07:53,000 --> 00:07:56,000 +And then the object which is calling this equals + +212 +00:07:56,000 --> 00:07:57,000 +is this object. + +213 +00:07:57,000 --> 00:07:59,000 +And we have passed one more object here which is other. + +214 +00:07:59,000 --> 00:08:02,000 +Or maybe I can say this is this and that. + +215 +00:08:02,000 --> 00:08:03,000 +So this is that object, this object. + +216 +00:08:03,000 --> 00:08:05,000 +So we are comparing two objects. + +217 +00:08:05,000 --> 00:08:07,000 +Now how do I compare? + +218 +00:08:07,000 --> 00:08:12,000 +I can check if this .model.equal... + +219 +00:08:12,000 --> 00:08:13,000 +So when you talk about this model, + +220 +00:08:13,000 --> 00:08:14,000 +it's a string, right? + +221 +00:08:14,000 --> 00:08:16,000 +Now string has equals implemented properly. + +222 +00:08:16,000 --> 00:08:19,000 +So we can use string equals to compare this string. + +223 +00:08:19,000 --> 00:08:20,000 +And this dot, + +224 +00:08:20,000 --> 00:08:23,000 +we are comparing that.model, right? + +225 +00:08:23,000 --> 00:08:24,000 +We are comparing two strings here. + +226 +00:08:24,000 --> 00:08:27,000 +And we have to also compare the price. + +227 +00:08:27,000 --> 00:08:31,000 +this.price == that.price. + +228 +00:08:31,000 --> 00:08:32,000 +Now why I'm using equal to for integer + +229 +00:08:32,000 --> 00:08:34,000 +because that's how it works, right? + +230 +00:08:34,000 --> 00:08:37,000 +And we have to also say open and close. + +231 +00:08:37,000 --> 00:08:41,000 +If this matches, then I will return true, + +232 +00:08:43,000 --> 00:08:47,000 +else, I will return false. + +233 +00:08:48,000 --> 00:08:49,000 +Simple, right? + +234 +00:08:49,000 --> 00:08:50,000 +And since we have only one statement, + +235 +00:08:50,000 --> 00:08:55,000 +I can actually do this, right, in one line. + +236 +00:08:56,000 --> 00:08:58,000 +You can see I'm just comparing the values here, + +237 +00:08:58,000 --> 00:09:01,000 +I'm comparing these two strings, which is model, model, + +238 +00:09:01,000 --> 00:09:03,000 +and price and price. + +239 +00:09:03,000 --> 00:09:05,000 +It will not use the equals of object class. + +240 +00:09:05,000 --> 00:09:07,000 +It will use the equals of my own class. + +241 +00:09:07,000 --> 00:09:08,000 +Let's see if that works. + +242 +00:09:08,000 --> 00:09:10,000 +Compile and run. + +243 +00:09:10,000 --> 00:09:11,000 +Oh it works. + +244 +00:09:11,000 --> 00:09:12,000 +Can you say that we got true? + +245 +00:09:12,000 --> 00:09:15,000 +Now if I change the value, let's say it becomes Yoga1 + +246 +00:09:16,000 --> 00:09:19,000 +and if I try to compile and run + +247 +00:09:19,000 --> 00:09:20,000 +you can see it says false + +248 +00:09:20,000 --> 00:09:22,000 +because we have a different value. + +249 +00:09:22,000 --> 00:09:23,000 +Cool. + +250 +00:09:23,000 --> 00:09:24,000 +Okay, in fact, you know, + +251 +00:09:24,000 --> 00:09:25,000 +I can actually simplify this more. + +252 +00:09:25,000 --> 00:09:27,000 +Why to even mention if, else? + +253 +00:09:27,000 --> 00:09:30,000 +Anyway this is returning true and false, right? + +254 +00:09:30,000 --> 00:09:33,000 +What I can actually do is the entire stuff from here, + +255 +00:09:33,000 --> 00:09:35,000 +you can cut this part + +256 +00:09:35,000 --> 00:09:38,000 +and so return the entire string + +257 +00:09:38,000 --> 00:09:41,000 +because the string itself will compare, right? + +258 +00:09:41,000 --> 00:09:43,000 +It will return true or false. + +259 +00:09:43,000 --> 00:09:45,000 +I can do that in one line. + +260 +00:09:46,000 --> 00:09:47,000 +Yeah. + +261 +00:09:47,000 --> 00:09:48,000 +There's one way. + +262 +00:09:48,000 --> 00:09:49,000 +But there are some rules in Java + +263 +00:09:49,000 --> 00:09:51,000 +if you are trying to do equals. + +264 +00:09:51,000 --> 00:09:54,000 +When you say two objects are equal, + +265 +00:09:54,000 --> 00:09:56,000 +of course they should have a same value + +266 +00:09:56,000 --> 00:09:58,000 +and they should also have a same hashCode. + +267 +00:09:58,000 --> 00:09:59,000 +So what I mean by that is, + +268 +00:09:59,000 --> 00:10:01,000 +don't define the equals + +269 +00:10:01,000 --> 00:10:04,000 +and equals method by yourself. + +270 +00:10:04,000 --> 00:10:07,000 +You can use your ID and you can say source action + +271 +00:10:07,000 --> 00:10:09,000 +and you can see it gives you an option + +272 +00:10:09,000 --> 00:10:11,000 +of generate hashCode and equals. + +273 +00:10:11,000 --> 00:10:12,000 +Click on that. + +274 +00:10:12,000 --> 00:10:14,000 +Select all the variables which you want to compare + +275 +00:10:14,000 --> 00:10:16,000 +and click on okay. + +276 +00:10:16,000 --> 00:10:18,000 +It will generate the equals method for you. + +277 +00:10:18,000 --> 00:10:21,000 +And you can see it does multiple checks. + +278 +00:10:21,000 --> 00:10:22,000 +We have only checks for the value. + +279 +00:10:22,000 --> 00:10:27,000 +But what if you want to check if the object is not null? + +280 +00:10:27,000 --> 00:10:29,000 +Maybe you want to check for the class name as well. + +281 +00:10:29,000 --> 00:10:31,000 +Maybe you want to check if the model + +282 +00:10:31,000 --> 00:10:32,000 +is not null before comparing. + +283 +00:10:32,000 --> 00:10:35,000 +So it does multiple checks for you. + +284 +00:10:35,000 --> 00:10:36,000 +It's a good practice. + +285 +00:10:36,000 --> 00:10:38,000 +And that's why I say sometime you have + +286 +00:10:38,000 --> 00:10:40,000 +to prefer your own IDE output. + +287 +00:10:40,000 --> 00:10:42,000 +And it also implements hashCode. + +288 +00:10:42,000 --> 00:10:44,000 +You know, this is how the hash code is generated. + +289 +00:10:44,000 --> 00:10:46,000 +Basically it takes your data + +290 +00:10:46,000 --> 00:10:49,000 +and works and applies some algorithm on your data + +291 +00:10:49,000 --> 00:10:51,000 +to generate the result. + +292 +00:10:51,000 --> 00:10:52,000 +If this algorithm can change, + +293 +00:10:52,000 --> 00:10:54,000 +you can implement your own algorithm. + +294 +00:10:54,000 --> 00:10:56,000 +But of course it should have all the variables + +295 +00:10:56,000 --> 00:10:58,000 +which should comes into play. + +296 +00:10:58,000 --> 00:11:00,000 +Example let's say, I have one more string here. + +297 +00:11:00,000 --> 00:11:02,000 +which is serial number. + +298 +00:11:02,000 --> 00:11:06,000 +And I'm okay if two objects have the same model number, + +299 +00:11:06,000 --> 00:11:08,000 +same model, same price, but different serial number. + +300 +00:11:08,000 --> 00:11:09,000 +That's okay with me. + +301 +00:11:09,000 --> 00:11:11,000 +So maybe you can just skip this serial + +302 +00:11:11,000 --> 00:11:13,000 +in your equals and hashCode. + +303 +00:11:13,000 --> 00:11:15,000 +Okay, that's the important thing. + +304 +00:11:15,000 --> 00:11:16,000 +The thing you have to remember here is + +305 +00:11:16,000 --> 00:11:19,000 +the object class has multiple methods + +306 +00:11:19,000 --> 00:11:22,000 +and we have worked with toString, which is here. + +307 +00:11:22,000 --> 00:11:23,000 +In fact, you know, + +308 +00:11:23,000 --> 00:11:25,000 +we don't even have to mention toString here. + +309 +00:11:25,000 --> 00:11:28,000 +We can right click and we say source action + +310 +00:11:28,000 --> 00:11:31,000 +generate toString with both the variables, okay. + +311 +00:11:31,000 --> 00:11:34,000 +It will generate a toString for you, + +312 +00:11:34,000 --> 00:11:38,000 +which gives you a much better output than what you can type. + +313 +00:11:38,000 --> 00:11:42,000 +Now I know by asking the code from your IDE, + +314 +00:11:42,000 --> 00:11:43,000 +you get these two extra annotations here, + +315 +00:11:43,000 --> 00:11:45,000 +in fact three. + +316 +00:11:45,000 --> 00:11:46,000 +This is actually, + +317 +00:11:46,000 --> 00:11:48,000 +we'll discuss that once we talk about annotations. + +318 +00:11:48,000 --> 00:11:49,000 +At this point it is not needed. + +319 +00:11:49,000 --> 00:11:50,000 +It's not compulsion, + +320 +00:11:50,000 --> 00:11:53,000 +but it has some importance which we'll discuss later. + +321 +00:11:53,000 --> 00:11:55,000 +Yeah, that's how we can use object class. + +322 +00:11:55,000 --> 00:11:59,000 +We can work with hashCode equals toString. + +323 +00:11:59,000 --> 00:12:01,000 +And yeah, that's it from this video. + diff --git a/02 - Core Java/062 Source-code.url b/02 - Core Java/062 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/062 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/063 Source-code.url b/02 - Core Java/063 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/063 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/063 Upcasting and Downcasting_en.srt b/02 - Core Java/063 Upcasting and Downcasting_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..8c09c2e3c4c791d7487654d30be7c84e7ba6a0e6 --- /dev/null +++ b/02 - Core Java/063 Upcasting and Downcasting_en.srt @@ -0,0 +1,664 @@ +1 +00:00:03,000 --> 00:00:06,000 +-: In this video we'll talk about what is downcasting + +2 +00:00:06,000 --> 00:00:08,000 +and upcasting. + +3 +00:00:08,000 --> 00:00:10,000 +Now before this we have to understand what is typecasting. + +4 +00:00:10,000 --> 00:00:13,000 +So let's say we have a double value. + +5 +00:00:13,000 --> 00:00:15,000 +So let's say double D is equal to let's say 4.5. + +6 +00:00:16,000 --> 00:00:19,000 +And then of course I can print this value. + +7 +00:00:19,000 --> 00:00:21,000 +I can say D, and we all know what will be the output, + +8 +00:00:21,000 --> 00:00:23,000 +it will find 4.5. + +9 +00:00:23,000 --> 00:00:27,000 +But what if I want to save this value somewhere? + +10 +00:00:27,000 --> 00:00:30,000 +Like this example, if I say int is I equal to D? + +11 +00:00:30,000 --> 00:00:31,000 +Can we do that? + +12 +00:00:31,000 --> 00:00:33,000 +Can we have the double value inside integer? + +13 +00:00:33,000 --> 00:00:34,000 +Of course it will not support, right? + +14 +00:00:34,000 --> 00:00:38,000 +Because 4.5, we cannot be stored in integer. + +15 +00:00:38,000 --> 00:00:40,000 +So yes, we can at least store four here, right? + +16 +00:00:40,000 --> 00:00:44,000 +So maybe we can remove .5, we can at least store four. + +17 +00:00:44,000 --> 00:00:46,000 +But how will we do that? + +18 +00:00:46,000 --> 00:00:47,000 +By default, Java will not allow you that + +19 +00:00:47,000 --> 00:00:51,000 +because Java says, "Type mismatch cannot convert + +20 +00:00:51,000 --> 00:00:52,000 +from double to int." + +21 +00:00:52,000 --> 00:00:55,000 +It's because it says, "You know, you might lose data." + +22 +00:00:55,000 --> 00:00:58,000 +And we can say, "Hey, that's okay for me, + +23 +00:00:58,000 --> 00:00:59,000 +I'm okay to lose data." + +24 +00:00:59,000 --> 00:01:01,000 +So what you can do is you can typecast it. + +25 +00:01:01,000 --> 00:01:04,000 +So you can say, "Hey, I don't care what value you send, + +26 +00:01:04,000 --> 00:01:07,000 +just send that in the integer format. I'm okay with it." + +27 +00:01:07,000 --> 00:01:09,000 +Okay, so this is typecasting. + +28 +00:01:09,000 --> 00:01:12,000 +You are converting the type of a value, right? + +29 +00:01:12,000 --> 00:01:14,000 +Now by doing this, if I try to print I, + +30 +00:01:14,000 --> 00:01:16,000 +let's see what happens. + +31 +00:01:16,000 --> 00:01:18,000 +Of course we are throwing that integer, we will lose data. + +32 +00:01:18,000 --> 00:01:20,000 +So we are losing .5, but that's okay. + +33 +00:01:20,000 --> 00:01:22,000 +We at least we got four. + +34 +00:01:22,000 --> 00:01:25,000 +Now this is called typecasting. + +35 +00:01:25,000 --> 00:01:28,000 +Now coming back to the world of OOPs, + +36 +00:01:28,000 --> 00:01:29,000 +can we do this in OOPs? + +37 +00:01:29,000 --> 00:01:32,000 +So let's try, and we have talked about in items. + +38 +00:01:32,000 --> 00:01:35,000 +We have talked about how can we have methods + +39 +00:01:35,000 --> 00:01:36,000 +in two different classes. + +40 +00:01:36,000 --> 00:01:37,000 +How can we override those stuff? + +41 +00:01:37,000 --> 00:01:39,000 +Now, just to keep it simple, + +42 +00:01:39,000 --> 00:01:42,000 +what I will do is I will create a class here. + +43 +00:01:42,000 --> 00:01:45,000 +I will name this class as, let's say A. + +44 +00:01:45,000 --> 00:01:47,000 +I am just trying to keep things as simple as possible. + +45 +00:01:47,000 --> 00:01:49,000 +We could have gone with other names as well, + +46 +00:01:49,000 --> 00:01:51,000 +like can't quit or something. + +47 +00:01:51,000 --> 00:01:52,000 +But just to keep it simple, + +48 +00:01:52,000 --> 00:01:53,000 +let's say we have these two classes, + +49 +00:01:53,000 --> 00:01:55,000 +class A and class B. + +50 +00:01:55,000 --> 00:01:58,000 +And class B extends A. + +51 +00:01:58,000 --> 00:02:01,000 +So we are basically trying to achieve inheritance here. + +52 +00:02:01,000 --> 00:02:03,000 +And now with this, what we can do + +53 +00:02:03,000 --> 00:02:05,000 +is we can create object of A. + +54 +00:02:05,000 --> 00:02:06,000 +Of course we can do that, right? + +55 +00:02:06,000 --> 00:02:09,000 +We can create object of A, and that perfectly works. + +56 +00:02:09,000 --> 00:02:12,000 +Now, in this two classes, I want to have some methods. + +57 +00:02:12,000 --> 00:02:15,000 +Let's say in A, I have a method which is show, + +58 +00:02:15,000 --> 00:02:17,000 +or maybe I will say show1, + +59 +00:02:18,000 --> 00:02:20,000 +and in B, I can have a method + +60 +00:02:20,000 --> 00:02:23,000 +which is public void show2, right? + +61 +00:02:23,000 --> 00:02:25,000 +And then I will just print something here, + +62 +00:02:25,000 --> 00:02:29,000 +so I can print in a show. + +63 +00:02:29,000 --> 00:02:31,000 +And now you know what I would say here. + +64 +00:02:31,000 --> 00:02:33,000 +See, we can write any complex logic for it, right? + +65 +00:02:33,000 --> 00:02:35,000 +But the idea here is to understand the concepts. + +66 +00:02:35,000 --> 00:02:38,000 +That's what you're focusing, so in B show. + +67 +00:02:38,000 --> 00:02:40,000 +Now what I wanna do is using this A object, + +68 +00:02:40,000 --> 00:02:44,000 +so if you can see we are creating this obj of type A, + +69 +00:02:44,000 --> 00:02:47,000 +and also the object is also of A, right? + +70 +00:02:47,000 --> 00:02:50,000 +And now with the help of this obj, + +71 +00:02:50,000 --> 00:02:51,000 +and you can see I'm not trying + +72 +00:02:51,000 --> 00:02:53,000 +to achieve method over writing here. + +73 +00:02:53,000 --> 00:02:54,000 +They are two different methods, + +74 +00:02:54,000 --> 00:02:56,000 +Show1, show2, two different methods, + +75 +00:02:56,000 --> 00:02:58,000 +and I can call show1. + +76 +00:02:58,000 --> 00:02:59,000 +And you can see I can only call show1. + +77 +00:02:59,000 --> 00:03:00,000 +I can't call show2. + +78 +00:03:00,000 --> 00:03:02,000 +What's the reason I can't call show2? + +79 +00:03:02,000 --> 00:03:03,000 +It's very simple. + +80 +00:03:03,000 --> 00:03:06,000 +We don't have show2 inside A, + +81 +00:03:06,000 --> 00:03:09,000 +and A don't even know B exists. + +82 +00:03:09,000 --> 00:03:09,000 +Okay, that's a fact. + +83 +00:03:09,000 --> 00:03:14,000 +B knows what A is, but A has no idea what B is. + +84 +00:03:14,000 --> 00:03:16,000 +I hope that is not the scene in real world, + +85 +00:03:16,000 --> 00:03:17,000 +your parents knows you. + +86 +00:03:17,000 --> 00:03:21,000 +So let's try this. + +87 +00:03:21,000 --> 00:03:22,000 +Compile and run. + +88 +00:03:22,000 --> 00:03:24,000 +You can see it says, "in A Show." + +89 +00:03:24,000 --> 00:03:27,000 +But now my question to you is, + +90 +00:03:27,000 --> 00:03:31,000 +can I have the reference of A and object of B? + +91 +00:03:31,000 --> 00:03:33,000 +And then we have seen that, it is possible. + +92 +00:03:33,000 --> 00:03:34,000 +I can actually create this. + +93 +00:03:34,000 --> 00:03:37,000 +I can have a reference of A and object of B. + +94 +00:03:37,000 --> 00:03:39,000 +We have done that in dynamic method dispatch. + +95 +00:03:39,000 --> 00:03:41,000 +We can basically do this, right? + +96 +00:03:41,000 --> 00:03:43,000 +Now what you're trying to do here is, + +97 +00:03:43,000 --> 00:03:46,000 +we have an object of class, subclass, + +98 +00:03:46,000 --> 00:03:49,000 +and then we are trying to assign that to a parent class. + +99 +00:03:49,000 --> 00:03:51,000 +Now we can actually say, "Hey, you know + +100 +00:03:51,000 --> 00:03:53,000 +what you can do? Please typecast it." + +101 +00:03:53,000 --> 00:03:55,000 +So whatever object you're sending is of child class, + +102 +00:03:55,000 --> 00:03:59,000 +but typecast it with A, which is a parent class. + +103 +00:03:59,000 --> 00:04:01,000 +The same thing we have done for the integer double, right? + +104 +00:04:01,000 --> 00:04:03,000 +Now, when you are doing this, + +105 +00:04:03,000 --> 00:04:05,000 +we are basically going up, right? + +106 +00:04:05,000 --> 00:04:08,000 +Which is if you try to compare this A and B, + +107 +00:04:08,000 --> 00:04:11,000 +A is a super class, B is a subclass. + +108 +00:04:11,000 --> 00:04:14,000 +So we are trying to say, refer, I mean object is of B, + +109 +00:04:14,000 --> 00:04:16,000 +but refer to A. + +110 +00:04:16,000 --> 00:04:18,000 +So we are going up, which is upcasting. + +111 +00:04:18,000 --> 00:04:21,000 +So this concept here is called upcasting, + +112 +00:04:21,000 --> 00:04:23,000 +and of course this will work. + +113 +00:04:23,000 --> 00:04:27,000 +There is no issue, you can still call in a show, + +114 +00:04:27,000 --> 00:04:30,000 +because in B as well, we got the same method. + +115 +00:04:30,000 --> 00:04:33,000 +Even if you have the object of B, it will still work. + +116 +00:04:33,000 --> 00:04:35,000 +Okay, so this is called upcasting. + +117 +00:04:35,000 --> 00:04:37,000 +So, even this is not compulsory, + +118 +00:04:37,000 --> 00:04:41,000 +it is something which happens implicitly behind the scene. + +119 +00:04:41,000 --> 00:04:44,000 +So you don't have even have to mention the casting part. + +120 +00:04:44,000 --> 00:04:45,000 +It normally works. + +121 +00:04:45,000 --> 00:04:47,000 +So this is upcasting. + +122 +00:04:47,000 --> 00:04:48,000 +Now what is downcasting? + +123 +00:04:49,000 --> 00:04:52,000 +So let's say I'm creating a reference of B. + +124 +00:04:52,000 --> 00:04:55,000 +Now why we have to go for downcasting first of all? + +125 +00:04:55,000 --> 00:04:58,000 +See, using this, even if the object is of B, + +126 +00:04:58,000 --> 00:05:01,000 +using this obj, I can't call show2. + +127 +00:05:01,000 --> 00:05:03,000 +If you can see the object is of B, + +128 +00:05:03,000 --> 00:05:05,000 +and the method show2 belongs to B, + +129 +00:05:05,000 --> 00:05:07,000 +but still you can't call it. + +130 +00:05:07,000 --> 00:05:09,000 +Reason is because the reference is of A, + +131 +00:05:09,000 --> 00:05:13,000 +and A has no idea what is this show2, okay? + +132 +00:05:13,000 --> 00:05:14,000 +So to solve that problem, + +133 +00:05:14,000 --> 00:05:16,000 +what we can do is we can create a new reference, + +134 +00:05:16,000 --> 00:05:19,000 +let's say obj1, but then I want to use the same object. + +135 +00:05:19,000 --> 00:05:20,000 +I don't want to get a new object. + +136 +00:05:20,000 --> 00:05:23,000 +Of course you can do this, but you're getting a new object. + +137 +00:05:23,000 --> 00:05:24,000 +Can we do this? + +138 +00:05:24,000 --> 00:05:27,000 +Can we say obj1 is equal to obj? + +139 +00:05:27,000 --> 00:05:31,000 +Now the problem is even obj is referring to B object, + +140 +00:05:31,000 --> 00:05:33,000 +but the problem is, it is referred to A. + +141 +00:05:33,000 --> 00:05:35,000 +It's the type of obj is A. + +142 +00:05:35,000 --> 00:05:39,000 +So it's the same problem of double and int, right? Similar. + +143 +00:05:39,000 --> 00:05:42,000 +So what we can do is, we can do a typecasting here. + +144 +00:05:42,000 --> 00:05:46,000 +We can say, "Hey, I know this is reference of A, + +145 +00:05:46,000 --> 00:05:47,000 +but the object is of B, right?" + +146 +00:05:47,000 --> 00:05:51,000 +So whenever you assign, I want the type to be of B, + +147 +00:05:51,000 --> 00:05:52,000 +and that's how it works. + +148 +00:05:52,000 --> 00:05:54,000 +This is called downcasting. + +149 +00:05:54,000 --> 00:05:58,000 +So this obj is the parent object or parent reference. + +150 +00:05:58,000 --> 00:06:01,000 +You are downcasting it to the child reference. + +151 +00:06:01,000 --> 00:06:03,000 +Now using this new obj1, I can call show2. + +152 +00:06:05,000 --> 00:06:08,000 +Okay, so this is downcasting. + +153 +00:06:08,000 --> 00:06:09,000 +This is downcasting. + +154 +00:06:09,000 --> 00:06:11,000 +This is upcasting. + +155 +00:06:11,000 --> 00:06:13,000 +In general, they're called typecasting, okay? + +156 +00:06:15,000 --> 00:06:16,000 +So you don't have to actually remember + +157 +00:06:16,000 --> 00:06:17,000 +these terms every time + +158 +00:06:17,000 --> 00:06:19,000 +upcasting, downcasting. + +159 +00:06:19,000 --> 00:06:22,000 +After some time you actually, you get used to this concept. + +160 +00:06:22,000 --> 00:06:26,000 +You will implicitly use those things anywhere you want. + +161 +00:06:26,000 --> 00:06:28,000 +But yes, while teaching, we have to use some words. + +162 +00:06:28,000 --> 00:06:30,000 +So upcasting, downcasting. + +163 +00:06:30,000 --> 00:06:32,000 +So yeah, that's about this concept. + +164 +00:06:32,000 --> 00:06:34,000 +But remember this, we can create a parent reference + +165 +00:06:34,000 --> 00:06:37,000 +and child object and we can also come back + +166 +00:06:37,000 --> 00:06:39,000 +to the child reference here. + diff --git a/02 - Core Java/064 Source-Code.url b/02 - Core Java/064 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/02 - Core Java/064 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/02 - Core Java/064 Wrapper Class_en.srt b/02 - Core Java/064 Wrapper Class_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..f4319b346b843f082f533348062f3eca2bbc0242 --- /dev/null +++ b/02 - Core Java/064 Wrapper Class_en.srt @@ -0,0 +1,884 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: Let's talk about wrapper classes. + +2 +00:00:02,000 --> 00:00:04,000 +Now we have talked about primitive variables, right? + +3 +00:00:04,000 --> 00:00:05,000 +Example, if you want to create, + +4 +00:00:05,000 --> 00:00:07,000 +if you want to store a number like five, + +5 +00:00:07,000 --> 00:00:09,000 +of course you will store that in an INT variable + +6 +00:00:09,000 --> 00:00:10,000 +or maybe short, maybe byte, + +7 +00:00:10,000 --> 00:00:11,000 +that's your choice. + +8 +00:00:11,000 --> 00:00:15,000 +If you want to work with a value which is point values, + +9 +00:00:15,000 --> 00:00:17,000 +let's say 6.5 or something like that, + +10 +00:00:17,000 --> 00:00:19,000 +you will be using double or float. + +11 +00:00:19,000 --> 00:00:21,000 +And then if you want to store a character, + +12 +00:00:21,000 --> 00:00:23,000 +you will use a char, right? + +13 +00:00:23,000 --> 00:00:25,000 +So Java does provide you primitive data types, + +14 +00:00:25,000 --> 00:00:28,000 +and that's what makes Java + +15 +00:00:28,000 --> 00:00:31,000 +99.99% object-oriented. + +16 +00:00:31,000 --> 00:00:34,000 +That's right, Java is not purely object-oriented + +17 +00:00:34,000 --> 00:00:36,000 +because say when you talk about a language + +18 +00:00:36,000 --> 00:00:37,000 +like object-oriented programming language, + +19 +00:00:37,000 --> 00:00:40,000 +it means that everything should be object. + +20 +00:00:40,000 --> 00:00:41,000 +But that's not the case with Java. + +21 +00:00:41,000 --> 00:00:42,000 +Java says, okay, we are still going to work + +22 +00:00:42,000 --> 00:00:45,000 +with something which is not extending object. + +23 +00:00:45,000 --> 00:00:49,000 +As we know, every class in Java extends object class, + +24 +00:00:49,000 --> 00:00:50,000 +but not the primitive values. + +25 +00:00:50,000 --> 00:00:51,000 +If I talk about INT, float, + +26 +00:00:51,000 --> 00:00:53,000 +they are primitive types. + +27 +00:00:53,000 --> 00:00:55,000 +They are not object types, right? + +28 +00:00:56,000 --> 00:01:00,000 +It's good because it helps Java to improve the performance + +29 +00:01:00,000 --> 00:01:02,000 +because it directly works with the fixed site value, right? + +30 +00:01:02,000 --> 00:01:06,000 +So it is not saved in a heap memory as an object, + +31 +00:01:06,000 --> 00:01:08,000 +which will be time consuming. + +32 +00:01:08,000 --> 00:01:10,000 +So what it says is, if you want to show the value, + +33 +00:01:10,000 --> 00:01:12,000 +you can do that directly in primitive, + +34 +00:01:12,000 --> 00:01:14,000 +but then there are certain features, + +35 +00:01:14,000 --> 00:01:15,000 +there are certain frameworks in Java, + +36 +00:01:15,000 --> 00:01:17,000 +which only works with objects. + +37 +00:01:17,000 --> 00:01:18,000 +Example, let's say in future, + +38 +00:01:18,000 --> 00:01:20,000 +we are gonna talk about collection framework. + +39 +00:01:20,000 --> 00:01:23,000 +In collection, if you want to store values. + +40 +00:01:23,000 --> 00:01:24,000 +So just to give you a heads up, + +41 +00:01:24,000 --> 00:01:28,000 +collection is basically similar to array at this point. + +42 +00:01:28,000 --> 00:01:30,000 +Just remember that. And we can have multiple values. + +43 +00:01:30,000 --> 00:01:33,000 +In array, we can have these primitive types, + +44 +00:01:33,000 --> 00:01:34,000 +but not in collection. + +45 +00:01:34,000 --> 00:01:36,000 +You need to have object type, + +46 +00:01:36,000 --> 00:01:38,000 +and that's why Java says, let me introduce something. + +47 +00:01:38,000 --> 00:01:42,000 +So in Java, we got this concept called a wrapper class. + +48 +00:01:42,000 --> 00:01:43,000 +Now what's a wrapper class? + +49 +00:01:43,000 --> 00:01:46,000 +For every primitive type, + +50 +00:01:46,000 --> 00:01:49,000 +we are going to have a class for it. + +51 +00:01:49,000 --> 00:01:51,000 +Okay? So if you want to work with INT, + +52 +00:01:51,000 --> 00:01:53,000 +you can work with integer, which is a class. + +53 +00:01:53,000 --> 00:01:56,000 +And this integer class basically extends the object class. + +54 +00:01:56,000 --> 00:01:59,000 +You have a char, we have character as a class, + +55 +00:01:59,000 --> 00:02:01,000 +we have double, we have a double as a class, + +56 +00:02:01,000 --> 00:02:03,000 +and this goes on, maybe it is short. + +57 +00:02:03,000 --> 00:02:04,000 +Then we have byte. + +58 +00:02:04,000 --> 00:02:08,000 +For every primitive type, we have a class for it, okay? + +59 +00:02:08,000 --> 00:02:12,000 +And it's quite good if you want to work with a framework, + +60 +00:02:12,000 --> 00:02:15,000 +which only supports objects. + +61 +00:02:15,000 --> 00:02:16,000 +But yes, if you want to improve the performance + +62 +00:02:16,000 --> 00:02:19,000 +and there's no restrictions, primitive still makes sense. + +63 +00:02:19,000 --> 00:02:21,000 +And that's why most of the examples + +64 +00:02:21,000 --> 00:02:22,000 +we have used primitive type. + +65 +00:02:22,000 --> 00:02:24,000 +But again, there are certain requirements + +66 +00:02:24,000 --> 00:02:26,000 +where you have to use classes. + +67 +00:02:26,000 --> 00:02:28,000 +Now how do we do that? So let's cut back here. + +68 +00:02:28,000 --> 00:02:29,000 +Let's say I have a INT variable + +69 +00:02:29,000 --> 00:02:32,000 +INT NUM is equal to seven. + +70 +00:02:32,000 --> 00:02:35,000 +Now how do I store this value into a class type + +71 +00:02:35,000 --> 00:02:36,000 +or the object type? + +72 +00:02:36,000 --> 00:02:39,000 +So the class we have is called integer, right? + +73 +00:02:39,000 --> 00:02:41,000 +So let me get a variable called NUM1. + +74 +00:02:41,000 --> 00:02:44,000 +So that's my reference object. + +75 +00:02:44,000 --> 00:02:45,000 +Now a reference variable, this is a primitive variable. + +76 +00:02:45,000 --> 00:02:47,000 +This is a reference variable. + +77 +00:02:47,000 --> 00:02:51,000 +Now since this is a class integer, we can say new integer. + +78 +00:02:52,000 --> 00:02:54,000 +And in this integer we can say + +79 +00:02:54,000 --> 00:02:55,000 +whatever value you want to store. + +80 +00:02:55,000 --> 00:02:57,000 +Let's say I want to store eight. + +81 +00:02:57,000 --> 00:02:59,000 +Now you can see there's a line on this. + +82 +00:02:59,000 --> 00:03:02,000 +So whenever you see a line in any of the IDE, + +83 +00:03:02,000 --> 00:03:05,000 +it simply means this particular syntax has been deprecated. + +84 +00:03:05,000 --> 00:03:07,000 +Deprecated simply means it was dead. + +85 +00:03:07,000 --> 00:03:09,000 +Don't use it now because in future release, + +86 +00:03:09,000 --> 00:03:10,000 +they might remove it, + +87 +00:03:10,000 --> 00:03:13,000 +or we have a better alternative for that. + +88 +00:03:13,000 --> 00:03:14,000 +But yeah, this is one of the syntax we can use. + +89 +00:03:14,000 --> 00:03:16,000 +So we have new integer, + +90 +00:03:16,000 --> 00:03:18,000 +and you can parse a value, which you want to store. + +91 +00:03:18,000 --> 00:03:21,000 +And of course we will see what is a better way + +92 +00:03:21,000 --> 00:03:22,000 +of doing this apart from this replicated. + +93 +00:03:22,000 --> 00:03:24,000 +But at this point, let's continue with that. + +94 +00:03:24,000 --> 00:03:26,000 +And let me print NUM1, + +95 +00:03:26,000 --> 00:03:27,000 +and let's see if that works + +96 +00:03:27,000 --> 00:03:29,000 +in the same way as we were doing. + +97 +00:03:29,000 --> 00:03:32,000 +Okay, you can see we got a warning, there's no error. + +98 +00:03:32,000 --> 00:03:35,000 +We got a warning, but let's run this code, + +99 +00:03:35,000 --> 00:03:36,000 +and you can see we got eight. + +100 +00:03:36,000 --> 00:03:39,000 +If this is not a good way of assigning a value, + +101 +00:03:39,000 --> 00:03:40,000 +what is a good way? + +102 +00:03:40,000 --> 00:03:42,000 +What we can actually do is instead of assigning like this, + +103 +00:03:42,000 --> 00:03:45,000 +we can directly assign a value called eight here. + +104 +00:03:45,000 --> 00:03:48,000 +And this works. We can see there's no deprecated warning. + +105 +00:03:48,000 --> 00:03:49,000 +And if I compile this code and run, + +106 +00:03:49,000 --> 00:03:51,000 +there's no warning as well. + +107 +00:03:51,000 --> 00:03:53,000 +Now how this is happening, first of all, + +108 +00:03:53,000 --> 00:03:54,000 +for every time we have an integer, + +109 +00:03:54,000 --> 00:03:56,000 +we have a class type, right? + +110 +00:03:56,000 --> 00:03:58,000 +Now what is happening here is example, + +111 +00:03:58,000 --> 00:04:01,000 +let's say if you want to store this INT NUM variable, + +112 +00:04:01,000 --> 00:04:03,000 +which is a primitive type in this, instead of saying eight, + +113 +00:04:03,000 --> 00:04:06,000 +let me just do that here, I will parse NUM. + +114 +00:04:06,000 --> 00:04:10,000 +Now what we are doing is we are taking a primitive value + +115 +00:04:10,000 --> 00:04:11,000 +and storing that in a object. + +116 +00:04:11,000 --> 00:04:15,000 +Now this concept is called autoboxing. + +117 +00:04:15,000 --> 00:04:17,000 +So this concept is called boxing, not autoboxing. + +118 +00:04:17,000 --> 00:04:19,000 +This concept is called boxing + +119 +00:04:19,000 --> 00:04:21,000 +because you are taking a primitive value + +120 +00:04:21,000 --> 00:04:24,000 +and storing that in a primitive object + +121 +00:04:24,000 --> 00:04:26,000 +or in a wrapper object, in a class object. + +122 +00:04:26,000 --> 00:04:29,000 +That is called a boxing. + +123 +00:04:29,000 --> 00:04:31,000 +Now since this is so important, + +124 +00:04:31,000 --> 00:04:34,000 +what they've done is you can directly assign NUM now, + +125 +00:04:34,000 --> 00:04:37,000 +so this boxing is happening automatically. + +126 +00:04:37,000 --> 00:04:40,000 +See behind the scene, the NUM is a primitive type, right? + +127 +00:04:40,000 --> 00:04:42,000 +This NUM1 isn't reference variable. + +128 +00:04:42,000 --> 00:04:43,000 +So of course it should be an object. + +129 +00:04:43,000 --> 00:04:47,000 +So this will be getting converted to object automatically, + +130 +00:04:47,000 --> 00:04:49,000 +and that's why we say that is autoboxing + +131 +00:04:49,000 --> 00:04:51,000 +when you convert it automatically, okay? + +132 +00:04:52,000 --> 00:04:54,000 +How do you assign the value to again, a primitive value? + +133 +00:04:54,000 --> 00:04:56,000 +Let's say we have NUM2, + +134 +00:04:56,000 --> 00:04:59,000 +and I want to assign the value of NUM1 to NUM2 now. + +135 +00:04:59,000 --> 00:05:02,000 +In this case you can say NUM1, + +136 +00:05:02,000 --> 00:05:05,000 +and then this number is an object type, right? + +137 +00:05:05,000 --> 00:05:06,000 +You have to get the value. + +138 +00:05:06,000 --> 00:05:08,000 +So you can say get integer, + +139 +00:05:08,000 --> 00:05:11,000 +and you can parse the value, which is NUM1. + +140 +00:05:11,000 --> 00:05:13,000 +Or you can parse, you can simply say get integer. + +141 +00:05:13,000 --> 00:05:16,000 +Okay, get integer giving you an - + +142 +00:05:16,000 --> 00:05:18,000 +Okay, so not get integer, we have to - + +143 +00:05:18,000 --> 00:05:20,000 +we have to use a different method. + +144 +00:05:20,000 --> 00:05:22,000 +So you have to say dot what's the method name? + +145 +00:05:22,000 --> 00:05:24,000 +Yeah, so we got INT value. + +146 +00:05:24,000 --> 00:05:25,000 +So the method name is INT value. + +147 +00:05:25,000 --> 00:05:28,000 +So from this object, which is of type integer, + +148 +00:05:28,000 --> 00:05:29,000 +you can say integer class, + +149 +00:05:29,000 --> 00:05:31,000 +which has lot of different methods. + +150 +00:05:31,000 --> 00:05:33,000 +If you scroll down for different features, + +151 +00:05:33,000 --> 00:05:35,000 +you can use different methods here. + +152 +00:05:35,000 --> 00:05:37,000 +But then if you want to get the integer value, + +153 +00:05:37,000 --> 00:05:38,000 +you can say INT value. + +154 +00:05:38,000 --> 00:05:41,000 +And then if you try to print NUM2, okay, + +155 +00:05:41,000 --> 00:05:42,000 +it'll give you the same value. + +156 +00:05:42,000 --> 00:05:43,000 +Compile and run. + +157 +00:05:43,000 --> 00:05:46,000 +And you can see we got seven, right? + +158 +00:05:46,000 --> 00:05:50,000 +The thing is, if you do this, you are saying unboxing + +159 +00:05:50,000 --> 00:05:53,000 +because you are getting a value from the object type + +160 +00:05:53,000 --> 00:05:55,000 +to a primitive type, right? + +161 +00:05:55,000 --> 00:05:57,000 +But what if you don't do it manually? + +162 +00:05:57,000 --> 00:05:59,000 +You directly assign NUM1 will this work, + +163 +00:05:59,000 --> 00:06:01,000 +and you can say there's no problem. + +164 +00:06:01,000 --> 00:06:03,000 +Now since this is happening automatically, + +165 +00:06:03,000 --> 00:06:05,000 +we call this auto-unboxing. + +166 +00:06:05,000 --> 00:06:07,000 +So what is autoboxing? + +167 +00:06:07,000 --> 00:06:09,000 +Is when you store a primitive type + +168 +00:06:09,000 --> 00:06:11,000 +in the object automatically. + +169 +00:06:11,000 --> 00:06:13,000 +And what is auto-unboxing is when you take out the value, + +170 +00:06:13,000 --> 00:06:17,000 +the primitive value from the object. + +171 +00:06:17,000 --> 00:06:20,000 +So this is auto-unboxing, and as I mentioned before, + +172 +00:06:20,000 --> 00:06:21,000 +we have different classes. + +173 +00:06:21,000 --> 00:06:22,000 +Example, we can also use double, + +174 +00:06:22,000 --> 00:06:24,000 +Even double is a class. + +175 +00:06:24,000 --> 00:06:26,000 +So you can see it's a class, + +176 +00:06:26,000 --> 00:06:29,000 +and we can use character as well. + +177 +00:06:29,000 --> 00:06:30,000 +Just write out. + +178 +00:06:30,000 --> 00:06:33,000 +Now apart from this autoboxing and boxing, + +179 +00:06:33,000 --> 00:06:35,000 +INT, integer provides you one more thing. + +180 +00:06:35,000 --> 00:06:39,000 +Let's say I have a string data here, which is string. + +181 +00:06:39,000 --> 00:06:40,000 +Let's say STR. + +182 +00:06:40,000 --> 00:06:42,000 +And the value in this is actually seven + +183 +00:06:42,000 --> 00:06:45,000 +or maybe some other value, let's say 12. + +184 +00:06:45,000 --> 00:06:48,000 +Now I got this 12 here, which is a number, + +185 +00:06:48,000 --> 00:06:50,000 +but it is stored in a string format, okay? + +186 +00:06:50,000 --> 00:06:53,000 +And then if I want to print this, if I want to print STR, + +187 +00:06:53,000 --> 00:06:55,000 +maybe I want to perform some operation on this, + +188 +00:06:55,000 --> 00:06:57,000 +maybe I want to double the value. + +189 +00:06:57,000 --> 00:07:01,000 +So can I say STR into two. This will not work, right? + +190 +00:07:01,000 --> 00:07:03,000 +Because STR is a string, + +191 +00:07:03,000 --> 00:07:05,000 +you can't perform an operation there. + +192 +00:07:05,000 --> 00:07:06,000 +So what you can do is + +193 +00:07:06,000 --> 00:07:08,000 +you can take this value into integer format. + +194 +00:07:08,000 --> 00:07:09,000 +You can say integer. + +195 +00:07:10,000 --> 00:07:12,000 +Maybe NUM3. + +196 +00:07:12,000 --> 00:07:14,000 +And in this NUM3, I want to store this 12. + +197 +00:07:14,000 --> 00:07:18,000 +How do you get a integer from a string? + +198 +00:07:18,000 --> 00:07:20,000 +Now for that there is a class called integer, + +199 +00:07:20,000 --> 00:07:22,000 +which we are using, + +200 +00:07:22,000 --> 00:07:24,000 +and this class has a method called parse INT. + +201 +00:07:24,000 --> 00:07:27,000 +Now parse INT is a method which will take the string, + +202 +00:07:27,000 --> 00:07:30,000 +and it'll convert that into integer + +203 +00:07:30,000 --> 00:07:31,000 +and I can simply say STR, + +204 +00:07:31,000 --> 00:07:34,000 +and it will take the value from the STR, + +205 +00:07:34,000 --> 00:07:36,000 +convert that into integer and store it in the NUM3, + +206 +00:07:36,000 --> 00:07:38,000 +and you can perform NUM3 into two. + +207 +00:07:38,000 --> 00:07:40,000 +Okay, so integer is great. + +208 +00:07:40,000 --> 00:07:41,000 +This is called a wrapper class + +209 +00:07:41,000 --> 00:07:44,000 +because it's a wrapper around a primitive type, + +210 +00:07:44,000 --> 00:07:45,000 +but it provides you multiple features. + +211 +00:07:45,000 --> 00:07:47,000 +One of them is parse INT. + +212 +00:07:47,000 --> 00:07:50,000 +Let's see if that works. Compile and run. + +213 +00:07:50,000 --> 00:07:51,000 +And it worked. Can you see that? + +214 +00:07:51,000 --> 00:07:53,000 +We've got 24 here. + +215 +00:07:53,000 --> 00:07:55,000 +So yeah, that's about the wrapper classes. + +216 +00:07:55,000 --> 00:07:57,000 +As I mentioned, we have multiple classes available. + +217 +00:07:57,000 --> 00:07:58,000 +Try them out. + +218 +00:07:58,000 --> 00:08:00,000 +And if you directly assign a primitive value + +219 +00:08:00,000 --> 00:08:04,000 +to the object type, it is called autoboxing. + +220 +00:08:04,000 --> 00:08:05,000 +And when you want to fetch the value automatically, + +221 +00:08:05,000 --> 00:08:07,000 +that is auto-unboxing. + diff --git a/02 - Core Java/065 Quiz 3.html b/02 - Core Java/065 Quiz 3.html new file mode 100644 index 0000000000000000000000000000000000000000..12ac92943edc51bb469aae3e43f9cbf186152d70 --- /dev/null +++ b/02 - Core Java/065 Quiz 3.html @@ -0,0 +1,479 @@ + + + + + + + Quiz + + + + +
+
+

+

+
+
+
+ Score: 999 of + 999% +
+
Correct: 999
+
Incorrect: 999
+
+ +
+ + + + +
+ + + + diff --git a/02 - Core Java/external-links.txt b/02 - Core Java/external-links.txt new file mode 100644 index 0000000000000000000000000000000000000000..68294d4141c2eea5f9f5bc337056e8987efc1b2c --- /dev/null +++ b/02 - Core Java/external-links.txt @@ -0,0 +1,183 @@ + +001 Source-Code +https://github.com/navinreddy20/Javacode.git + +001 Core-Java-Source +https://go.telusko.com/javaCodeGit + +002 Source-code +https://github.com/navinreddy20/Javacode.git + +004 Source-code +https://github.com/navinreddy20/Javacode.git + +006 Source-code +https://github.com/navinreddy20/Javacode.git + +007 Source-code +https://github.com/navinreddy20/Javacode.git + +008 Source-code +https://github.com/navinreddy20/Javacode.git + +009 Source-Code +https://github.com/navinreddy20/Javacode.git + +010 Source-code +https://github.com/navinreddy20/Javacode.git + +012 Source-code +https://github.com/navinreddy20/Javacode.git + +013 Source-code +https://github.com/navinreddy20/Javacode.git + +014 Source-code +https://github.com/navinreddy20/Javacode.git + +015 Source-code +https://github.com/navinreddy20/Javacode.git + +016 Source-code +https://github.com/navinreddy20/Javacode.git + +017 Source-code +https://github.com/navinreddy20/Javacode.git + +018 Source-code +https://github.com/navinreddy20/Javacode.git + +019 Source-code +https://github.com/navinreddy20/Javacode.git + +020 Source-code +https://github.com/navinreddy20/Javacode.git + +021 Source-code +https://github.com/navinreddy20/Javacode.git + +022 Source-code +https://github.com/navinreddy20/Javacode.git + +023 Source-code +https://github.com/navinreddy20/Javacode.git + +025 Source-code +https://github.com/navinreddy20/Javacode.git + +026 Source-code +https://github.com/navinreddy20/Javacode.git + +027 Source-code +https://github.com/navinreddy20/Javacode.git + +028 Source-code +https://github.com/navinreddy20/Javacode.git + +029 Source-code +https://github.com/navinreddy20/Javacode.git + +030 Source-code +https://github.com/navinreddy20/Javacode.git + +031 Source-code +https://github.com/navinreddy20/Javacode.git + +032 Source-code +https://github.com/navinreddy20/Javacode.git + +033 Source-code +https://github.com/navinreddy20/Javacode.git + +034 Source-code +https://github.com/navinreddy20/Javacode.git + +035 Source-code +https://github.com/navinreddy20/Javacode.git + +036 Source-code +https://github.com/navinreddy20/Javacode.git + +037 Source-code +https://github.com/navinreddy20/Javacode.git + +038 Source-code +https://github.com/navinreddy20/Javacode.git + +039 Source-code +https://github.com/navinreddy20/Javacode.git + +040 Source-code +https://github.com/navinreddy20/Javacode.git + +041 Source-code +https://github.com/navinreddy20/Javacode.git + +042 Source-code +https://github.com/navinreddy20/Javacode.git + +043 Source-code +https://github.com/navinreddy20/Javacode.git + +044 Source-code +https://github.com/navinreddy20/Javacode.git + +045 Source-code +https://github.com/navinreddy20/Javacode.git + +046 Source-code +https://github.com/navinreddy20/Javacode.git + +047 Source-code +https://github.com/navinreddy20/Javacode.git + +048 Source-code +https://github.com/navinreddy20/Javacode.git + +049 Source-code +https://github.com/navinreddy20/Javacode.git + +050 Source-code +https://github.com/navinreddy20/Javacode.git + +051 Source-code +https://github.com/navinreddy20/Javacode.git + +052 Source-code +https://github.com/navinreddy20/Javacode.git + +053 Source-code +https://github.com/navinreddy20/Javacode.git + +054 Source-code +https://github.com/navinreddy20/Javacode.git + +055 Source-code +https://github.com/navinreddy20/Javacode.git + +056 Source-code +https://github.com/navinreddy20/Javacode.git + +057 Source-code +https://github.com/navinreddy20/Javacode.git + +058 Source-code +https://github.com/navinreddy20/Javacode.git + +059 Source-code +https://github.com/navinreddy20/Javacode.git + +060 Source-code +https://github.com/navinreddy20/Javacode.git + +061 Source-code +https://github.com/navinreddy20/Javacode.git + +062 Source-code +https://github.com/navinreddy20/Javacode.git + +063 Source-code +https://github.com/navinreddy20/Javacode.git + +064 Source-Code +https://github.com/navinreddy20/Javacode.git diff --git a/03 - Advance Java/001 Project 1- Introduction_en.srt b/03 - Advance Java/001 Project 1- Introduction_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..f05057fbcef5e596ba36f6a50ae84ea7a02b3c2c --- /dev/null +++ b/03 - Advance Java/001 Project 1- Introduction_en.srt @@ -0,0 +1,336 @@ +1 +00:00:00,000 --> 00:00:02,000 +(soft bright music) + +2 +00:00:02,000 --> 00:00:03,000 +-: When you learn something, + +3 +00:00:03,000 --> 00:00:07,000 +it's also important to implement that in a project. + +4 +00:00:07,000 --> 00:00:09,000 +And we don't have to wait till the end of the course + +5 +00:00:09,000 --> 00:00:11,000 +to work on a project, right? + +6 +00:00:11,000 --> 00:00:14,000 +And that's where in between we are building a project, + +7 +00:00:14,000 --> 00:00:16,000 +and also we are going to continue this project + +8 +00:00:16,000 --> 00:00:19,000 +as the module pass on. + +9 +00:00:19,000 --> 00:00:21,000 +Now the idea of this project is very simple. + +10 +00:00:21,000 --> 00:00:24,000 +What we want to build is a quiz application + +11 +00:00:24,000 --> 00:00:26,000 +with whatever we have learned till now. + +12 +00:00:26,000 --> 00:00:28,000 +Now when I say quiz application, what it means? + +13 +00:00:28,000 --> 00:00:30,000 +Of course we all play quiz somewhere, right? + +14 +00:00:30,000 --> 00:00:32,000 +Maybe you have watched a quiz show, + +15 +00:00:32,000 --> 00:00:35,000 +or a quiz competition, on television, + +16 +00:00:35,000 --> 00:00:36,000 +or it doesn't matter where. + +17 +00:00:36,000 --> 00:00:39,000 +But the thing is, we all know what quiz is. + +18 +00:00:39,000 --> 00:00:40,000 +Basically you get a set of questions, + +19 +00:00:40,000 --> 00:00:44,000 +and then as a player, you give the answers. + +20 +00:00:44,000 --> 00:00:47,000 +And if you are right, you get a score. + +21 +00:00:47,000 --> 00:00:48,000 +So let's say you get five questions, + +22 +00:00:48,000 --> 00:00:52,000 +and for every right answer, you get one point, okay? + +23 +00:00:52,000 --> 00:00:55,000 +And then at the end, you get your total scores. + +24 +00:00:55,000 --> 00:00:56,000 +That's what we want to build here. + +25 +00:00:56,000 --> 00:01:00,000 +Now for this, of course, I have a lot of requirements + +26 +00:01:00,000 --> 00:01:01,000 +coming to this particular project. + +27 +00:01:01,000 --> 00:01:04,000 +Maybe I want to give real-time score, + +28 +00:01:04,000 --> 00:01:06,000 +or maybe I can give this score at the end. + +29 +00:01:06,000 --> 00:01:08,000 +Maybe I want to set the timer as well. + +30 +00:01:08,000 --> 00:01:11,000 +So as a user, if you want to play this game, + +31 +00:01:11,000 --> 00:01:14,000 +you have to answer each question in 10 to 15 seconds. + +32 +00:01:14,000 --> 00:01:16,000 +We can do that as well. + +33 +00:01:16,000 --> 00:01:17,000 +But again, we'll go step by step. + +34 +00:01:17,000 --> 00:01:18,000 +Initially, we'll keep it very simple, + +35 +00:01:18,000 --> 00:01:19,000 +and then we'll move forward. + +36 +00:01:19,000 --> 00:01:21,000 +So the first thing is we have to understand + +37 +00:01:21,000 --> 00:01:22,000 +the project scenario here. + +38 +00:01:22,000 --> 00:01:24,000 +So what are the roles we need? + +39 +00:01:24,000 --> 00:01:26,000 +Basically, I need two roles here. + +40 +00:01:26,000 --> 00:01:29,000 +We need an admin, and we need a user. + +41 +00:01:29,000 --> 00:01:31,000 +Now why do we need an admin here? + +42 +00:01:31,000 --> 00:01:34,000 +Maybe you can say, instead of admin, we can say trainer. + +43 +00:01:34,000 --> 00:01:37,000 +As example, if I'm a trainer and if I'm teaching a class, + +44 +00:01:37,000 --> 00:01:40,000 +basically I want to create those questions, right? + +45 +00:01:40,000 --> 00:01:42,000 +So that when I go tomorrow to the classroom, + +46 +00:01:42,000 --> 00:01:44,000 +I can give them the quiz. + +47 +00:01:44,000 --> 00:01:47,000 +Now here, as a trainer, it is my responsibility + +48 +00:01:47,000 --> 00:01:48,000 +to create those questions. + +49 +00:01:48,000 --> 00:01:51,000 +So in one quiz, let's say we have five questions. + +50 +00:01:51,000 --> 00:01:53,000 +And then we have the other role, which is a player, + +51 +00:01:53,000 --> 00:01:55,000 +where a player will play the game. + +52 +00:01:55,000 --> 00:01:57,000 +Basically, they can say, okay, I want to play the quiz, + +53 +00:01:57,000 --> 00:01:58,000 +they will get five questions, + +54 +00:01:58,000 --> 00:02:01,000 +and they will choose the answers. + +55 +00:02:01,000 --> 00:02:03,000 +Now at this point, we don't have any DUIs, + +56 +00:02:03,000 --> 00:02:05,000 +of course, we'll not be creating that. + +57 +00:02:05,000 --> 00:02:06,000 +We'll work this with a console level. + +58 +00:02:06,000 --> 00:02:08,000 +So what are the things we need? + +59 +00:02:08,000 --> 00:02:10,000 +Basically, when a player start the game, + +60 +00:02:10,000 --> 00:02:12,000 +or anyone starts the game, + +61 +00:02:12,000 --> 00:02:14,000 +they will get an option of choosing who they are. + +62 +00:02:14,000 --> 00:02:16,000 +Are they a student or a trainer? + +63 +00:02:18,000 --> 00:02:20,000 +We'll not be starting with that complex initially, + +64 +00:02:20,000 --> 00:02:23,000 +we'll do it very easily, but we need this two. + +65 +00:02:23,000 --> 00:02:26,000 +Plus, when a user is playing it, + +66 +00:02:26,000 --> 00:02:28,000 +it should show the questions one by one, + +67 +00:02:28,000 --> 00:02:30,000 +and every time it'll ask the user, + +68 +00:02:30,000 --> 00:02:33,000 +hey, this is a question, what's your answer? + +69 +00:02:33,000 --> 00:02:35,000 +And a user can choose an option, right? + +70 +00:02:35,000 --> 00:02:36,000 +A or B or C, + +71 +00:02:36,000 --> 00:02:40,000 +or maybe a user can choose the exact name of the option. + +72 +00:02:40,000 --> 00:02:42,000 +So that's something we want to build. + +73 +00:02:42,000 --> 00:02:43,000 +Now to understand this, what we're going to do is, + +74 +00:02:43,000 --> 00:02:46,000 +of course, once we start going we'll get everything. + +75 +00:02:46,000 --> 00:02:47,000 +So what are the things we need? + +76 +00:02:47,000 --> 00:02:49,000 +Basically we need a editor. + +77 +00:02:49,000 --> 00:02:51,000 +We will continue to use VS Code. + +78 +00:02:51,000 --> 00:02:54,000 +Of course, you can use any IDE which you love, + +79 +00:02:54,000 --> 00:02:56,000 +IntelliJ IDEA, Eclipse, or we have multiple things, + +80 +00:02:56,000 --> 00:02:58,000 +but VS Code works. + +81 +00:02:58,000 --> 00:03:01,000 +And I'm creating a project here, empty project. + +82 +00:03:01,000 --> 00:03:03,000 +But what are the files we need? + +83 +00:03:03,000 --> 00:03:04,000 +And how do we get started? + +84 +00:03:04,000 --> 00:03:07,000 +And that we'll see in the next video. + diff --git a/03 - Advance Java/002 Project 1- QuestionService and Question class_en.srt b/03 - Advance Java/002 Project 1- QuestionService and Question class_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..b036e19e27f13f44e552cda1bbd59f8986eae96c --- /dev/null +++ b/03 - Advance Java/002 Project 1- QuestionService and Question class_en.srt @@ -0,0 +1,640 @@ +1 +00:00:00,000 --> 00:00:02,000 +(bright music) + +2 +00:00:02,000 --> 00:00:03,000 +-: So let's get started. + +3 +00:00:03,000 --> 00:00:06,000 +So what we need here is a main file, right? + +4 +00:00:06,000 --> 00:00:09,000 +Of course we don't have any file in this project yet. + +5 +00:00:09,000 --> 00:00:11,000 +Of course, you can create a basic project + +6 +00:00:11,000 --> 00:00:12,000 +where you have different folders, + +7 +00:00:12,000 --> 00:00:13,000 +but just to keep it simple, + +8 +00:00:13,000 --> 00:00:15,000 +what I will do is I will create a new file here + +9 +00:00:15,000 --> 00:00:18,000 +and I will name this file as maybe main, + +10 +00:00:18,000 --> 00:00:22,000 +because in this I'm going to have my main code + +11 +00:00:22,000 --> 00:00:25,000 +so I can simply say main, and it'll give me a main function, + +12 +00:00:25,000 --> 00:00:28,000 +but also this need to be a part of a class. + +13 +00:00:28,000 --> 00:00:31,000 +So I will say class main. So we got to our main method here. + +14 +00:00:31,000 --> 00:00:34,000 +Now, what I do from here is just to test. + +15 +00:00:34,000 --> 00:00:36,000 +If things are working out, I can simply say so + +16 +00:00:36,000 --> 00:00:40,000 +and hello just to see if this, how this is working. + +17 +00:00:40,000 --> 00:00:42,000 +Let's run this. + +18 +00:00:42,000 --> 00:00:44,000 +And also, I want to make sure + +19 +00:00:44,000 --> 00:00:48,000 +that I have my things ready here. + +20 +00:00:48,000 --> 00:00:50,000 +The thing is, my JDK gives a lot of things, + +21 +00:00:50,000 --> 00:00:51,000 +but the output is hello. + +22 +00:00:51,000 --> 00:00:54,000 +So that's what we wanted. So we got hello. + +23 +00:00:54,000 --> 00:00:55,000 +And that looks cool. + +24 +00:00:55,000 --> 00:00:56,000 +So this working out, okay, + +25 +00:00:56,000 --> 00:00:58,000 +but then I don't want to do any + +26 +00:00:58,000 --> 00:01:00,000 +of this stuff in this particular main. + +27 +00:01:00,000 --> 00:01:03,000 +So normally in your main, most of the things will be empty. + +28 +00:01:03,000 --> 00:01:05,000 +So if you wanna do something, let's create another class. + +29 +00:01:05,000 --> 00:01:06,000 +So what we want to do here is we want + +30 +00:01:06,000 --> 00:01:08,000 +to work with quiz, right? + +31 +00:01:08,000 --> 00:01:10,000 +And this quiz will have questions. + +32 +00:01:10,000 --> 00:01:11,000 +If you think about quiz, + +33 +00:01:11,000 --> 00:01:14,000 +the most important thing is the questions. + +34 +00:01:14,000 --> 00:01:16,000 +So what I will do is I will create another file here + +35 +00:01:16,000 --> 00:01:19,000 +where I will do all the work with the questions. + +36 +00:01:19,000 --> 00:01:22,000 +So creating questions, displaying the questions to the user, + +37 +00:01:22,000 --> 00:01:24,000 +or maybe playing a game. + +38 +00:01:24,000 --> 00:01:28,000 +So I will put that to the QuestionService.java. + +39 +00:01:28,000 --> 00:01:30,000 +Of course, there's no need to have service word, + +40 +00:01:30,000 --> 00:01:32,000 +but that's, that perfectly works. + +41 +00:01:32,000 --> 00:01:33,000 +So let's have question service. + +42 +00:01:33,000 --> 00:01:35,000 +It should also explain what we are doing, right? + +43 +00:01:35,000 --> 00:01:36,000 +So question, service. + +44 +00:01:36,000 --> 00:01:40,000 +And in this, let's do our actual work. + +45 +00:01:40,000 --> 00:01:42,000 +So what I'm going to do is, + +46 +00:01:42,000 --> 00:01:44,000 +the first step would be if you say you have questions, + +47 +00:01:44,000 --> 00:01:46,000 +how will you represent question? + +48 +00:01:46,000 --> 00:01:49,000 +Because every question will have certain things, right? + +49 +00:01:49,000 --> 00:01:50,000 +It'll have a question ID, + +50 +00:01:50,000 --> 00:01:52,000 +it'll have a question, actual question. + +51 +00:01:52,000 --> 00:01:54,000 +It'll have four options. + +52 +00:01:54,000 --> 00:01:58,000 +And then out of four, we'll be having one correct answer. + +53 +00:01:58,000 --> 00:01:59,000 +So how do you represent that? + +54 +00:01:59,000 --> 00:02:02,000 +So one thing I can do, which is very common, + +55 +00:02:02,000 --> 00:02:06,000 +maybe I can have int which represents an ID. + +56 +00:02:06,000 --> 00:02:08,000 +I can have a string, + +57 +00:02:08,000 --> 00:02:11,000 +which represents a question, the actual question. + +58 +00:02:11,000 --> 00:02:13,000 +Maybe instead of saying question, yeah, let's go, go + +59 +00:02:13,000 --> 00:02:17,000 +for question, and then we will have options like string, + +60 +00:02:17,000 --> 00:02:20,000 +OPT1, and then so on, right? + +61 +00:02:20,000 --> 00:02:23,000 +But what if you want to show five questions? + +62 +00:02:23,000 --> 00:02:24,000 +So don't you think we can write? + +63 +00:02:24,000 --> 00:02:27,000 +Of course, we can write ID one. + +64 +00:02:27,000 --> 00:02:30,000 +And then one more variable ID two. Same for question. + +65 +00:02:30,000 --> 00:02:33,000 +So instead of doing that, we have learned one concept, + +66 +00:02:33,000 --> 00:02:34,000 +which is using an array. + +67 +00:02:34,000 --> 00:02:37,000 +Okay? So instead of having five IDs, we can have one array. + +68 +00:02:37,000 --> 00:02:38,000 +How do we do that? + +69 +00:02:38,000 --> 00:02:42,000 +So, what we can do is we can create a array of ID, + +70 +00:02:42,000 --> 00:02:44,000 +we can create array of questions, + +71 +00:02:44,000 --> 00:02:46,000 +we can create ID array of options. + +72 +00:02:46,000 --> 00:02:51,000 +But again, the problem is why to have this thing separately. + +73 +00:02:51,000 --> 00:02:52,000 +In the concept of, OOPs + +74 +00:02:52,000 --> 00:02:54,000 +we have also understood there's something + +75 +00:02:54,000 --> 00:02:55,000 +called object, right? + +76 +00:02:55,000 --> 00:02:57,000 +Where you can, you can have a class, + +77 +00:02:57,000 --> 00:02:59,000 +and in class we can have multiple variables. + +78 +00:02:59,000 --> 00:03:01,000 +And when you create one object of it, + +79 +00:03:01,000 --> 00:03:04,000 +that represents one entity, one object, right? + +80 +00:03:04,000 --> 00:03:09,000 +So instead of having a array of ID question OPT one, + +81 +00:03:09,000 --> 00:03:12,000 +what if you can create one question class, + +82 +00:03:12,000 --> 00:03:14,000 +which will hold all the values + +83 +00:03:14,000 --> 00:03:16,000 +and then create a array of the questions? + +84 +00:03:16,000 --> 00:03:19,000 +What I'm saying is let's remove all these things. + +85 +00:03:19,000 --> 00:03:21,000 +Let's create a separate class, + +86 +00:03:21,000 --> 00:03:23,000 +which will represent a question basically. + +87 +00:03:23,000 --> 00:03:25,000 +So this is a question class. + +88 +00:03:25,000 --> 00:03:28,000 +And in this question class, I can have an ID, + +89 +00:03:28,000 --> 00:03:33,000 +I can have the string question, I can have string, OPT1, + +90 +00:03:33,000 --> 00:03:35,000 +and the list goes on. + +91 +00:03:35,000 --> 00:03:37,000 +We can keep it string or integer before the options. + +92 +00:03:37,000 --> 00:03:39,000 +But then in most of the questions, + +93 +00:03:39,000 --> 00:03:41,000 +you'll be having answers which are, + +94 +00:03:41,000 --> 00:03:42,000 +or the options which are strings. + +95 +00:03:42,000 --> 00:03:43,000 +So let's go with string. + +96 +00:03:43,000 --> 00:03:44,000 +And even if you have numbers, + +97 +00:03:44,000 --> 00:03:47,000 +you can represent that in string as well. + +98 +00:03:47,000 --> 00:03:49,000 +You just have to put double codes. And there's OPT4. + +99 +00:03:49,000 --> 00:03:51,000 +So these are the four options you will have. + +100 +00:03:51,000 --> 00:03:54,000 +And also we are going to have the right answer. + +101 +00:03:54,000 --> 00:03:56,000 +So I can say answer. Okay? + +102 +00:03:56,000 --> 00:03:58,000 +Now one more thing you can do. + +103 +00:03:58,000 --> 00:03:59,000 +Instead of creating four options like this, + +104 +00:03:59,000 --> 00:04:04,000 +you can simply create one string array by saying options. + +105 +00:04:04,000 --> 00:04:06,000 +And then you can have four values there. + +106 +00:04:06,000 --> 00:04:08,000 +Again, that's your choice, but this looks much simpler + +107 +00:04:08,000 --> 00:04:11,000 +to work with and we got answer as well. + +108 +00:04:11,000 --> 00:04:13,000 +So this is what you are doing. + +109 +00:04:13,000 --> 00:04:14,000 +The only thing is you have to make sure + +110 +00:04:14,000 --> 00:04:16,000 +that all the variables are private. + +111 +00:04:16,000 --> 00:04:18,000 +In the concept of encapsulation, + +112 +00:04:18,000 --> 00:04:19,000 +we have seen this, right? + +113 +00:04:19,000 --> 00:04:22,000 +So variables and methods works together + +114 +00:04:22,000 --> 00:04:25,000 +and you have to make sure that your variables are private. + +115 +00:04:25,000 --> 00:04:27,000 +So that's done. All the variables are private. + +116 +00:04:27,000 --> 00:04:29,000 +But then how will you access it? + +117 +00:04:29,000 --> 00:04:31,000 +Let's go back to encapsulation. + +118 +00:04:31,000 --> 00:04:33,000 +If you want to access this variable, + +119 +00:04:33,000 --> 00:04:35,000 +you need getters and setters, right? + +120 +00:04:35,000 --> 00:04:37,000 +So getters which will get data, + +121 +00:04:37,000 --> 00:04:39,000 +setters which will set the data. + +122 +00:04:39,000 --> 00:04:41,000 +So I can simply right click here + +123 +00:04:41,000 --> 00:04:45,000 +and I can say, source action, generate getters + +124 +00:04:45,000 --> 00:04:48,000 +and setters, okay, for how many variables you need + +125 +00:04:48,000 --> 00:04:49,000 +for all the variables, select that. + +126 +00:04:49,000 --> 00:04:53,000 +Click on, okay, and you can see we got all the + +127 +00:04:53,000 --> 00:04:54,000 +getters and setters. + +128 +00:04:54,000 --> 00:04:56,000 +I know it's huge, but we are not going + +129 +00:04:56,000 --> 00:04:58,000 +to open this file one again, right? + +130 +00:04:58,000 --> 00:04:59,000 +It's done and dusted. + +131 +00:04:59,000 --> 00:05:02,000 +So one more thing, we also need a constructor. Okay? + +132 +00:05:02,000 --> 00:05:03,000 +Why do we need a constructor? + +133 +00:05:03,000 --> 00:05:05,000 +What if you want to create a question + +134 +00:05:05,000 --> 00:05:07,000 +and while creating a question, maybe I want + +135 +00:05:07,000 --> 00:05:08,000 +to pass all the values, + +136 +00:05:08,000 --> 00:05:09,000 +or maybe we, let's do that later so + +137 +00:05:09,000 --> 00:05:11,000 +that it'll make much more sense. + +138 +00:05:11,000 --> 00:05:12,000 +So let's have, getters and setters that's it, + +139 +00:05:12,000 --> 00:05:14,000 +nothing much here. + +140 +00:05:14,000 --> 00:05:16,000 +Go back to your question service. + +141 +00:05:16,000 --> 00:05:19,000 +Now what I was saying is instead of creating the array + +142 +00:05:19,000 --> 00:05:22,000 +of individual values, now all the variables are there + +143 +00:05:22,000 --> 00:05:24,000 +inside the question class. + +144 +00:05:24,000 --> 00:05:29,000 +So what I can do is I can create the question array, okay? + +145 +00:05:29,000 --> 00:05:32,000 +So I will name this as questions equal to new + +146 +00:05:32,000 --> 00:05:35,000 +questions, right? + +147 +00:05:35,000 --> 00:05:37,000 +And I can specify how many questions I need. + +148 +00:05:37,000 --> 00:05:39,000 +So I need only five question at this point. + +149 +00:05:39,000 --> 00:05:42,000 +So we got a array of questions, + +150 +00:05:42,000 --> 00:05:43,000 +nothing fancy right? + +151 +00:05:43,000 --> 00:05:46,000 +Now at this point we have done + +152 +00:05:46,000 --> 00:05:49,000 +with our question class, we have done + +153 +00:05:49,000 --> 00:05:50,000 +with the question service. + +154 +00:05:50,000 --> 00:05:52,000 +But how will you add the question? + +155 +00:05:53,000 --> 00:05:55,000 +To start with, we'll have our own question hardcoded, + +156 +00:05:55,000 --> 00:05:57,000 +and then we'll see how do we get the + +157 +00:05:57,000 --> 00:05:58,000 +question from the user as well. + +158 +00:05:58,000 --> 00:06:01,000 +But at this point, we got our questions hardcoded, + +159 +00:06:01,000 --> 00:06:03,000 +but we have not done that yet. + +160 +00:06:03,000 --> 00:06:06,000 +So let's try to do that in the next video. + diff --git a/03 - Advance Java/003 Project 1- Hard Coded Questions and Display them_en.srt b/03 - Advance Java/003 Project 1- Hard Coded Questions and Display them_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..95ae9ece11eb1451dfd614807911c0fb8cb77e4b --- /dev/null +++ b/03 - Advance Java/003 Project 1- Hard Coded Questions and Display them_en.srt @@ -0,0 +1,948 @@ +1 +00:00:00,000 --> 00:00:03,000 +(melodic orchestral music) + +2 +00:00:03,000 --> 00:00:06,000 +-: Now basically we have to get the five questions hardcoded + +3 +00:00:06,000 --> 00:00:08,000 +so that we can play the game. + +4 +00:00:08,000 --> 00:00:08,000 +So let's do one thing. + +5 +00:00:08,000 --> 00:00:10,000 +So what I will do is I will create + +6 +00:00:10,000 --> 00:00:12,000 +a method called display questions, okay? + +7 +00:00:12,000 --> 00:00:15,000 +Because we got five questions, right? + +8 +00:00:15,000 --> 00:00:16,000 +I mean, that's what we are thinking now. + +9 +00:00:16,000 --> 00:00:18,000 +So I will say display questions, again, + +10 +00:00:18,000 --> 00:00:20,000 +we are not going to use this method later, + +11 +00:00:20,000 --> 00:00:23,000 +but at this point, let me display questions. + +12 +00:00:23,000 --> 00:00:25,000 +And the way you can do that is we got a method here, + +13 +00:00:25,000 --> 00:00:28,000 +and then we just have to call this method from the main. + +14 +00:00:28,000 --> 00:00:30,000 +So inside this main, + +15 +00:00:30,000 --> 00:00:32,000 +I need to get the object of caution service + +16 +00:00:32,000 --> 00:00:35,000 +because if you want to call display questions, + +17 +00:00:35,000 --> 00:00:38,000 +which is a part of a caution service, right? + +18 +00:00:38,000 --> 00:00:41,000 +So if you don't have the object for it, you can't call it. + +19 +00:00:41,000 --> 00:00:42,000 +So I would say question service, + +20 +00:00:42,000 --> 00:00:45,000 +I will say service as a variable name + +21 +00:00:45,000 --> 00:00:47,000 +and the new question service, right? + +22 +00:00:47,000 --> 00:00:51,000 +Once you got this with the help of this particular object + +23 +00:00:51,000 --> 00:00:54,000 +or the reference, we can simply call the display questions. + +24 +00:00:54,000 --> 00:00:57,000 +Now it is going to call this method, which is doing nothing. + +25 +00:00:57,000 --> 00:00:59,000 +But at this point, let me just print here + +26 +00:00:59,000 --> 00:01:01,000 +in display questions + +27 +00:01:01,000 --> 00:01:03,000 +so that we'll know something is happening. + +28 +00:01:03,000 --> 00:01:04,000 +Let's go back to main. + +29 +00:01:04,000 --> 00:01:05,000 +Okay, so how you run this, + +30 +00:01:05,000 --> 00:01:07,000 +you can simply click on this button + +31 +00:01:07,000 --> 00:01:10,000 +and you will get the console here, right? + +32 +00:01:10,000 --> 00:01:12,000 +Maybe I just want to clear the screen, + +33 +00:01:12,000 --> 00:01:15,000 +and also I want to keep it on the right hand side. + +34 +00:01:15,000 --> 00:01:18,000 +Will it be a good place? Okay, this looks good. + +35 +00:01:19,000 --> 00:01:20,000 +I'll just run this once again, + +36 +00:01:20,000 --> 00:01:21,000 +and you can see we got the output + +37 +00:01:21,000 --> 00:01:23,000 +which is in display question. + +38 +00:01:23,000 --> 00:01:26,000 +So that means we are able to run this, okay. + +39 +00:01:26,000 --> 00:01:28,000 +But I don't want this particular thing here. + +40 +00:01:28,000 --> 00:01:30,000 +What I want is I want to display question right? + +41 +00:01:30,000 --> 00:01:32,000 +Now how do you display each question? + +42 +00:01:32,000 --> 00:01:34,000 +Of course, I can print one question here. + +43 +00:01:34,000 --> 00:01:36,000 +Let's print one question. + +44 +00:01:36,000 --> 00:01:39,000 +So the way you can access that is I can say print. + +45 +00:01:39,000 --> 00:01:42,000 +Now since this is an array, I can say questions, + +46 +00:01:42,000 --> 00:01:44,000 +but it'll print the entire array, right? + +47 +00:01:44,000 --> 00:01:46,000 +And that's not how things works out in Java. + +48 +00:01:46,000 --> 00:01:50,000 +You can see, okay, let me just clear this and read on this. + +49 +00:01:50,000 --> 00:01:53,000 +So you can see it says something weird output here, + +50 +00:01:53,000 --> 00:01:53,000 +which is this. + +51 +00:01:53,000 --> 00:01:57,000 +So it says quad packet L and then questions. + +52 +00:01:57,000 --> 00:02:00,000 +The thing is if you want to print the data, okay, + +53 +00:02:00,000 --> 00:02:03,000 +not the array, we have to print each element, + +54 +00:02:03,000 --> 00:02:04,000 +because since we are printing the array, + +55 +00:02:04,000 --> 00:02:06,000 +that's what is doing it. + +56 +00:02:06,000 --> 00:02:08,000 +We need to print each element. + +57 +00:02:08,000 --> 00:02:10,000 +So let me print the first question. + +58 +00:02:10,000 --> 00:02:11,000 +Okay, so when, when you say first question, + +59 +00:02:11,000 --> 00:02:13,000 +that means from the array, + +60 +00:02:13,000 --> 00:02:14,000 +we need to get the first question, + +61 +00:02:14,000 --> 00:02:16,000 +and that's what we're doing here. + +62 +00:02:16,000 --> 00:02:18,000 +Let's run this once again. + +63 +00:02:18,000 --> 00:02:21,000 +And you can see now it says null, oh, that's weird. + +64 +00:02:21,000 --> 00:02:23,000 +Do we have question there? + +65 +00:02:23,000 --> 00:02:25,000 +Unfortunately, we don't have any question. + +66 +00:02:25,000 --> 00:02:28,000 +Example, let's say with this question here, + +67 +00:02:28,000 --> 00:02:31,000 +so of course the array represents one object, right? + +68 +00:02:31,000 --> 00:02:34,000 +So when I say question zero, it'll represent the first one, + +69 +00:02:34,000 --> 00:02:36,000 +not the case, because if you try to do that example, + +70 +00:02:36,000 --> 00:02:40,000 +if I try to get the question for a particular question, + +71 +00:02:40,000 --> 00:02:42,000 +so let's say we have five questions, right? + +72 +00:02:42,000 --> 00:02:44,000 +So if I try to fetch the question, + +73 +00:02:44,000 --> 00:02:47,000 +and what I know the name can be confusing, + +74 +00:02:47,000 --> 00:02:49,000 +but we have a question variable here, + +75 +00:02:49,000 --> 00:02:50,000 +which is the actual question. + +76 +00:02:50,000 --> 00:02:54,000 +What is Java or what's the size of anything Java? + +77 +00:02:54,000 --> 00:02:57,000 +So we have this here and what I will do is I will try + +78 +00:02:57,000 --> 00:03:00,000 +to run this call and what you will get + +79 +00:03:00,000 --> 00:03:02,000 +is one of the favorite thing in Java, + +80 +00:03:02,000 --> 00:03:03,000 +which is the null point exception. + +81 +00:03:03,000 --> 00:03:07,000 +Let me wrap this, let me just fit it here. + +82 +00:03:07,000 --> 00:03:09,000 +So you can see we got an error, + +83 +00:03:09,000 --> 00:03:12,000 +which is a null pointer exception. + +84 +00:03:12,000 --> 00:03:13,000 +The reason we got null point exception is + +85 +00:03:13,000 --> 00:03:16,000 +if you are thinking we got the questions, no, + +86 +00:03:16,000 --> 00:03:17,000 +we have not got the question, + +87 +00:03:17,000 --> 00:03:20,000 +but what we got is the reference of the questions, + +88 +00:03:20,000 --> 00:03:22,000 +not the actual object. + +89 +00:03:22,000 --> 00:03:25,000 +So we need to initialize those object, example. + +90 +00:03:25,000 --> 00:03:28,000 +If I initialize the object by saying questions + +91 +00:03:28,000 --> 00:03:32,000 +of zero equal to new question. + +92 +00:03:32,000 --> 00:03:34,000 +Now this is where I'm creating an object, okay? + +93 +00:03:34,000 --> 00:03:36,000 +In the initial set, which is this, + +94 +00:03:36,000 --> 00:03:39,000 +this is just a array of references, + +95 +00:03:39,000 --> 00:03:41,000 +but not the actual object. + +96 +00:03:41,000 --> 00:03:44,000 +And now I follow on this by clear the screen, let's run. + +97 +00:03:44,000 --> 00:03:45,000 +And you can see it says null, + +98 +00:03:45,000 --> 00:03:47,000 +so there's no null point exception, but it is null, + +99 +00:03:47,000 --> 00:03:49,000 +reason being we got the object, + +100 +00:03:49,000 --> 00:03:52,000 +but the get question is still null, okay. + +101 +00:03:52,000 --> 00:03:55,000 +So that means when we create the question, + +102 +00:03:55,000 --> 00:03:58,000 +We need to make sure that we are creating the objects of it. + +103 +00:03:58,000 --> 00:04:00,000 +Now how will we do it? + +104 +00:04:00,000 --> 00:04:01,000 +Now since we want five questions, + +105 +00:04:01,000 --> 00:04:03,000 +what I can do is I can create a constructor here + +106 +00:04:03,000 --> 00:04:05,000 +for the question service, + +107 +00:04:05,000 --> 00:04:08,000 +and in this we have to manually create the object. + +108 +00:04:08,000 --> 00:04:12,000 +So I can say questions of zero, + +109 +00:04:12,000 --> 00:04:13,000 +the thing which we have done here, + +110 +00:04:13,000 --> 00:04:15,000 +this should not be done here. + +111 +00:04:15,000 --> 00:04:16,000 +We should be doing that in the constructor + +112 +00:04:16,000 --> 00:04:18,000 +so that it'll happen only once. + +113 +00:04:18,000 --> 00:04:20,000 +New question. + +114 +00:04:20,000 --> 00:04:22,000 +And likewise, I can type this statement five times. + +115 +00:04:22,000 --> 00:04:25,000 +Of course, we can also use a loop there to assign + +116 +00:04:25,000 --> 00:04:28,000 +the values or assign the instances, okay? + +117 +00:04:28,000 --> 00:04:30,000 +This is the actual instance. + +118 +00:04:30,000 --> 00:04:32,000 +But then even if you do that, + +119 +00:04:32,000 --> 00:04:34,000 +your questions values will be empty, right? + +120 +00:04:34,000 --> 00:04:37,000 +So we have so many things to fill up, right? + +121 +00:04:37,000 --> 00:04:40,000 +So what we can do here is we can pass the values here + +122 +00:04:40,000 --> 00:04:42,000 +in the constructor for the question. + +123 +00:04:42,000 --> 00:04:45,000 +That also means that we need to create a constructor, + +124 +00:04:45,000 --> 00:04:46,000 +which will take values, + +125 +00:04:46,000 --> 00:04:50,000 +so I click here and say source action, + +126 +00:04:50,000 --> 00:04:54,000 +I need a constructor which can accept all the values. + +127 +00:04:54,000 --> 00:04:55,000 +Click on, okay? + +128 +00:04:55,000 --> 00:04:56,000 +So you can see we've got a constructor here, + +129 +00:04:56,000 --> 00:04:57,000 +which will take all the values + +130 +00:04:57,000 --> 00:04:59,000 +and then you can come back here + +131 +00:04:59,000 --> 00:05:01,000 +and you can put up the values here. + +132 +00:05:01,000 --> 00:05:02,000 +Okay, example, + +133 +00:05:02,000 --> 00:05:05,000 +let's say the first thing we have is question ID. + +134 +00:05:05,000 --> 00:05:06,000 +Then we have some question, + +135 +00:05:06,000 --> 00:05:08,000 +I'm not sure what the question is. + +136 +00:05:08,000 --> 00:05:10,000 +I will simply say what, okay? + +137 +00:05:10,000 --> 00:05:12,000 +And then we'll be having option one. + +138 +00:05:12,000 --> 00:05:15,000 +Let's say option one is A, + +139 +00:05:15,000 --> 00:05:17,000 +I mean the actual thing, let's say Java. + +140 +00:05:18,000 --> 00:05:22,000 +Then option two is Cpp, which is C++, + +141 +00:05:22,000 --> 00:05:24,000 +option three is Python. + +142 +00:05:24,000 --> 00:05:26,000 +And then so there's a question maybe, + +143 +00:05:26,000 --> 00:05:30,000 +which of these language is founded in year 1995, + +144 +00:05:30,000 --> 00:05:32,000 +so on says Java, so let's do that. + +145 +00:05:32,000 --> 00:05:34,000 +And the last option we are saying is C+, + +146 +00:05:34,000 --> 00:05:36,000 +and then we can give a right answer + +147 +00:05:36,000 --> 00:05:38,000 +to all these are options here, right? + +148 +00:05:38,000 --> 00:05:41,000 +So if we got ID question OPD, again, + +149 +00:05:41,000 --> 00:05:43,000 +don't get confused with this things in grade. + +150 +00:05:43,000 --> 00:05:46,000 +This is basically given by your VS code just + +151 +00:05:46,000 --> 00:05:48,000 +to make you understand what value you are assigning. + +152 +00:05:48,000 --> 00:05:52,000 +It'll reduce the bugs. And the last one is the answer. + +153 +00:05:52,000 --> 00:05:54,000 +So answer out of this is Java, + +154 +00:05:54,000 --> 00:05:56,000 +so we can mention Java there, right? + +155 +00:05:56,000 --> 00:05:58,000 +This is how we can basically create questions, okay? + +156 +00:05:58,000 --> 00:06:00,000 +But then we, we need to do this for five times, right? + +157 +00:06:00,000 --> 00:06:02,000 +So instead of typing it manually in the video, + +158 +00:06:02,000 --> 00:06:03,000 +I will just copy paste it, okay. + +159 +00:06:03,000 --> 00:06:06,000 +So I have a file somewhere sitting, + +160 +00:06:06,000 --> 00:06:08,000 +so I got just entered this values here, + +161 +00:06:08,000 --> 00:06:11,000 +so we can say we have question zero, + +162 +00:06:11,000 --> 00:06:13,000 +I mean, we got five questions here, + +163 +00:06:13,000 --> 00:06:16,000 +and then these are the values which I've inserted. + +164 +00:06:16,000 --> 00:06:18,000 +Nothing fancy, just a simple thing. + +165 +00:06:18,000 --> 00:06:20,000 +The size of a, size of double size of cache, + +166 +00:06:20,000 --> 00:06:22,000 +size of long size of bullion, + +167 +00:06:22,000 --> 00:06:23,000 +and we've got all these options here + +168 +00:06:23,000 --> 00:06:25,000 +with the answers of course. + +169 +00:06:25,000 --> 00:06:26,000 +And now let's try to display the question. + +170 +00:06:26,000 --> 00:06:30,000 +So now if I try to display this and if I run this code, + +171 +00:06:30,000 --> 00:06:31,000 +okay, let me just clear this screen. + +172 +00:06:31,000 --> 00:06:36,000 +So if I try to do this, compile all the files and run main, + +173 +00:06:40,000 --> 00:06:42,000 +yeah, this looks much better, right? + +174 +00:06:42,000 --> 00:06:45,000 +So instead of clicking it, it gives the bunch of text, + +175 +00:06:45,000 --> 00:06:46,000 +let's do this. + +176 +00:06:46,000 --> 00:06:48,000 +So you can see when when I run this main, + +177 +00:06:48,000 --> 00:06:50,000 +it is giving me size of ink, okay? + +178 +00:06:50,000 --> 00:06:52,000 +So we got the first question, + +179 +00:06:52,000 --> 00:06:53,000 +but I want to print all the questions, right? + +180 +00:06:53,000 --> 00:06:55,000 +How will you do that? + +181 +00:06:55,000 --> 00:06:58,000 +So what I can do here is I will use a loop + +182 +00:06:58,000 --> 00:06:59,000 +to print all the values, + +183 +00:06:59,000 --> 00:07:02,000 +or maybe I can use the enhance follow loop, + +184 +00:07:02,000 --> 00:07:03,000 +which we have seen. + +185 +00:07:03,000 --> 00:07:07,000 +So I can say question Q, colon questions. + +186 +00:07:07,000 --> 00:07:08,000 +So question Q from questions, + +187 +00:07:08,000 --> 00:07:12,000 +and then I can open the curly package packets, + +188 +00:07:12,000 --> 00:07:13,000 +close it here, okay? + +189 +00:07:13,000 --> 00:07:15,000 +And now instead of printing this get question, + +190 +00:07:15,000 --> 00:07:18,000 +I want to print, or maybe I can just print get question. + +191 +00:07:18,000 --> 00:07:19,000 +The only thing is instead of saying zero here, + +192 +00:07:19,000 --> 00:07:21,000 +we should be saying Q now. + +193 +00:07:21,000 --> 00:07:23,000 +Because every time you run this loop, + +194 +00:07:23,000 --> 00:07:24,000 +the enhanced follow loop, + +195 +00:07:24,000 --> 00:07:27,000 +basically it'll take one question from the questions + +196 +00:07:27,000 --> 00:07:28,000 +and put it in the queue. + +197 +00:07:28,000 --> 00:07:30,000 +So we can say q.getQuestion, right? + +198 +00:07:30,000 --> 00:07:33,000 +So in the loop it'll run five times + +199 +00:07:33,000 --> 00:07:36,000 +and you will get five questions basically, + +200 +00:07:36,000 --> 00:07:37,000 +I dunno why I'm using this. + +201 +00:07:37,000 --> 00:07:39,000 +Let me just use this. + +202 +00:07:39,000 --> 00:07:41,000 +Compile all the files and run main, + +203 +00:07:41,000 --> 00:07:43,000 +and you can see we got all the questions, + +204 +00:07:43,000 --> 00:07:45,000 +but what if you want to print + +205 +00:07:45,000 --> 00:07:48,000 +the question, options, answer as well? + +206 +00:07:48,000 --> 00:07:50,000 +So what I can do is instead of printing the get question, + +207 +00:07:50,000 --> 00:07:53,000 +let me print the queue itself. + +208 +00:07:53,000 --> 00:07:55,000 +Now the problem is when you do this, + +209 +00:07:55,000 --> 00:07:57,000 +let me compile all the files again + +210 +00:07:57,000 --> 00:08:01,000 +and one, it'll print the question and some weird stuff. + +211 +00:08:01,000 --> 00:08:04,000 +It's just that if you want to print the object, + +212 +00:08:04,000 --> 00:08:06,000 +we have to use two string method. + +213 +00:08:06,000 --> 00:08:09,000 +So I can go back to my question and question + +214 +00:08:09,000 --> 00:08:13,000 +what we don't have in the question is a two stringing. + +215 +00:08:13,000 --> 00:08:14,000 +So I can right click here + +216 +00:08:14,000 --> 00:08:17,000 +and source action, generate two string. + +217 +00:08:17,000 --> 00:08:19,000 +So what two stringing will do is every time + +218 +00:08:19,000 --> 00:08:21,000 +we try to print the object, + +219 +00:08:21,000 --> 00:08:24,000 +it'll call the two stringing method, which is printing what? + +220 +00:08:24,000 --> 00:08:28,000 +So two basically prints the data in a particular format, + +221 +00:08:28,000 --> 00:08:30,000 +which is generated by the ID. + +222 +00:08:30,000 --> 00:08:32,000 +Of course you can type it manually as well, + +223 +00:08:32,000 --> 00:08:33,000 +that's your choice. + +224 +00:08:33,000 --> 00:08:34,000 +Now, after making the changes, + +225 +00:08:34,000 --> 00:08:37,000 +if I go back and compile this two, + +226 +00:08:37,000 --> 00:08:40,000 +run, and now you can see we got all the questions. + +227 +00:08:40,000 --> 00:08:42,000 +So we got question, question ID, + +228 +00:08:42,000 --> 00:08:46,000 +the actual question then options + +229 +00:08:46,000 --> 00:08:48,000 +four options and answer, right? + +230 +00:08:48,000 --> 00:08:50,000 +So at this point what we were able to do is we were able + +231 +00:08:50,000 --> 00:08:54,000 +to generate the questions, five questions here, + +232 +00:08:54,000 --> 00:08:55,000 +the hard coding way, + +233 +00:08:55,000 --> 00:08:58,000 +and then we were able to display the questions. + +234 +00:08:58,000 --> 00:08:59,000 +Now in the next video, + +235 +00:08:59,000 --> 00:09:01,000 +let's try to do something where a user can play the game, + +236 +00:09:01,000 --> 00:09:05,000 +at least a user can see the questions one by one. + +237 +00:09:05,000 --> 00:09:07,000 +Let's try do that in the upcoming videos. + diff --git a/03 - Advance Java/003 Source-Code.url b/03 - Advance Java/003 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..4c695cf501825d447ed987531cc3845423d626b8 --- /dev/null +++ b/03 - Advance Java/003 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/Gaurav560/quiz-console-app \ No newline at end of file diff --git a/03 - Advance Java/004 Project 1- Play Quiz_en.srt b/03 - Advance Java/004 Project 1- Play Quiz_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..ad2bea53af7d06b6b762b8e4c07eb4ee4d016a9b --- /dev/null +++ b/03 - Advance Java/004 Project 1- Play Quiz_en.srt @@ -0,0 +1,604 @@ +1 +00:00:00,000 --> 00:00:02,000 +(gentle music) + +2 +00:00:02,000 --> 00:00:04,000 +-: Now, basically we were able + +3 +00:00:04,000 --> 00:00:05,000 +to display the questions right? + +4 +00:00:05,000 --> 00:00:07,000 +Now it's time to play the quiz. + +5 +00:00:07,000 --> 00:00:08,000 +Now in the play the quiz, + +6 +00:00:08,000 --> 00:00:11,000 +what will happen is we will create a method, + +7 +00:00:11,000 --> 00:00:14,000 +which will display the questions, okay, not this one, + +8 +00:00:14,000 --> 00:00:15,000 +but it'll display the question. + +9 +00:00:15,000 --> 00:00:17,000 +And also for every question, + +10 +00:00:17,000 --> 00:00:20,000 +it'll take the input from the user. + +11 +00:00:20,000 --> 00:00:22,000 +What is their choice, right? What is their answer? + +12 +00:00:22,000 --> 00:00:23,000 +Now how do we do that? + +13 +00:00:23,000 --> 00:00:25,000 +So basically we can change + +14 +00:00:25,000 --> 00:00:26,000 +the display question itself. + +15 +00:00:26,000 --> 00:00:27,000 +Instead of saying display question, + +16 +00:00:27,000 --> 00:00:31,000 +I can say play quiz, right? + +17 +00:00:31,000 --> 00:00:33,000 +This will make much more sense, right? Play quiz. + +18 +00:00:33,000 --> 00:00:36,000 +And then, okay, also we need to change it here now. + +19 +00:00:36,000 --> 00:00:38,000 +We can't say display questions, + +20 +00:00:38,000 --> 00:00:40,000 +the method name has been changed. + +21 +00:00:41,000 --> 00:00:45,000 +So just play.quiz, right? And now go back here. + +22 +00:00:45,000 --> 00:00:47,000 +Now when you are playing quiz, you don't need + +23 +00:00:47,000 --> 00:00:48,000 +to just display the question, right? + +24 +00:00:48,000 --> 00:00:51,000 +We need to display the question, but not in this format. + +25 +00:00:51,000 --> 00:00:53,000 +It is printing all the question. + +26 +00:00:53,000 --> 00:00:55,000 +And after printing one question, + +27 +00:00:55,000 --> 00:00:59,000 +it should also ask the user what is their answer, right? + +28 +00:00:59,000 --> 00:01:01,000 +Okay, so what I'm saying is instead of printing + +29 +00:01:01,000 --> 00:01:04,000 +the entire question, I will print this one by one. + +30 +00:01:04,000 --> 00:01:07,000 +Maybe the first thing I'm going to print is the ID, + +31 +00:01:07,000 --> 00:01:09,000 +because I want to print the ID as well. + +32 +00:01:09,000 --> 00:01:13,000 +The only thing is when I say ID, maybe I want to + +33 +00:01:13,000 --> 00:01:16,000 +print because the ID number starts with one okay? + +34 +00:01:16,000 --> 00:01:17,000 +That's what we're doing here. + +35 +00:01:17,000 --> 00:01:19,000 +Yeah, we have ID number one that works. + +36 +00:01:19,000 --> 00:01:21,000 +I can simply say, getID. + +37 +00:01:21,000 --> 00:01:22,000 +It'll print the ID first + +38 +00:01:22,000 --> 00:01:27,000 +and maybe instead of printing just this, I will also print + +39 +00:01:27,000 --> 00:01:29,000 +question number so that our user will know + +40 +00:01:29,000 --> 00:01:31,000 +what we are printing. + +41 +00:01:31,000 --> 00:01:33,000 +So question number, it'll print the ID. + +42 +00:01:33,000 --> 00:01:35,000 +And then the next line, on the next line, + +43 +00:01:35,000 --> 00:01:37,000 +I want to print the actual question. + +44 +00:01:37,000 --> 00:01:42,000 +So I will say q.get question, that's done. + +45 +00:01:43,000 --> 00:01:45,000 +And then on the next line, I want to print the options, + +46 +00:01:45,000 --> 00:01:46,000 +all four options. + +47 +00:01:46,000 --> 00:01:51,000 +So I can say, print out, this is not print, q.getOpt1. + +48 +00:01:53,000 --> 00:01:56,000 +And likewise, I want to do this for four options. + +49 +00:01:56,000 --> 00:01:58,000 +I can simply, you know, creating array will make + +50 +00:01:58,000 --> 00:02:01,000 +much more sense for the options. + +51 +00:02:01,000 --> 00:02:03,000 +We can use inner loop to print all the options, right? + +52 +00:02:03,000 --> 00:02:04,000 +You can do that. + +53 +00:02:04,000 --> 00:02:07,000 +Maybe that is one of your assignment. + +54 +00:02:07,000 --> 00:02:10,000 +You can try it out. So three and four, okay? + +55 +00:02:10,000 --> 00:02:13,000 +So what we are displaying is we are displaying a question. + +56 +00:02:13,000 --> 00:02:15,000 +I mean we are displaying the question ID here, + +57 +00:02:15,000 --> 00:02:17,000 +then the actual question and the four options. + +58 +00:02:17,000 --> 00:02:20,000 +And after this we need to display, okay? + +59 +00:02:20,000 --> 00:02:23,000 +Make sure that you are not displaying the answer. + +60 +00:02:23,000 --> 00:02:25,000 +So let's keep that, because here if you can see the earlier + +61 +00:02:25,000 --> 00:02:27,000 +output it was displaying the answer as well. + +62 +00:02:27,000 --> 00:02:29,000 +Don't display the answers. + +63 +00:02:29,000 --> 00:02:31,000 +But after showing this question, + +64 +00:02:31,000 --> 00:02:33,000 +I want to take the user input. + +65 +00:02:33,000 --> 00:02:34,000 +Now, how do you take user input? + +66 +00:02:34,000 --> 00:02:36,000 +So we can use something called scanner. + +67 +00:02:36,000 --> 00:02:39,000 +So I can say scanner sc = new scanner. + +68 +00:02:40,000 --> 00:02:43,000 +And in this scanner you can pass two things, + +69 +00:02:43,000 --> 00:02:44,000 +not two things, one. + +70 +00:02:44,000 --> 00:02:46,000 +You can pass System.in. + +71 +00:02:46,000 --> 00:02:48,000 +So basically what you're doing is you're saying, + +72 +00:02:48,000 --> 00:02:51,000 +"Hey, I want to get the input from the user," + +73 +00:02:51,000 --> 00:02:53,000 +and the input will be coming from the input + +74 +00:02:53,000 --> 00:02:55,000 +source, which is System.in. + +75 +00:02:55,000 --> 00:02:56,000 +So the way we are doing it here, right, + +76 +00:02:56,000 --> 00:02:58,000 +we are saying System.out, which is for the output. + +77 +00:02:58,000 --> 00:03:01,000 +System.in is for the input. + +78 +00:03:01,000 --> 00:03:04,000 +And scanner helps you to get the value + +79 +00:03:04,000 --> 00:03:06,000 +because scanner had a lot of methods to use. + +80 +00:03:06,000 --> 00:03:07,000 +And one thing to remember, + +81 +00:03:07,000 --> 00:03:09,000 +whenever we use scanner, we have to import a package, + +82 +00:03:09,000 --> 00:03:13,000 +which is java.util.scanner because it's a inbuilt class. + +83 +00:03:13,000 --> 00:03:16,000 +And if you want to use it, you have to just import it, okay? + +84 +00:03:16,000 --> 00:03:19,000 +Otherwise, in your IDE, if you complete the word scanner, + +85 +00:03:19,000 --> 00:03:23,000 +say Ctrl + space, it'll give you the input option. + +86 +00:03:23,000 --> 00:03:25,000 +Okay once you've got this scanner, + +87 +00:03:25,000 --> 00:03:26,000 +now we can take the input. + +88 +00:03:26,000 --> 00:03:30,000 +The way you can take the input is sc.nextLine. + +89 +00:03:30,000 --> 00:03:31,000 +So with the help of next line, whatever, + +90 +00:03:31,000 --> 00:03:35,000 +a user has written on the console, it'll take that + +91 +00:03:35,000 --> 00:03:38,000 +as a input and you can store that somewhere now. + +92 +00:03:38,000 --> 00:03:39,000 +Where you want to store it, + +93 +00:03:39,000 --> 00:03:41,000 +maybe I will use a variable here. + +94 +00:03:41,000 --> 00:03:44,000 +Let's say selection, the user selection, + +95 +00:03:44,000 --> 00:03:47,000 +and then, okay, I need to create this variable somewhere. + +96 +00:03:47,000 --> 00:03:49,000 +We'll do that later, but I will take the input + +97 +00:03:49,000 --> 00:03:52,000 +and store it in a selection as a variable. + +98 +00:03:52,000 --> 00:03:54,000 +So whatever user selected. + +99 +00:03:54,000 --> 00:03:58,000 +But the problem here is this loop will run for five times, + +100 +00:03:58,000 --> 00:04:01,000 +and every time you do this, it'll change the value, right? + +101 +00:04:01,000 --> 00:04:02,000 +It'll keep changing the value + +102 +00:04:02,000 --> 00:04:05,000 +with the new value which a user inserted. + +103 +00:04:05,000 --> 00:04:06,000 +So instead of creating one variable, + +104 +00:04:06,000 --> 00:04:08,000 +it should be an array itself. + +105 +00:04:08,000 --> 00:04:13,000 +So I can say string selection, + +106 +00:04:13,000 --> 00:04:16,000 +and this will be an array of five values, right? + +107 +00:04:16,000 --> 00:04:20,000 +So it'll be five, new string five, okay? + +108 +00:04:20,000 --> 00:04:24,000 +So we got an array of five values here as you can see. + +109 +00:04:26,000 --> 00:04:30,000 +And if I go back now, instead of storing it in selection, + +110 +00:04:30,000 --> 00:04:33,000 +we should store in a particular index. + +111 +00:04:33,000 --> 00:04:34,000 +Now, how do we know the index? + +112 +00:04:34,000 --> 00:04:36,000 +The problem is we are using an enhanced forloop. + +113 +00:04:36,000 --> 00:04:39,000 +That's why we don't have an I. + +114 +00:04:39,000 --> 00:04:43,000 +So what I can do is I can say int i=0 initially, + +115 +00:04:43,000 --> 00:04:46,000 +and here I can simply say i++. + +116 +00:04:46,000 --> 00:04:48,000 +Either you can say i++ here, + +117 +00:04:48,000 --> 00:04:50,000 +or maybe you can just to keep it simple, I can say + +118 +00:04:50,000 --> 00:04:52,000 +i and then i++. + +119 +00:04:52,000 --> 00:04:55,000 +Because every time you run this loop, we have to make sure + +120 +00:04:55,000 --> 00:04:58,000 +that the new answer is going to the next index. + +121 +00:04:58,000 --> 00:05:00,000 +So whatever a user is entering + +122 +00:05:00,000 --> 00:05:02,000 +that is going into the selection. + +123 +00:05:02,000 --> 00:05:05,000 +And maybe I want to display the entire selection + +124 +00:05:05,000 --> 00:05:09,000 +what user has entered, just to see right + +125 +00:05:09,000 --> 00:05:11,000 +how things are working out. + +126 +00:05:11,000 --> 00:05:15,000 +So I can use a loop here, I can say string s from + +127 +00:05:16,000 --> 00:05:19,000 +selection, and I can print each s. + +128 +00:05:19,000 --> 00:05:23,000 +So it'll print all the user answers, what users entered. + +129 +00:05:23,000 --> 00:05:25,000 +I think this looks cool. + +130 +00:05:25,000 --> 00:05:27,000 +Let me just clear this screen + +131 +00:05:27,000 --> 00:05:30,000 +and compile the file, all the files, + +132 +00:05:30,000 --> 00:05:35,000 +basically whatever you have changed and run this. + +133 +00:05:35,000 --> 00:05:38,000 +And you can see it says, now you got the question right? + +134 +00:05:38,000 --> 00:05:41,000 +Question number one, size of int, + +135 +00:05:41,000 --> 00:05:43,000 +these are the four options you got. + +136 +00:05:43,000 --> 00:05:44,000 +You have to enter the right answer. + +137 +00:05:46,000 --> 00:05:48,000 +So let's say four in this case, enter. + +138 +00:05:48,000 --> 00:05:52,000 +Second question, size of double is eight character. + +139 +00:05:52,000 --> 00:05:54,000 +Let me give some wrong answer. + +140 +00:05:54,000 --> 00:05:58,000 +Long, just enter anything you want + +141 +00:05:58,000 --> 00:06:00,000 +because at this point we are just displaying it, right? + +142 +00:06:00,000 --> 00:06:05,000 +And at the end, you can see this is what user has entered. + +143 +00:06:05,000 --> 00:06:07,000 +So for the first question, user has entered four, + +144 +00:06:07,000 --> 00:06:11,000 +then eight, then eight, then two, then three, right? + +145 +00:06:11,000 --> 00:06:13,000 +So yeah, so till this point, we were able + +146 +00:06:13,000 --> 00:06:17,000 +to display the questions one by one and get the user input. + +147 +00:06:17,000 --> 00:06:21,000 +That's what we have done here. Cool. + +148 +00:06:21,000 --> 00:06:24,000 +But then how will you check if the user has + +149 +00:06:24,000 --> 00:06:26,000 +entered it correctly or not? + +150 +00:06:26,000 --> 00:06:28,000 +Or maybe you want to display this call. + +151 +00:06:28,000 --> 00:06:31,000 +How will you do that? Let's do that in the next video. + diff --git a/03 - Advance Java/004 Source-Code.url b/03 - Advance Java/004 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..4c695cf501825d447ed987531cc3845423d626b8 --- /dev/null +++ b/03 - Advance Java/004 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/Gaurav560/quiz-console-app \ No newline at end of file diff --git a/03 - Advance Java/005 Source-Code.url b/03 - Advance Java/005 Source-Code.url new file mode 100644 index 0000000000000000000000000000000000000000..4c695cf501825d447ed987531cc3845423d626b8 --- /dev/null +++ b/03 - Advance Java/005 Source-Code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/Gaurav560/quiz-console-app \ No newline at end of file diff --git a/03 - Advance Java/006 Abstract keyword_en.srt b/03 - Advance Java/006 Abstract keyword_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..2d51a7c7f395ddd915fc945e8b73880a119c9ec1 --- /dev/null +++ b/03 - Advance Java/006 Abstract keyword_en.srt @@ -0,0 +1,1304 @@ +1 +00:00:00,000 --> 00:00:03,000 +(light piano music) + +2 +00:00:03,000 --> 00:00:06,000 +-: In this video, let's talk about abstract keyword. + +3 +00:00:06,000 --> 00:00:07,000 +Now, what is abstract? + +4 +00:00:07,000 --> 00:00:09,000 +So before that, let's understand something, + +5 +00:00:09,000 --> 00:00:11,000 +so we know what is class, + +6 +00:00:11,000 --> 00:00:12,000 +what is object, what is inheritance, + +7 +00:00:12,000 --> 00:00:15,000 +and how do we do overriding of the methods, right? + +8 +00:00:15,000 --> 00:00:16,000 +Now, with those knowledge, + +9 +00:00:16,000 --> 00:00:18,000 +let's talk about abstract keyword. + +10 +00:00:18,000 --> 00:00:21,000 +So for that, what I will do is I will create a class here, + +11 +00:00:21,000 --> 00:00:24,000 +and let me name this class as let's say, a car. + +12 +00:00:24,000 --> 00:00:26,000 +Okay? Now, in this car you will define some methods. + +13 +00:00:26,000 --> 00:00:28,000 +Let's say, if we talk about car, + +14 +00:00:28,000 --> 00:00:32,000 +a car can drive, of course, right? + +15 +00:00:32,000 --> 00:00:35,000 +A car can play music as well, public void. + +16 +00:00:35,000 --> 00:00:37,000 +I mean, that's what we do, right? + +17 +00:00:37,000 --> 00:00:39,000 +We also play music in the car. + +18 +00:00:39,000 --> 00:00:40,000 +You know, some people, they don't start their car + +19 +00:00:40,000 --> 00:00:45,000 +before playing the music or they don't set it + +20 +00:00:45,000 --> 00:00:48,000 +on a drive mode before they play the music. + +21 +00:00:48,000 --> 00:00:49,000 +Okay, so we have these two methods, let's say, + +22 +00:00:49,000 --> 00:00:51,000 +or maybe we can add some more methods in the car. + +23 +00:00:51,000 --> 00:00:54,000 +Maybe you can sleep in your car and other methods. + +24 +00:00:54,000 --> 00:00:56,000 +But let's say, we have these two, + +25 +00:00:56,000 --> 00:00:58,000 +we have drive and we have playMusic. + +26 +00:00:58,000 --> 00:01:01,000 +And of course, in this method, I can define some statements. + +27 +00:01:01,000 --> 00:01:03,000 +Let's say, when I say it is playing music, + +28 +00:01:03,000 --> 00:01:06,000 +I can say, play music or we can write multiple statements. + +29 +00:01:06,000 --> 00:01:08,000 +Logic is not important. The important thing is the concept. + +30 +00:01:08,000 --> 00:01:09,000 +Let's focus on that. + +31 +00:01:09,000 --> 00:01:12,000 +So let's say in the play music, we have this statement. + +32 +00:01:12,000 --> 00:01:15,000 +And in drive, let's say, at this point, I'm not sure I want + +33 +00:01:15,000 --> 00:01:18,000 +to build my own car, but I'm not sure how can I drive a car? + +34 +00:01:18,000 --> 00:01:20,000 +I know how to implement play music, + +35 +00:01:20,000 --> 00:01:22,000 +but I don't know how to set up the engine. + +36 +00:01:22,000 --> 00:01:23,000 +I'm manufacturing my own car. + +37 +00:01:23,000 --> 00:01:25,000 +So I don't know how to set up the engine, + +38 +00:01:25,000 --> 00:01:28,000 +how to set up the wheels, but a car should drive. + +39 +00:01:28,000 --> 00:01:31,000 +That's what I know and that's why we call it a car, right? + +40 +00:01:31,000 --> 00:01:32,000 +If you don't have a drive feature, + +41 +00:01:32,000 --> 00:01:34,000 +then why you will call it a car? + +42 +00:01:34,000 --> 00:01:36,000 +You can state it's a home theater. + +43 +00:01:36,000 --> 00:01:39,000 +Anyway, now, if I want to use this car, + +44 +00:01:39,000 --> 00:01:41,000 +I can simply create an object. + +45 +00:01:41,000 --> 00:01:44,000 +I can say, a car obj = new Car. + +46 +00:01:44,000 --> 00:01:45,000 +And then I can call the methods. + +47 +00:01:45,000 --> 00:01:50,000 +I can call both the methods, obj.drive and obj.playMusic. + +48 +00:01:52,000 --> 00:01:53,000 +Of course, we have two methods, + +49 +00:01:53,000 --> 00:01:55,000 +I should be able to call them. + +50 +00:01:55,000 --> 00:01:56,000 +Now, if you observe, in the drive, + +51 +00:01:56,000 --> 00:01:58,000 +we are not doing anything. Why we are not doing anything? + +52 +00:01:58,000 --> 00:02:01,000 +Because I don't know how to implement drive, + +53 +00:02:01,000 --> 00:02:02,000 +but I know how to implement playMusic. + +54 +00:02:02,000 --> 00:02:04,000 +So I'm able to create this object, + +55 +00:02:04,000 --> 00:02:06,000 +and I'm able to call these two methods + +56 +00:02:06,000 --> 00:02:08,000 +and I think it should work. + +57 +00:02:08,000 --> 00:02:10,000 +And if I go back and run, it works. + +58 +00:02:10,000 --> 00:02:12,000 +It's not like if you don't define something, + +59 +00:02:12,000 --> 00:02:13,000 +it will not work. + +60 +00:02:13,000 --> 00:02:14,000 +It's working. + +61 +00:02:14,000 --> 00:02:17,000 +The only thing is when you're defining this method, + +62 +00:02:17,000 --> 00:02:20,000 +you are actually not implementing anything here. + +63 +00:02:20,000 --> 00:02:21,000 +You're not writing any statements. + +64 +00:02:21,000 --> 00:02:23,000 +So it's valid in Javas and text term, + +65 +00:02:23,000 --> 00:02:25,000 +but as a feature, I mean, + +66 +00:02:25,000 --> 00:02:26,000 +just imagine you're buying a new phone, + +67 +00:02:26,000 --> 00:02:29,000 +and your phone says it can do this feature, this feature, + +68 +00:02:29,000 --> 00:02:31,000 +feature one, feature two, feature three, + +69 +00:02:31,000 --> 00:02:32,000 +but then none of the feature works. + +70 +00:02:32,000 --> 00:02:35,000 +Of course, you will not buy any phone like that, right? + +71 +00:02:35,000 --> 00:02:37,000 +In the same way, if we talk about this car, + +72 +00:02:37,000 --> 00:02:38,000 +you are writing a method over here, + +73 +00:02:38,000 --> 00:02:41,000 +which is drive when you're not implementing it. + +74 +00:02:41,000 --> 00:02:43,000 +Why someone should be able to create object of this? + +75 +00:02:43,000 --> 00:02:45,000 +Why in first place, + +76 +00:02:45,000 --> 00:02:47,000 +someone should create a class itself, right? + +77 +00:02:47,000 --> 00:02:49,000 +When you don't know how to implement it. + +78 +00:02:49,000 --> 00:02:51,000 +See, the thing is when you build an application, + +79 +00:02:51,000 --> 00:02:53,000 +you have a structure which you follow. + +80 +00:02:53,000 --> 00:02:55,000 +You design your application. + +81 +00:02:55,000 --> 00:02:56,000 +I'm not talking about the thing + +82 +00:02:56,000 --> 00:02:57,000 +which you see on the screen. + +83 +00:02:57,000 --> 00:02:59,000 +I'm talking about when you build an application, + +84 +00:02:59,000 --> 00:03:00,000 +you design on paper. + +85 +00:03:00,000 --> 00:03:01,000 +What are the classes you need? + +86 +00:03:01,000 --> 00:03:03,000 +What are the methods you need? + +87 +00:03:03,000 --> 00:03:06,000 +Now, at this point, you know how to play music, + +88 +00:03:06,000 --> 00:03:08,000 +but you don't know how to drive. + +89 +00:03:08,000 --> 00:03:11,000 +Now, in that case, should I remove this method completely? + +90 +00:03:11,000 --> 00:03:14,000 +If I do that, then how will you call it as a car? + +91 +00:03:14,000 --> 00:03:15,000 +So what you can do is you can say, + +92 +00:03:15,000 --> 00:03:18,000 +hey, you know, I'm creating this method drive, + +93 +00:03:18,000 --> 00:03:22,000 +but I'm not sure what should I implement it here? + +94 +00:03:22,000 --> 00:03:24,000 +So instead of defining it here, + +95 +00:03:24,000 --> 00:03:25,000 +so when you open the curly brackets and close, + +96 +00:03:25,000 --> 00:03:27,000 +you are basically defining a method. + +97 +00:03:27,000 --> 00:03:29,000 +So instead of defining a method, + +98 +00:03:29,000 --> 00:03:31,000 +you can basically declare a method, + +99 +00:03:31,000 --> 00:03:34,000 +which means you can put a semicolon at the end. + +100 +00:03:34,000 --> 00:03:37,000 +When you do that, you are declaring a method. + +101 +00:03:37,000 --> 00:03:40,000 +That also means that the drive feature you had declaring + +102 +00:03:40,000 --> 00:03:42,000 +but it is not working and no one should be able + +103 +00:03:42,000 --> 00:03:44,000 +to create object of it, okay? + +104 +00:03:44,000 --> 00:03:45,000 +Again, we'll see that in some time, + +105 +00:03:45,000 --> 00:03:48,000 +but when I declare it, it is giving you an error. + +106 +00:03:48,000 --> 00:03:49,000 +Now, first of all, why you will declare it + +107 +00:03:49,000 --> 00:03:51,000 +when you are not defining anything? + +108 +00:03:51,000 --> 00:03:55,000 +Maybe I have this mindset, someone else can just come back, + +109 +00:03:55,000 --> 00:03:57,000 +and they can say they can implement a car. + +110 +00:03:57,000 --> 00:04:01,000 +Maybe they will build a wagon, let's say, + +111 +00:04:01,000 --> 00:04:04,000 +and a wagon will extend my car + +112 +00:04:04,000 --> 00:04:06,000 +and it is the wagon's responsibility. + +113 +00:04:06,000 --> 00:04:08,000 +In fact, you know, instead of saying wagon, + +114 +00:04:08,000 --> 00:04:09,000 +let's say, WagonR. + +115 +00:04:09,000 --> 00:04:11,000 +It's a very famous car if you search online. + +116 +00:04:12,000 --> 00:04:16,000 +So we have this WagonR car which some of them don't like, + +117 +00:04:16,000 --> 00:04:18,000 +not a big fan of it, but okay, + +118 +00:04:19,000 --> 00:04:21,000 +so let's say we have this car WagonR extends Car. + +119 +00:04:21,000 --> 00:04:23,000 +In fact, I have a car which is smaller + +120 +00:04:23,000 --> 00:04:26,000 +than WagonR which I love. + +121 +00:04:26,000 --> 00:04:29,000 +So we have this class, WagonR extends Car here. + +122 +00:04:29,000 --> 00:04:32,000 +And what I want here is I want this WagonR + +123 +00:04:32,000 --> 00:04:34,000 +to implement the drive feature. + +124 +00:04:34,000 --> 00:04:37,000 +See, if you think car is just an abstract concept, right? + +125 +00:04:37,000 --> 00:04:38,000 +No one actually buys a car. + +126 +00:04:38,000 --> 00:04:43,000 +What you buy is a real car, like WagonR or BMW GLS + +127 +00:04:43,000 --> 00:04:47,000 +or Toyota Fortuner or Ford EcoSport, right? + +128 +00:04:47,000 --> 00:04:49,000 +So you buy a real car. You don't buy a car. + +129 +00:04:49,000 --> 00:04:51,000 +This is just a concept. + +130 +00:04:51,000 --> 00:04:52,000 +So when I say drive here, + +131 +00:04:52,000 --> 00:04:53,000 +a car don't know how to drive, + +132 +00:04:53,000 --> 00:04:56,000 +but then when you create a real car, + +133 +00:04:56,000 --> 00:04:58,000 +that car will know how to drive. + +134 +00:04:58,000 --> 00:05:01,000 +So I want this WagonR to be responsible + +135 +00:05:01,000 --> 00:05:02,000 +to define this method. + +136 +00:05:02,000 --> 00:05:05,000 +Okay, so, I can just copy this and paste it here. + +137 +00:05:05,000 --> 00:05:07,000 +And here, instead of declaring it, I will define. + +138 +00:05:07,000 --> 00:05:11,000 +So what I want is I want this WagonR to implement it. + +139 +00:05:11,000 --> 00:05:13,000 +Of course, the other feature like play music + +140 +00:05:13,000 --> 00:05:16,000 +can simply be extended, but not drive. + +141 +00:05:16,000 --> 00:05:18,000 +Drive should be implemented here. + +142 +00:05:18,000 --> 00:05:22,000 +And WagonR, I mean the company who built WagonR cars, + +143 +00:05:22,000 --> 00:05:26,000 +it's their responsibility to define the method here. + +144 +00:05:26,000 --> 00:05:27,000 +I can say driving, of course, + +145 +00:05:27,000 --> 00:05:29,000 +they will write a complex logic, + +146 +00:05:29,000 --> 00:05:31,000 +but just to make it simple, I'm writing one statement. + +147 +00:05:31,000 --> 00:05:34,000 +So they're responsible to define this method, + +148 +00:05:34,000 --> 00:05:35,000 +and we're not doing it here. + +149 +00:05:35,000 --> 00:05:37,000 +Now, if you want to declare a method, + +150 +00:05:37,000 --> 00:05:39,000 +you can see we are getting an error here, + +151 +00:05:39,000 --> 00:05:41,000 +you can use an abstract keyword. + +152 +00:05:41,000 --> 00:05:42,000 +So when you say abstract, + +153 +00:05:42,000 --> 00:05:46,000 +it means I'm just giving you the abstract idea + +154 +00:05:46,000 --> 00:05:48,000 +of a car should have a drive feature, + +155 +00:05:48,000 --> 00:05:50,000 +but I don't know how to implement it. + +156 +00:05:50,000 --> 00:05:52,000 +Now, when you are using my feature + +157 +00:05:52,000 --> 00:05:55,000 +and when you call your vehicle as a car, + +158 +00:05:55,000 --> 00:05:57,000 +it should have a drive feature. + +159 +00:05:57,000 --> 00:05:59,000 +Or whatever object you're creating is a car, + +160 +00:05:59,000 --> 00:06:01,000 +you need to have a drive feature, + +161 +00:06:01,000 --> 00:06:02,000 +and that's how you make it. + +162 +00:06:02,000 --> 00:06:05,000 +So you make a public abstract void drive. + +163 +00:06:05,000 --> 00:06:07,000 +So you have to use an abstract keyboard + +164 +00:06:07,000 --> 00:06:09,000 +if you want to declare a method, okay? + +165 +00:06:09,000 --> 00:06:11,000 +Here we are defining it, + +166 +00:06:11,000 --> 00:06:13,000 +but if you can see, even if I do abstract, + +167 +00:06:13,000 --> 00:06:15,000 +it is giving you an error. + +168 +00:06:15,000 --> 00:06:18,000 +It says, the abstract method drive in type Car + +169 +00:06:18,000 --> 00:06:20,000 +can only be defined by an abstract class. + +170 +00:06:20,000 --> 00:06:22,000 +Okay, what it simply means is + +171 +00:06:22,000 --> 00:06:24,000 +if you have an abstract method, + +172 +00:06:24,000 --> 00:06:27,000 +abstract method can belong to only abstract class. + +173 +00:06:27,000 --> 00:06:29,000 +So that means you have to make this class + +174 +00:06:29,000 --> 00:06:30,000 +as abstract as well. + +175 +00:06:30,000 --> 00:06:34,000 +So you can have abstract methods only in the abstract class + +176 +00:06:34,000 --> 00:06:38,000 +and whichever class is extending the abstract class + +177 +00:06:38,000 --> 00:06:39,000 +will have this drive method. + +178 +00:06:39,000 --> 00:06:41,000 +If we don't do that, it'll give you error. + +179 +00:06:41,000 --> 00:06:43,000 +Example, if I change the method name, + +180 +00:06:43,000 --> 00:06:44,000 +you can see it will give you error. + +181 +00:06:44,000 --> 00:06:47,000 +It says, the type WagonR must implement + +182 +00:06:47,000 --> 00:06:50,000 +the inherited abstract method Car.drive. + +183 +00:06:50,000 --> 00:06:53,000 +So if you are extending an abstract class, + +184 +00:06:53,000 --> 00:06:55,000 +you need to define it, compulsory, + +185 +00:06:55,000 --> 00:06:56,000 +otherwise, it will not work. + +186 +00:06:56,000 --> 00:06:57,000 +So you have to define, + +187 +00:06:57,000 --> 00:06:59,000 +you have to basically override the method. + +188 +00:06:59,000 --> 00:07:01,000 +Now, when someone creates an object of a car, + +189 +00:07:01,000 --> 00:07:04,000 +in fact, first of all, you can't create the object + +190 +00:07:04,000 --> 00:07:06,000 +of abstract class, very important point. + +191 +00:07:06,000 --> 00:07:08,000 +And that's why you can see we got a error here. + +192 +00:07:08,000 --> 00:07:11,000 +It says, cannot instantiate the type Car. + +193 +00:07:11,000 --> 00:07:14,000 +It simply means you cannot create object of car. + +194 +00:07:14,000 --> 00:07:16,000 +So the point, remember? + +195 +00:07:16,000 --> 00:07:18,000 +You cannot create object of an abstract class. + +196 +00:07:18,000 --> 00:07:21,000 +Okay, but then, can I create a reference of abstract class? + +197 +00:07:21,000 --> 00:07:22,000 +Yes, that is possible. + +198 +00:07:22,000 --> 00:07:26,000 +You can say Car obj, that is absolutely correct, + +199 +00:07:26,000 --> 00:07:27,000 +but if you want to create an object, + +200 +00:07:27,000 --> 00:07:31,000 +you have to create object of not car, but WagonR, okay? + +201 +00:07:31,000 --> 00:07:34,000 +And then with this obj, you can call drive, + +202 +00:07:34,000 --> 00:07:36,000 +we can call playMusic. + +203 +00:07:36,000 --> 00:07:38,000 +And we have talked about dynamic method dispatch. + +204 +00:07:38,000 --> 00:07:39,000 +And we know when you call drive, + +205 +00:07:39,000 --> 00:07:42,000 +it will be calling drive of WagonR, not of car. + +206 +00:07:42,000 --> 00:07:45,000 +Anyway, it is declared, right? + +207 +00:07:45,000 --> 00:07:47,000 +So basically, that's how you can create an abstract class. + +208 +00:07:47,000 --> 00:07:49,000 +That's how you can create an abstract method. + +209 +00:07:49,000 --> 00:07:51,000 +And basically, that's how you can restrict + +210 +00:07:51,000 --> 00:07:53,000 +the creation of the object here. + +211 +00:07:53,000 --> 00:07:54,000 +Now, a few more points to remember. + +212 +00:07:54,000 --> 00:07:57,000 +Is it compulsory to have an abstract method + +213 +00:07:57,000 --> 00:07:58,000 +in the abstract class? + +214 +00:07:58,000 --> 00:08:00,000 +I mean, we know that it is compulsory + +215 +00:08:00,000 --> 00:08:01,000 +to have the abstract method. + +216 +00:08:01,000 --> 00:08:03,000 +You need abstract class, right? + +217 +00:08:03,000 --> 00:08:05,000 +But is it compulsory to have + +218 +00:08:05,000 --> 00:08:06,000 +a abstract method in the abstract class? + +219 +00:08:06,000 --> 00:08:07,000 +Let's do that. + +220 +00:08:07,000 --> 00:08:10,000 +If you come in this section, okay? + +221 +00:08:10,000 --> 00:08:11,000 +You can see there's no problem. + +222 +00:08:11,000 --> 00:08:15,000 +It's not like a abstract class should have abstract method. + +223 +00:08:15,000 --> 00:08:17,000 +Abstract class can have abstract methods, + +224 +00:08:17,000 --> 00:08:19,000 +it can have normal methods. + +225 +00:08:19,000 --> 00:08:20,000 +So you can have an abstract class + +226 +00:08:20,000 --> 00:08:21,000 +with which only has abstract methods. + +227 +00:08:21,000 --> 00:08:23,000 +You can have abstract class + +228 +00:08:23,000 --> 00:08:27,000 +which has only implemented methods, which are this, + +229 +00:08:27,000 --> 00:08:29,000 +which are non-abstract methods. + +230 +00:08:29,000 --> 00:08:31,000 +And you can have a abstract class which can have both, + +231 +00:08:31,000 --> 00:08:34,000 +normal methods as well as the abstract method. + +232 +00:08:34,000 --> 00:08:36,000 +And you can create multiple abstract method, okay? + +233 +00:08:36,000 --> 00:08:38,000 +Example, if I can come back here + +234 +00:08:38,000 --> 00:08:41,000 +and say, hey, you know, I want a car to fly. + +235 +00:08:41,000 --> 00:08:44,000 +I dunno when this is possible, + +236 +00:08:44,000 --> 00:08:45,000 +but in the movies you see that, right? + +237 +00:08:45,000 --> 00:08:49,000 +So a car jumps from one building to other building, okay? + +238 +00:08:49,000 --> 00:08:50,000 +And I hope you know the movie name, + +239 +00:08:50,000 --> 00:08:53,000 +but anyway, so if you talk about this abstract method, + +240 +00:08:53,000 --> 00:08:55,000 +which is fly, and you can see I'm declaring it here. + +241 +00:08:55,000 --> 00:08:57,000 +Now, when you declare a method + +242 +00:08:57,000 --> 00:08:59,000 +and when you say you have an abstract class + +243 +00:08:59,000 --> 00:09:01,000 +which is getting implemented + +244 +00:09:01,000 --> 00:09:04,000 +or which is getting extended by another class, + +245 +00:09:04,000 --> 00:09:05,000 +it becomes compulsory for you + +246 +00:09:05,000 --> 00:09:07,000 +to define one more method here. + +247 +00:09:07,000 --> 00:09:09,000 +So you can say public, not abstract, + +248 +00:09:09,000 --> 00:09:11,000 +you can say public void. + +249 +00:09:11,000 --> 00:09:14,000 +You have to define fly as well, otherwise, it'll not work. + +250 +00:09:14,000 --> 00:09:17,000 +So you can say flying, okay? + +251 +00:09:17,000 --> 00:09:19,000 +And now, with this you can call the methods. + +252 +00:09:19,000 --> 00:09:24,000 +You can say obj.fly and this should work, compile, run. + +253 +00:09:25,000 --> 00:09:28,000 +And you can see it says Driving, play music, and Flying. + +254 +00:09:28,000 --> 00:09:30,000 +Now, there's one important thing here, + +255 +00:09:30,000 --> 00:09:33,000 +which is what if when you are creating + +256 +00:09:33,000 --> 00:09:35,000 +this class WagonR and then you are not able + +257 +00:09:35,000 --> 00:09:37,000 +to implement fly, what will happen? + +258 +00:09:37,000 --> 00:09:38,000 +First of all, it's compulsory + +259 +00:09:38,000 --> 00:09:40,000 +to define all the methods, okay? + +260 +00:09:40,000 --> 00:09:42,000 +All the abstract methods. + +261 +00:09:42,000 --> 00:09:45,000 +So you can see in this class, we have three methods, + +262 +00:09:45,000 --> 00:09:47,000 +but only two methods are abstract. + +263 +00:09:47,000 --> 00:09:48,000 +And that's why it says, + +264 +00:09:48,000 --> 00:09:50,000 +it should define fly as well as drive, + +265 +00:09:50,000 --> 00:09:51,000 +but we are not implementing fly. + +266 +00:09:51,000 --> 00:09:53,000 +So you can see it says, the type WagonR + +267 +00:09:53,000 --> 00:09:57,000 +must implement the inherited abstract method, Car.fly. + +268 +00:09:57,000 --> 00:09:59,000 +But what if you don't do that? + +269 +00:09:59,000 --> 00:10:02,000 +So in that case, if you have a class which is not able + +270 +00:10:02,000 --> 00:10:05,000 +to implement all the methods, in this case, + +271 +00:10:05,000 --> 00:10:07,000 +even this class becomes abstract class. + +272 +00:10:07,000 --> 00:10:09,000 +So you have to make it abstract class, + +273 +00:10:09,000 --> 00:10:11,000 +and you can see there's no error. + +274 +00:10:11,000 --> 00:10:14,000 +But the problem now is you can't create object + +275 +00:10:14,000 --> 00:10:15,000 +of abstract class, right? + +276 +00:10:15,000 --> 00:10:18,000 +So maybe you have to create a new class here by saying, + +277 +00:10:19,000 --> 00:10:24,000 +UpdatedWagonR which can also fly and this will extend. + +278 +00:10:24,000 --> 00:10:25,000 +Now, which class you want me to extend, + +279 +00:10:25,000 --> 00:10:27,000 +the car or the WagonR? + +280 +00:10:27,000 --> 00:10:28,000 +Of course, Wagoner, right? + +281 +00:10:28,000 --> 00:10:31,000 +Because you will get this dry feature from WagonR. + +282 +00:10:31,000 --> 00:10:34,000 +So you can simply extend it with WagonR. + +283 +00:10:34,000 --> 00:10:36,000 +And then here, in this class, + +284 +00:10:36,000 --> 00:10:38,000 +you can basically create object of fly. + +285 +00:10:38,000 --> 00:10:40,000 +In fact, if you are using IDE, + +286 +00:10:40,000 --> 00:10:41,000 +and if you don't, if you're lazy to type, + +287 +00:10:41,000 --> 00:10:45,000 +you can come to this error, and you can say Quick Fix, + +288 +00:10:45,000 --> 00:10:48,000 +add unimplemented methods, and it will do it for you. + +289 +00:10:48,000 --> 00:10:52,000 +Okay? And here, we can say Flying. So even you can do that. + +290 +00:10:52,000 --> 00:10:55,000 +The only thing is now the object will not be of WagonR. + +291 +00:10:55,000 --> 00:10:58,000 +The object will be of UpdatedWagonR. + +292 +00:10:59,000 --> 00:11:00,000 +Is it the method class name? + +293 +00:11:00,000 --> 00:11:03,000 +Okay, it should be updated, right? + +294 +00:11:03,000 --> 00:11:05,000 +So that's how you can create a object. + +295 +00:11:05,000 --> 00:11:07,000 +So again, we are not creating object of abstract class. + +296 +00:11:07,000 --> 00:11:09,000 +We are creating object of the class + +297 +00:11:09,000 --> 00:11:11,000 +which is not abstract. + +298 +00:11:11,000 --> 00:11:12,000 +Okay, now, since we are calling + +299 +00:11:12,000 --> 00:11:14,000 +this class as abstract class, + +300 +00:11:14,000 --> 00:11:15,000 +even this class should have a name, right? + +301 +00:11:15,000 --> 00:11:18,000 +So normally, we call them as a concrete class. + +302 +00:11:18,000 --> 00:11:22,000 +So we can say this is a concrete class. + +303 +00:11:22,000 --> 00:11:23,000 +Okay, so we have abstract class + +304 +00:11:23,000 --> 00:11:24,000 +and then we have concrete class. + +305 +00:11:24,000 --> 00:11:27,000 +So you can create object of a concrete class, + +306 +00:11:27,000 --> 00:11:29,000 +not an abstract class. + +307 +00:11:29,000 --> 00:11:32,000 +So I hope this makes sense. That's about abstract keyword. + +308 +00:11:32,000 --> 00:11:34,000 +So let me just reiterate. + +309 +00:11:34,000 --> 00:11:35,000 +Abstract keyword can be used + +310 +00:11:35,000 --> 00:11:37,000 +with methods if you want to declare them. + +311 +00:11:37,000 --> 00:11:39,000 +And if you have any abstract methods, + +312 +00:11:39,000 --> 00:11:41,000 +you have to make a class as abstract. + +313 +00:11:41,000 --> 00:11:43,000 +But again, on the other hand, if you have abstract class, + +314 +00:11:43,000 --> 00:11:46,000 +there's no compulsion that abstract class + +315 +00:11:46,000 --> 00:11:47,000 +should have abstract methods. + +316 +00:11:47,000 --> 00:11:50,000 +Abstract class can have normal methods also. + +317 +00:11:50,000 --> 00:11:51,000 +I mean, all these normal methods. + +318 +00:11:51,000 --> 00:11:54,000 +No abstract methods. Even that is possible. + +319 +00:11:54,000 --> 00:11:56,000 +Next point, remember, we cannot create object + +320 +00:11:56,000 --> 00:11:57,000 +of an abstract class. + +321 +00:11:57,000 --> 00:12:00,000 +If you want to create object, you have to get a class, + +322 +00:12:00,000 --> 00:12:02,000 +which implements all the methods, + +323 +00:12:02,000 --> 00:12:04,000 +a subclass which implements all the methods. + +324 +00:12:04,000 --> 00:12:06,000 +And then that class will be called as a concrete class, + +325 +00:12:06,000 --> 00:12:08,000 +and you can create object of it. + +326 +00:12:08,000 --> 00:12:09,000 +So that's about abstract keyword. + diff --git a/03 - Advance Java/006 Source-code.url b/03 - Advance Java/006 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/03 - Advance Java/006 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/03 - Advance Java/007 Inner class_en.srt b/03 - Advance Java/007 Inner class_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..de7d610afb790f41250f908114ebd2678c75de0a --- /dev/null +++ b/03 - Advance Java/007 Inner class_en.srt @@ -0,0 +1,588 @@ +1 +00:00:03,000 --> 00:00:05,000 +-: In this video we'll talk about inner class. + +2 +00:00:05,000 --> 00:00:07,000 +That's right, a class inside a class. + +3 +00:00:07,000 --> 00:00:08,000 +I know that sounds weird at first, + +4 +00:00:08,000 --> 00:00:11,000 +but let me go with that. + +5 +00:00:11,000 --> 00:00:13,000 +See, most of the time when you build an application, + +6 +00:00:13,000 --> 00:00:16,000 +you might have a scenario where + +7 +00:00:16,000 --> 00:00:20,000 +a class will be used only for that particular purpose, okay? + +8 +00:00:20,000 --> 00:00:22,000 +So let's say you have a class A + +9 +00:00:22,000 --> 00:00:24,000 +and then that class needs variables + +10 +00:00:24,000 --> 00:00:25,000 +so we can do that. + +11 +00:00:25,000 --> 00:00:27,000 +Example, we have a class A here. + +12 +00:00:27,000 --> 00:00:28,000 +So when you have this class A, + +13 +00:00:28,000 --> 00:00:29,000 +this class can have variables, + +14 +00:00:29,000 --> 00:00:31,000 +this class can have methods, right? + +15 +00:00:31,000 --> 00:00:34,000 +Now, these variables and methods only work for this class A. + +16 +00:00:34,000 --> 00:00:36,000 +So if you want to access them, + +17 +00:00:36,000 --> 00:00:39,000 +you have to get A class first. + +18 +00:00:39,000 --> 00:00:40,000 +In the same way let's say if you have a class + +19 +00:00:40,000 --> 00:00:44,000 +where the class is totally dependent on class A, + +20 +00:00:44,000 --> 00:00:46,000 +there's no other need for it. + +21 +00:00:46,000 --> 00:00:50,000 +In that case, you can create a class inside the class, okay? + +22 +00:00:50,000 --> 00:00:52,000 +The way you do that is by defining a class like A, + +23 +00:00:52,000 --> 00:00:56,000 +and then you can define a class called B inside it. + +24 +00:00:56,000 --> 00:00:58,000 +Now, just the way you have class A here, + +25 +00:00:58,000 --> 00:01:00,000 +I can create variable that works, + +26 +00:01:00,000 --> 00:01:03,000 +we can say variable age, + +27 +00:01:03,000 --> 00:01:05,000 +and then we can have method as well, + +28 +00:01:05,000 --> 00:01:09,000 +which is public void show. + +29 +00:01:09,000 --> 00:01:10,000 +Now we have these two things, + +30 +00:01:10,000 --> 00:01:12,000 +we have a variable, which is age, + +31 +00:01:12,000 --> 00:01:13,000 +and then we have a method which is show, + +32 +00:01:13,000 --> 00:01:15,000 +and then we have a class as well. + +33 +00:01:15,000 --> 00:01:17,000 +So imagine a class having all three things, + +34 +00:01:17,000 --> 00:01:20,000 +a variable, a method, and a class. + +35 +00:01:20,000 --> 00:01:22,000 +And inside this class, let's say I create a method, + +36 +00:01:22,000 --> 00:01:24,000 +which is public void config. + +37 +00:01:24,000 --> 00:01:27,000 +And in this config, I will print something, + +38 +00:01:27,000 --> 00:01:29,000 +let's say in config, + +39 +00:01:29,000 --> 00:01:31,000 +just to print something, okay? + +40 +00:01:31,000 --> 00:01:34,000 +And I'm trying to make this example as simple as possible, + +41 +00:01:34,000 --> 00:01:35,000 +that's why I'm using this class name. + +42 +00:01:35,000 --> 00:01:36,000 +In real life, don't do that. + +43 +00:01:36,000 --> 00:01:39,000 +Make sure that you use proper class names. + +44 +00:01:39,000 --> 00:01:41,000 +And so let's say we have this scenario. + +45 +00:01:41,000 --> 00:01:44,000 +Now question is how will you call this config? + +46 +00:01:44,000 --> 00:01:45,000 +So it's very easy to call show. + +47 +00:01:45,000 --> 00:01:47,000 +In fact, I should print something here just to see + +48 +00:01:47,000 --> 00:01:51,000 +what it prints in show. + +49 +00:01:51,000 --> 00:01:53,000 +Now, question to you is how will you call this show? + +50 +00:01:53,000 --> 00:01:55,000 +And then we have seen this, right? + +51 +00:01:55,000 --> 00:01:58,000 +You simply have to create this object of A + +52 +00:01:58,000 --> 00:02:01,000 +and then you can call obj.show + +53 +00:02:03,000 --> 00:02:05,000 +I mean, show will perfectly work, right? + +54 +00:02:05,000 --> 00:02:08,000 +If I compile this code and run, you can see it says in show. + +55 +00:02:08,000 --> 00:02:11,000 +Now I want to call config. + +56 +00:02:11,000 --> 00:02:12,000 +How will you do that? + +57 +00:02:12,000 --> 00:02:14,000 +Of course, you will say if it is a class B, + +58 +00:02:14,000 --> 00:02:16,000 +then you have to get object of B to call this config. + +59 +00:02:16,000 --> 00:02:18,000 +Let's try. + +60 +00:02:18,000 --> 00:02:23,000 +If I try to create object of B by saying B obj1 new B, + +61 +00:02:24,000 --> 00:02:26,000 +you can see it'll give you an error. + +62 +00:02:26,000 --> 00:02:29,000 +It says B cannot be resolved. + +63 +00:02:29,000 --> 00:02:31,000 +Oh, it's not able to find B. + +64 +00:02:31,000 --> 00:02:34,000 +The thing is, B is not openly available, right? + +65 +00:02:34,000 --> 00:02:36,000 +B is a part of A. + +66 +00:02:36,000 --> 00:02:37,000 +So if you want to refer B here, + +67 +00:02:37,000 --> 00:02:39,000 +you need to say A.B. + +68 +00:02:39,000 --> 00:02:40,000 +So B belongs to A, + +69 +00:02:40,000 --> 00:02:42,000 +that's why you have to say this. + +70 +00:02:42,000 --> 00:02:44,000 +Okay, now while I'm doing this, + +71 +00:02:44,000 --> 00:02:46,000 +so when you say if you compile this code, + +72 +00:02:46,000 --> 00:02:48,000 +and if I go back here, + +73 +00:02:48,000 --> 00:02:49,000 +can you see that we got two classes + +74 +00:02:49,000 --> 00:02:50,000 +and we have seen this, right? + +75 +00:02:50,000 --> 00:02:52,000 +Every time you compile a code, + +76 +00:02:52,000 --> 00:02:55,000 +you will get those number of class files. + +77 +00:02:55,000 --> 00:02:56,000 +So you've got a classify for demo that makes sense. + +78 +00:02:56,000 --> 00:02:58,000 +You can see we have demo.class. + +79 +00:02:58,000 --> 00:03:03,000 +You will get A.class as well because A is a class, + +80 +00:03:03,000 --> 00:03:04,000 +but we also got, + +81 +00:03:04,000 --> 00:03:05,000 +B is also class, right? + +82 +00:03:05,000 --> 00:03:07,000 +It should have its own class file. + +83 +00:03:07,000 --> 00:03:08,000 +And that's right, we got it. + +84 +00:03:08,000 --> 00:03:10,000 +We got B.class. + +85 +00:03:10,000 --> 00:03:12,000 +The only thing is B belongs to A. + +86 +00:03:12,000 --> 00:03:15,000 +So the dollar symbol separates this two. + +87 +00:03:15,000 --> 00:03:17,000 +So it says B class belongs to B class + +88 +00:03:17,000 --> 00:03:20,000 +and that's why it's A dollar B. + +89 +00:03:20,000 --> 00:03:21,000 +Okay, that makes sense. + +90 +00:03:21,000 --> 00:03:25,000 +So if you want to access B, you have to say A.B. + +91 +00:03:25,000 --> 00:03:27,000 +That's how you access it and you can say obj1. + +92 +00:03:27,000 --> 00:03:30,000 +You can say there's no problem in declaring the variable. + +93 +00:03:30,000 --> 00:03:32,000 +The problem is when you say new, + +94 +00:03:32,000 --> 00:03:34,000 +now, how will you do it? + +95 +00:03:34,000 --> 00:03:36,000 +If you say B, it'll not work again. + +96 +00:03:36,000 --> 00:03:38,000 +It'll say cannot able to resolve B. + +97 +00:03:38,000 --> 00:03:41,000 +In that case, if you say A.B, + +98 +00:03:41,000 --> 00:03:43,000 +no, that's not the proper syntax. + +99 +00:03:43,000 --> 00:03:46,000 +The thing is, if you want to create object of B, + +100 +00:03:46,000 --> 00:03:49,000 +you need object of A first. + +101 +00:03:49,000 --> 00:03:50,000 +Now you will say why? + +102 +00:03:50,000 --> 00:03:51,000 +It's very simple. + +103 +00:03:51,000 --> 00:03:54,000 +If you want to call B, if you want to call this show, + +104 +00:03:54,000 --> 00:03:56,000 +now, show is a method, right? + +105 +00:03:56,000 --> 00:03:58,000 +It's a non-static method, + +106 +00:03:58,000 --> 00:04:02,000 +which means if you want to call show, you need object. + +107 +00:04:02,000 --> 00:04:05,000 +In the same way if you want to access this variable age, + +108 +00:04:05,000 --> 00:04:05,000 +you need object. + +109 +00:04:05,000 --> 00:04:09,000 +In the same way, if you want to create object of B, + +110 +00:04:09,000 --> 00:04:11,000 +you need object of A, + +111 +00:04:11,000 --> 00:04:14,000 +because that is a non non-static method. + +112 +00:04:14,000 --> 00:04:16,000 +Okay, so I'm focusing more on static here, right? + +113 +00:04:16,000 --> 00:04:18,000 +Can I create a static class? + +114 +00:04:18,000 --> 00:04:20,000 +Yes, we'll see that in some time, + +115 +00:04:20,000 --> 00:04:22,000 +but we got this object here. + +116 +00:04:22,000 --> 00:04:27,000 +And now using this obj1, I can call the config method. + +117 +00:04:27,000 --> 00:04:30,000 +If you want to create object of inner class, + +118 +00:04:30,000 --> 00:04:31,000 +which is be in this case, + +119 +00:04:31,000 --> 00:04:33,000 +you need to first specify where it belongs to. + +120 +00:04:33,000 --> 00:04:35,000 +So B belongs to A, + +121 +00:04:35,000 --> 00:04:37,000 +and then you need object of outer class, + +122 +00:04:37,000 --> 00:04:40,000 +which is obj here to create object of inner class. + +123 +00:04:40,000 --> 00:04:43,000 +And once you have that, you can simply call these methods + +124 +00:04:43,000 --> 00:04:44,000 +and that will properly work. + +125 +00:04:44,000 --> 00:04:47,000 +You can see what in show and in config. + +126 +00:04:47,000 --> 00:04:48,000 +Okay. + +127 +00:04:48,000 --> 00:04:50,000 +Now we have also focused on a static class, right? + +128 +00:04:50,000 --> 00:04:53,000 +You can also make this class as static. + +129 +00:04:53,000 --> 00:04:55,000 +And we all know when you refer it to a static class, + +130 +00:04:55,000 --> 00:04:57,000 +you don't need object. + +131 +00:04:57,000 --> 00:05:00,000 +So basically you don't call this with the help of object, + +132 +00:05:00,000 --> 00:05:02,000 +you can directly refer with A.B. + +133 +00:05:02,000 --> 00:05:05,000 +Now this will work only when the class is static, + +134 +00:05:05,000 --> 00:05:06,000 +the inner class is static. + +135 +00:05:06,000 --> 00:05:09,000 +It'll not work for non-static class. + +136 +00:05:09,000 --> 00:05:10,000 +Now can we really make static class? + +137 +00:05:10,000 --> 00:05:13,000 +Yes, we can, provided that it's an inner class. + +138 +00:05:13,000 --> 00:05:17,000 +If it is outer class, you can't make outer class static. + +139 +00:05:17,000 --> 00:05:18,000 +You can see, it'll give you error. + +140 +00:05:18,000 --> 00:05:21,000 +It says illegal modifier for a class A; + +141 +00:05:21,000 --> 00:05:25,000 +only public, abstract and final art permitted. + +142 +00:05:25,000 --> 00:05:27,000 +So basically you can't use static here. + +143 +00:05:27,000 --> 00:05:30,000 +Static can be used only for the inner class. + +144 +00:05:30,000 --> 00:05:33,000 +Okay, I hope that makes sense. + +145 +00:05:33,000 --> 00:05:34,000 +So that's a concept of inner class. + +146 +00:05:34,000 --> 00:05:36,000 +And in the upcoming videos, + +147 +00:05:36,000 --> 00:05:39,000 +we'll do something more within classes. + diff --git a/03 - Advance Java/007 Source-code.url b/03 - Advance Java/007 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/03 - Advance Java/007 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/03 - Advance Java/008 Anonymous Inner class_en.srt b/03 - Advance Java/008 Anonymous Inner class_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..6ae0beaaf96b5653b2457257f5b997a92da1073f --- /dev/null +++ b/03 - Advance Java/008 Anonymous Inner class_en.srt @@ -0,0 +1,508 @@ +1 +00:00:00,000 --> 00:00:03,000 +(soft music plays) + +2 +00:00:03,000 --> 00:00:05,000 +-: Now once we know about Inner Class, + +3 +00:00:05,000 --> 00:00:07,000 +let's talk about Anonymous Inner Class. + +4 +00:00:07,000 --> 00:00:10,000 +I know that sounds weird, right? Anonymous. + +5 +00:00:10,000 --> 00:00:12,000 +Anonymous simply means something which doesn't have a name. + +6 +00:00:12,000 --> 00:00:14,000 +Okay? So what I'll do is I will just try + +7 +00:00:14,000 --> 00:00:16,000 +to remove everything here + +8 +00:00:16,000 --> 00:00:19,000 +and let's work with this particular class, okay? + +9 +00:00:19,000 --> 00:00:21,000 +And in fact, I needed a A class, + +10 +00:00:21,000 --> 00:00:23,000 +let's say class A is there, okay? + +11 +00:00:23,000 --> 00:00:26,000 +And inside this class A, I'm creating a method, + +12 +00:00:26,000 --> 00:00:28,000 +which is public void show. + +13 +00:00:28,000 --> 00:00:31,000 +Again, just to keep it simple, I'm using simple names here + +14 +00:00:31,000 --> 00:00:34,000 +and here I will print in A show, okay? + +15 +00:00:34,000 --> 00:00:38,000 +And if I want to call A, of course, I will simply say A OBJ. + +16 +00:00:38,000 --> 00:00:40,000 +You need to create object of A. + +17 +00:00:40,000 --> 00:00:44,000 +And then I will say obj.show + +18 +00:00:44,000 --> 00:00:48,000 +Let's see if this works. Compile and run. + +19 +00:00:48,000 --> 00:00:48,000 +It works. + +20 +00:00:48,000 --> 00:00:51,000 +You can see got, in A Show, that perfectly works. + +21 +00:00:51,000 --> 00:00:53,000 +Now the question I have for you is very simple question: + +22 +00:00:53,000 --> 00:00:56,000 +What if I want to change the behavior of show method? + +23 +00:00:56,000 --> 00:00:58,000 +Okay, that means I wanted to do something else. + +24 +00:00:58,000 --> 00:01:01,000 +Now I know in your mind you have the idea, right? + +25 +00:01:01,000 --> 00:01:03,000 +We can simply create a class which is B, + +26 +00:01:03,000 --> 00:01:06,000 +and this class B will extend A. + +27 +00:01:06,000 --> 00:01:09,000 +So basically we're trying to do method over writing here. + +28 +00:01:09,000 --> 00:01:11,000 +In this B as well, + +29 +00:01:11,000 --> 00:01:13,000 +you will be having the same method, which is show, + +30 +00:01:13,000 --> 00:01:14,000 +which will print in B show. + +31 +00:01:14,000 --> 00:01:16,000 +We just want to do something else. + +32 +00:01:16,000 --> 00:01:18,000 +And by doing something else, what I mean by in this code, + +33 +00:01:18,000 --> 00:01:20,000 +I'm just trying to print something else, + +34 +00:01:20,000 --> 00:01:21,000 +but of course you can do it whatever you want. + +35 +00:01:21,000 --> 00:01:25,000 +I just want to change the behavior of this show method. + +36 +00:01:25,000 --> 00:01:27,000 +So we created a new class called B, + +37 +00:01:27,000 --> 00:01:31,000 +and here what we can do is we can create the object of B, + +38 +00:01:31,000 --> 00:01:33,000 +but the reference is of A. + +39 +00:01:33,000 --> 00:01:35,000 +So what we are doing is we are overriding + +40 +00:01:35,000 --> 00:01:37,000 +the behavior of show. + +41 +00:01:37,000 --> 00:01:39,000 +And then you will say, "Hey, we have + +42 +00:01:39,000 --> 00:01:40,000 +already talked about it," right? + +43 +00:01:40,000 --> 00:01:42,000 +We have talked about method of writing. + +44 +00:01:42,000 --> 00:01:43,000 +Hold on, hold your horses. + +45 +00:01:43,000 --> 00:01:46,000 +Let's see, if I compile this code and run, + +46 +00:01:46,000 --> 00:01:47,000 +it says, in B Show. + +47 +00:01:47,000 --> 00:01:50,000 +But now, if you think about this class, now first of all, + +48 +00:01:50,000 --> 00:01:53,000 +as we mentioned, every time you compile a code, + +49 +00:01:53,000 --> 00:01:55,000 +it'll create all the class files. + +50 +00:01:55,000 --> 00:01:59,000 +See, we got a B because of the previous code. + +51 +00:01:59,000 --> 00:02:01,000 +Let's delete that. You can see we got B.class as well. + +52 +00:02:01,000 --> 00:02:03,000 +Now you will say, what's the harm in that? + +53 +00:02:03,000 --> 00:02:08,000 +See the harm is, the only purpose of this Class B was + +54 +00:02:08,000 --> 00:02:12,000 +to extend or to extend a class to override the method. + +55 +00:02:12,000 --> 00:02:14,000 +And maybe it'll be used only once. + +56 +00:02:14,000 --> 00:02:17,000 +Now, of course, if it is using multiple times + +57 +00:02:17,000 --> 00:02:19,000 +in different places, of course + +58 +00:02:19,000 --> 00:02:22,000 +you should create a different class. + +59 +00:02:22,000 --> 00:02:25,000 +But what if we know that this class will be used only once, + +60 +00:02:25,000 --> 00:02:27,000 +only for this particular use? + +61 +00:02:27,000 --> 00:02:31,000 +In that scenario, why to create a new class? + +62 +00:02:31,000 --> 00:02:33,000 +Oh, then you will say, "If you don't get a new class, + +63 +00:02:33,000 --> 00:02:35,000 +how will you get a new implementation?" + +64 +00:02:35,000 --> 00:02:36,000 +Okay, let me show you something. + +65 +00:02:36,000 --> 00:02:40,000 +What if I can simply come back here just + +66 +00:02:40,000 --> 00:02:42,000 +before the semicolon + +67 +00:02:42,000 --> 00:02:45,000 +and I can define what should be the new implementation? + +68 +00:02:45,000 --> 00:02:48,000 +I know, this looks tricky, but just be with me. + +69 +00:02:48,000 --> 00:02:51,000 +So till this point, we have seen that first you have + +70 +00:02:51,000 --> 00:02:54,000 +to create a class and then you create the object, right? + +71 +00:02:54,000 --> 00:02:56,000 +And that's what we are doing from a long time. + +72 +00:02:56,000 --> 00:02:59,000 +But now this is the first time that I'm saying I want + +73 +00:02:59,000 --> 00:03:00,000 +to create object of A. + +74 +00:03:00,000 --> 00:03:05,000 +But this is the implementation, this is the design for it. + +75 +00:03:05,000 --> 00:03:07,000 +Don't go for the original design. + +76 +00:03:07,000 --> 00:03:08,000 +I will give you new design. + +77 +00:03:08,000 --> 00:03:11,000 +The new design is, I will use the same method name + +78 +00:03:11,000 --> 00:03:12,000 +or same method here. + +79 +00:03:12,000 --> 00:03:14,000 +The only change I want to do here is + +80 +00:03:14,000 --> 00:03:16,000 +it'll print in new show. + +81 +00:03:16,000 --> 00:03:20,000 +I'll just provide some invitation so that it'll look better. + +82 +00:03:20,000 --> 00:03:22,000 +So what I'm doing here is I'm providing a different + +83 +00:03:22,000 --> 00:03:26,000 +implementation for show method, okay? + +84 +00:03:26,000 --> 00:03:30,000 +And let's see if this works. I just want to see, compile. + +85 +00:03:30,000 --> 00:03:33,000 +And you can see that it says, in new show, it is not going + +86 +00:03:33,000 --> 00:03:34,000 +for the original method. + +87 +00:03:34,000 --> 00:03:37,000 +So we are providing a new implementation here. + +88 +00:03:37,000 --> 00:03:40,000 +Now, this is called Inner Class, okay? + +89 +00:03:40,000 --> 00:03:42,000 +You will say, "Hey, how it's a class? + +90 +00:03:42,000 --> 00:03:45,000 +We are not using a class keyword," right? + +91 +00:03:45,000 --> 00:03:46,000 +That's right, we are not using a class keyword + +92 +00:03:46,000 --> 00:03:48,000 +and we are not even giving a name to it. + +93 +00:03:48,000 --> 00:03:50,000 +And that's why it is a Inner Class, + +94 +00:03:50,000 --> 00:03:53,000 +because this class is created inside the demo class. + +95 +00:03:53,000 --> 00:03:56,000 +And also there's no name for the class. + +96 +00:03:56,000 --> 00:03:58,000 +And that's why if you go back to your files, + +97 +00:03:58,000 --> 00:04:01,000 +can you see this? + +98 +00:04:01,000 --> 00:04:04,000 +We got demo, inside that we have a class, + +99 +00:04:04,000 --> 00:04:06,000 +but there's no name for it. + +100 +00:04:06,000 --> 00:04:08,000 +And since we don't have a name for it, it says one, + +101 +00:04:08,000 --> 00:04:10,000 +of course, if you create multiple classes, + +102 +00:04:10,000 --> 00:04:12,000 +we'll have a different number. + +103 +00:04:12,000 --> 00:04:13,000 +It was a 2, 3, 4, and list goes on. + +104 +00:04:13,000 --> 00:04:16,000 +And since we don't have a name, + +105 +00:04:16,000 --> 00:04:17,000 +it is called anonymous class. + +106 +00:04:17,000 --> 00:04:21,000 +So this is anonymous in a class, which we are going + +107 +00:04:21,000 --> 00:04:22,000 +to use a lot in the upcoming videos. + +108 +00:04:22,000 --> 00:04:24,000 +Once we talk about interfaces, + +109 +00:04:24,000 --> 00:04:27,000 +once we talk about functional interfaces, + +110 +00:04:27,000 --> 00:04:28,000 +Lambda Expressions, + +111 +00:04:28,000 --> 00:04:30,000 +this is very famous concept, so just get used to it. + +112 +00:04:30,000 --> 00:04:32,000 +Now, if you want me + +113 +00:04:32,000 --> 00:04:34,000 +to type this code one second, I will do that for you. + +114 +00:04:34,000 --> 00:04:36,000 +What I will do is I will just go back here. + +115 +00:04:36,000 --> 00:04:40,000 +See, it is not difficult, it's just that you're unfamiliar + +116 +00:04:40,000 --> 00:04:42,000 +with this concept and it'll take some time. + +117 +00:04:42,000 --> 00:04:45,000 +Okay, so what you do is just before the semicolon and + +118 +00:04:45,000 --> 00:04:48,000 +after the constructor open the curly brackets and close, + +119 +00:04:48,000 --> 00:04:49,000 +this is your class. + +120 +00:04:49,000 --> 00:04:53,000 +And in this class you provide the method, which is show, + +121 +00:04:53,000 --> 00:04:56,000 +and whatever you wanna do in show, you can do that here, + +122 +00:04:56,000 --> 00:04:58,000 +you can say, I'm printing a new show. + +123 +00:04:58,000 --> 00:04:59,000 +And then you can call this show. + +124 +00:04:59,000 --> 00:05:01,000 +So when you call this show, it'll call the show + +125 +00:05:01,000 --> 00:05:04,000 +of the new implementation, not this one. + +126 +00:05:04,000 --> 00:05:07,000 +Okay? And yeah, that's how it works. + +127 +00:05:07,000 --> 00:05:11,000 +So that is your Anonymous Inner Class. + diff --git a/03 - Advance Java/008 Source-code.url b/03 - Advance Java/008 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/03 - Advance Java/008 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/03 - Advance Java/009 Abstract and Anonymous Inner Class_en.srt b/03 - Advance Java/009 Abstract and Anonymous Inner Class_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..1b445eee518e45f55a66df9eaf59f64584c50a7b --- /dev/null +++ b/03 - Advance Java/009 Abstract and Anonymous Inner Class_en.srt @@ -0,0 +1,324 @@ +1 +00:00:00,000 --> 00:00:03,000 +(light calming music) + +2 +00:00:03,000 --> 00:00:05,000 +-: Okay, so we have talked about abstract class. + +3 +00:00:05,000 --> 00:00:07,000 +We have talked about inner class. + +4 +00:00:07,000 --> 00:00:09,000 +In fact, we have talked about anonymous inner class. + +5 +00:00:09,000 --> 00:00:12,000 +Can we just use both? Let's try. + +6 +00:00:12,000 --> 00:00:14,000 +What I will do is I will just make this class + +7 +00:00:14,000 --> 00:00:16,000 +as an abstract class + +8 +00:00:16,000 --> 00:00:18,000 +because I want to create this method show + +9 +00:00:18,000 --> 00:00:21,000 +as an abstract method. + +10 +00:00:21,000 --> 00:00:23,000 +Let's say this is abstract, okay? + +11 +00:00:23,000 --> 00:00:28,000 +And then let's say I'm not doing all those thing. + +12 +00:00:28,000 --> 00:00:29,000 +Now I have a question for you. + +13 +00:00:29,000 --> 00:00:31,000 +If I want to create a object + +14 +00:00:31,000 --> 00:00:34,000 +or if I want to instantiate A, can I do that? + +15 +00:00:34,000 --> 00:00:36,000 +Of course, in the abstract class we have seen, + +16 +00:00:36,000 --> 00:00:39,000 +we can't do that because you can't instantiate + +17 +00:00:39,000 --> 00:00:40,000 +the abstract class. + +18 +00:00:40,000 --> 00:00:43,000 +And we have found a solution as well. + +19 +00:00:43,000 --> 00:00:46,000 +You can create a separate class by saying extends A, + +20 +00:00:46,000 --> 00:00:48,000 +and then you can provide the implementation here, + +21 +00:00:48,000 --> 00:00:52,000 +you can just go here, quick fix, implement the method. + +22 +00:00:52,000 --> 00:00:54,000 +And one more thing, you might be saying + +23 +00:00:54,000 --> 00:00:56,000 +that I'm reviewing this line multiple times, + +24 +00:00:56,000 --> 00:00:57,000 +it's just that we have not talked about + +25 +00:00:57,000 --> 00:00:59,000 +what is @Override. + +26 +00:00:59,000 --> 00:01:00,000 +Well, we have a separate topic on that. + +27 +00:01:00,000 --> 00:01:03,000 +So at that point we'll discuss that. + +28 +00:01:03,000 --> 00:01:08,000 +So here, I just want to say in B Show, okay? + +29 +00:01:08,000 --> 00:01:11,000 +And then of course I can create the object of B + +30 +00:01:11,000 --> 00:01:15,000 +and that's done with the help of this object, obj, + +31 +00:01:15,000 --> 00:01:17,000 +can call show, again, we have seen this in + +32 +00:01:17,000 --> 00:01:19,000 +abstract class concept, + +33 +00:01:19,000 --> 00:01:21,000 +but I wanna talk about something else. + +34 +00:01:21,000 --> 00:01:23,000 +So you can see it says in B Show. + +35 +00:01:24,000 --> 00:01:27,000 +Now if you look at the entire purpose of this class B + +36 +00:01:27,000 --> 00:01:30,000 +is to implement the show method, that's it. + +37 +00:01:30,000 --> 00:01:34,000 +And if you are using this method only once, + +38 +00:01:34,000 --> 00:01:36,000 +then why to even create this class? + +39 +00:01:36,000 --> 00:01:37,000 +Can I use something else? + +40 +00:01:37,000 --> 00:01:41,000 +Can I use anonymous inner class here? + +41 +00:01:41,000 --> 00:01:43,000 +Okay, so what I'm saying is I can simply say + +42 +00:01:43,000 --> 00:01:47,000 +new A, and okay, it's not working, + +43 +00:01:47,000 --> 00:01:50,000 +but can I provide the implementation here? + +44 +00:01:50,000 --> 00:01:52,000 +And I can create a method which is public void. + +45 +00:01:52,000 --> 00:01:54,000 +In fact, I would just simply paste the code + +46 +00:01:54,000 --> 00:01:56,000 +because we already have that code with me. + +47 +00:01:56,000 --> 00:01:58,000 +And here I will say in new Show. + +48 +00:01:58,000 --> 00:02:01,000 +So what I'm doing is I'm providing the implementation + +49 +00:02:01,000 --> 00:02:05,000 +for the abstract class here itself, okay? + +50 +00:02:05,000 --> 00:02:06,000 +And you might be thinking, + +51 +00:02:06,000 --> 00:02:08,000 +we are still getting new A, right? + +52 +00:02:08,000 --> 00:02:11,000 +Is it the object of an abstract class? + +53 +00:02:11,000 --> 00:02:12,000 +We can't do that, right? + +54 +00:02:12,000 --> 00:02:14,000 +How we are doing this, + +55 +00:02:14,000 --> 00:02:16,000 +the thing is, this is a class here, right? + +56 +00:02:16,000 --> 00:02:17,000 +The anonymous in a class, + +57 +00:02:17,000 --> 00:02:21,000 +and we are creating the object of not A, + +58 +00:02:21,000 --> 00:02:25,000 +we are creating the object of this anonymous inner class. + +59 +00:02:25,000 --> 00:02:26,000 +That's a point to remember. + +60 +00:02:26,000 --> 00:02:28,000 +So most of the time when we have interfaces + +61 +00:02:28,000 --> 00:02:30,000 +or abstract class, we use this concept. + +62 +00:02:30,000 --> 00:02:33,000 +So if you want to implement the interface + +63 +00:02:33,000 --> 00:02:36,000 +or the abstract class only once, + +64 +00:02:36,000 --> 00:02:37,000 +then you can use anonymous in a class. + +65 +00:02:37,000 --> 00:02:39,000 +And this perfectly works. + +66 +00:02:39,000 --> 00:02:43,000 +So let me just try it out, compile this code and run. + +67 +00:02:43,000 --> 00:02:45,000 +You can see it works. + +68 +00:02:45,000 --> 00:02:46,000 +So we can use anonymous in a class + +69 +00:02:46,000 --> 00:02:49,000 +for the abstract class as well. + +70 +00:02:49,000 --> 00:02:52,000 +In fact, if you have multiple methods, you can still do it. + +71 +00:02:52,000 --> 00:02:54,000 +If let's say we have one more abstract method here, + +72 +00:02:54,000 --> 00:02:56,000 +which is public void, let's say config. + +73 +00:02:56,000 --> 00:02:59,000 +We can create a method for config as well. + +74 +00:02:59,000 --> 00:03:01,000 +And since we're using an IDE, + +75 +00:03:01,000 --> 00:03:04,000 +it give you the method so you can simply use this + +76 +00:03:04,000 --> 00:03:07,000 +and you can print in config. + +77 +00:03:07,000 --> 00:03:09,000 +So it's not just for one method. + +78 +00:03:09,000 --> 00:03:10,000 +If you have multiple methods, + +79 +00:03:10,000 --> 00:03:12,000 +you can provide the implementation + +80 +00:03:12,000 --> 00:03:15,000 +in the anonymous in a class. + +81 +00:03:15,000 --> 00:03:18,000 +So that's about abstract class and anonymous in the class. + diff --git a/03 - Advance Java/009 Source-code.url b/03 - Advance Java/009 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/03 - Advance Java/009 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/03 - Advance Java/010 Source-code.url b/03 - Advance Java/010 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/03 - Advance Java/010 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/03 - Advance Java/010 What is Interface_en.srt b/03 - Advance Java/010 What is Interface_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..6c456e35b872594494024253ca52dbf7d2f1fe2c --- /dev/null +++ b/03 - Advance Java/010 What is Interface_en.srt @@ -0,0 +1,792 @@ +1 +00:00:00,000 --> 00:00:03,000 +-: In this video, we'll talk about interfaces. + +2 +00:00:03,000 --> 00:00:05,000 +Now, before we start with interface, + +3 +00:00:05,000 --> 00:00:06,000 +let's talk about the abstract class. + +4 +00:00:06,000 --> 00:00:08,000 +Now, let's say I create abstract class here. + +5 +00:00:08,000 --> 00:00:11,000 +So we'll say abstract class A. + +6 +00:00:11,000 --> 00:00:14,000 +And in this class, let me create some methods. + +7 +00:00:14,000 --> 00:00:16,000 +Now, we all know in the abstract class, + +8 +00:00:16,000 --> 00:00:20,000 +we can create abstract methods and normal methods as well. + +9 +00:00:20,000 --> 00:00:21,000 +But let's say if I have abstract class, + +10 +00:00:21,000 --> 00:00:24,000 +which only has let's say abstract methods, + +11 +00:00:24,000 --> 00:00:28,000 +if I say public abstract void show, + +12 +00:00:28,000 --> 00:00:32,000 +and we have public abstract void config. + +13 +00:00:32,000 --> 00:00:33,000 +Now, when you have abstract class + +14 +00:00:33,000 --> 00:00:35,000 +with two abstract methods, of course this works. + +15 +00:00:35,000 --> 00:00:38,000 +There's no compulsion to have non-abstract methods here, + +16 +00:00:38,000 --> 00:00:40,000 +or we call them as concrete methods. + +17 +00:00:40,000 --> 00:00:41,000 +There's no need for them. + +18 +00:00:41,000 --> 00:00:43,000 +Now, when you have a situation + +19 +00:00:43,000 --> 00:00:45,000 +where you have abstract class, + +20 +00:00:45,000 --> 00:00:47,000 +which only has abstract methods, + +21 +00:00:47,000 --> 00:00:49,000 +we have another way of doing it. + +22 +00:00:49,000 --> 00:00:51,000 +Now see, all this are decided at design time. + +23 +00:00:51,000 --> 00:00:54,000 +So when you make an application at designing, + +24 +00:00:54,000 --> 00:00:56,000 +you specify things, okay? + +25 +00:00:56,000 --> 00:00:57,000 +What are the classes I need + +26 +00:00:57,000 --> 00:01:00,000 +and what are the abstract class we need? + +27 +00:01:00,000 --> 00:01:02,000 +So you create this hierarchy of system. + +28 +00:01:02,000 --> 00:01:04,000 +Of course we'll talk about that hierarchy in some time. + +29 +00:01:04,000 --> 00:01:07,000 +Why do we need it? But at this point, we create a hierarchy. + +30 +00:01:07,000 --> 00:01:10,000 +And when you have a class which only has abstract methods, + +31 +00:01:10,000 --> 00:01:13,000 +the other alternative for that is to create a interface. + +32 +00:01:13,000 --> 00:01:14,000 +So instead of creating an abstract class, + +33 +00:01:14,000 --> 00:01:17,000 +we can create an interface. + +34 +00:01:17,000 --> 00:01:19,000 +Most of the time we use this interface + +35 +00:01:19,000 --> 00:01:20,000 +in project development. + +36 +00:01:20,000 --> 00:01:23,000 +So let's understand that. So we have the interface A. + +37 +00:01:23,000 --> 00:01:25,000 +Now you will say, what is changing there? + +38 +00:01:25,000 --> 00:01:27,000 +Now, first of all, interface is not a class, okay? + +39 +00:01:27,000 --> 00:01:29,000 +So that's why that's the main difference + +40 +00:01:29,000 --> 00:01:31,000 +between abstract class and interface. + +41 +00:01:31,000 --> 00:01:32,000 +And by default, + +42 +00:01:32,000 --> 00:01:36,000 +every method in the interface is public abstract. + +43 +00:01:36,000 --> 00:01:38,000 +So these two keywords actually not required, + +44 +00:01:38,000 --> 00:01:40,000 +because anyway they are public abstract. + +45 +00:01:40,000 --> 00:01:41,000 +So even if you don't mention, + +46 +00:01:41,000 --> 00:01:44,000 +they are by default public abstract. + +47 +00:01:44,000 --> 00:01:45,000 +And that's why, if you can see, + +48 +00:01:45,000 --> 00:01:47,000 +when I put a semicolon just after the method, + +49 +00:01:47,000 --> 00:01:49,000 +there is no error, + +50 +00:01:49,000 --> 00:01:52,000 +because behind the scene, public abstract is there. + +51 +00:01:52,000 --> 00:01:54,000 +Now this is your interface. + +52 +00:01:54,000 --> 00:01:57,000 +So interface basically means it's like a class, just + +53 +00:01:57,000 --> 00:02:02,000 +that all the methods are by default, public abstract, okay? + +54 +00:02:02,000 --> 00:02:05,000 +But then how will I use this? How will I instantiate this? + +55 +00:02:05,000 --> 00:02:07,000 +The same problem was there with abstract class, right? + +56 +00:02:07,000 --> 00:02:09,000 +We cannot instantiate it. + +57 +00:02:09,000 --> 00:02:12,000 +Now, if you look at this, how do I instantiate? Let's try. + +58 +00:02:12,000 --> 00:02:14,000 +So I will say A obj. + +59 +00:02:14,000 --> 00:02:16,000 +See, there's no problem in creating a reference of it. + +60 +00:02:16,000 --> 00:02:20,000 +So that also means we are creating a type, + +61 +00:02:20,000 --> 00:02:21,000 +the way you create classes. + +62 +00:02:21,000 --> 00:02:24,000 +So let's say if A is a class, you can say A obj. + +63 +00:02:24,000 --> 00:02:25,000 +So A is a type, right? + +64 +00:02:25,000 --> 00:02:29,000 +In the same way here, the interface A is a type. + +65 +00:02:29,000 --> 00:02:30,000 +Okay, this works. + +66 +00:02:30,000 --> 00:02:34,000 +But now when you try to create object for this, + +67 +00:02:34,000 --> 00:02:35,000 +it will give you an error. + +68 +00:02:35,000 --> 00:02:37,000 +It says if you see the error, + +69 +00:02:37,000 --> 00:02:40,000 +it says "Cannot instantiate the type A," + +70 +00:02:40,000 --> 00:02:43,000 +because it's interface, you can't instantiate it. + +71 +00:02:43,000 --> 00:02:45,000 +Okay, then what's the solution? + +72 +00:02:45,000 --> 00:02:47,000 +The solution is, see, first of all, + +73 +00:02:47,000 --> 00:02:48,000 +why do we create interface? + +74 +00:02:48,000 --> 00:02:50,000 +We create interface so that we can design something. + +75 +00:02:50,000 --> 00:02:51,000 +We can say, example, let's say + +76 +00:02:51,000 --> 00:02:55,000 +if you ask me about how do we create a table, + +77 +00:02:55,000 --> 00:02:58,000 +I can say, or maybe if you ask me how to reach somewhere, + +78 +00:02:58,000 --> 00:03:01,000 +so I can say, "Go straight, take a right, go straight, + +79 +00:03:01,000 --> 00:03:03,000 +take a right, take an auto, take a bus, + +80 +00:03:03,000 --> 00:03:05,000 +get down at the next stop." + +81 +00:03:05,000 --> 00:03:07,000 +So what I'm doing is I'm giving you the path, right? + +82 +00:03:07,000 --> 00:03:11,000 +I'm giving you design. You have to literally take a walk. + +83 +00:03:11,000 --> 00:03:14,000 +I will not walk for you. I will just show the directions. + +84 +00:03:14,000 --> 00:03:16,000 +In the same way, interface says, + +85 +00:03:16,000 --> 00:03:19,000 +"I will tell you what are the methods you need, + +86 +00:03:19,000 --> 00:03:21,000 +but I will not implement them. + +87 +00:03:21,000 --> 00:03:22,000 +It is your job to implement." + +88 +00:03:22,000 --> 00:03:25,000 +So when I say your, now I'm talking about classes. + +89 +00:03:25,000 --> 00:03:28,000 +See, ultimately you create object of a class, + +90 +00:03:28,000 --> 00:03:31,000 +interface just show you the design. + +91 +00:03:31,000 --> 00:03:33,000 +So here, what I can do is I can create a class, + +92 +00:03:33,000 --> 00:03:34,000 +let's say class B. + +93 +00:03:34,000 --> 00:03:36,000 +So we have a class B here. + +94 +00:03:36,000 --> 00:03:37,000 +And then in this class, + +95 +00:03:37,000 --> 00:03:40,000 +I want to implement these two methods. + +96 +00:03:40,000 --> 00:03:43,000 +Now, in terms of abstract class, we say extends, right? + +97 +00:03:43,000 --> 00:03:46,000 +Because class extends another class. + +98 +00:03:46,000 --> 00:03:48,000 +Here A is the interface. + +99 +00:03:48,000 --> 00:03:50,000 +We have to use a different keyword here. + +100 +00:03:50,000 --> 00:03:52,000 +The keyword which we use is implements. + +101 +00:03:52,000 --> 00:03:54,000 +So whenever you want to implement the interface, + +102 +00:03:54,000 --> 00:03:57,000 +you say implements keyword. + +103 +00:03:57,000 --> 00:04:00,000 +And when you say implements, it becomes compulsory for you + +104 +00:04:00,000 --> 00:04:02,000 +to define both the methods, okay? + +105 +00:04:02,000 --> 00:04:04,000 +If you fail to define both the methods, + +106 +00:04:04,000 --> 00:04:08,000 +by default, your class also becomes abstract, okay? + +107 +00:04:08,000 --> 00:04:10,000 +But we don't want to create abstract class, right? + +108 +00:04:10,000 --> 00:04:11,000 +We want to create a concrete class. + +109 +00:04:11,000 --> 00:04:13,000 +We want to create object of B. + +110 +00:04:13,000 --> 00:04:18,000 +In that case, it's your job to define both the methods. + +111 +00:04:18,000 --> 00:04:20,000 +Okay, there's one way you can use a shortcut here. + +112 +00:04:20,000 --> 00:04:22,000 +You can just go back here and say Quick Fix. + +113 +00:04:22,000 --> 00:04:24,000 +It will ask you to implement the methods, + +114 +00:04:24,000 --> 00:04:26,000 +or you can do the hard work. + +115 +00:04:26,000 --> 00:04:28,000 +You can say public void show. + +116 +00:04:28,000 --> 00:04:30,000 +And then you can define something here. + +117 +00:04:30,000 --> 00:04:33,000 +I can say I'm defining in show. + +118 +00:04:33,000 --> 00:04:35,000 +And then we can define another method as well, + +119 +00:04:35,000 --> 00:04:38,000 +which is config or public void config. + +120 +00:04:38,000 --> 00:04:42,000 +And here we can define in config. + +121 +00:04:42,000 --> 00:04:43,000 +So basically what you're doing is + +122 +00:04:43,000 --> 00:04:46,000 +you are implementing both the methods. + +123 +00:04:46,000 --> 00:04:47,000 +As I mentioned before, + +124 +00:04:47,000 --> 00:04:49,000 +if you fail to implement these methods, + +125 +00:04:49,000 --> 00:04:51,000 +this class becomes an abstract class, + +126 +00:04:51,000 --> 00:04:53,000 +but we are implementing it, right? + +127 +00:04:53,000 --> 00:04:54,000 +Now, when you implement, + +128 +00:04:54,000 --> 00:04:57,000 +basically you can't create object of A, + +129 +00:04:57,000 --> 00:05:02,000 +but you can create object of B, and you can see it works. + +130 +00:05:02,000 --> 00:05:06,000 +And now using this obj I can call show. + +131 +00:05:06,000 --> 00:05:10,000 +I can call, what other method we have? We have config. + +132 +00:05:10,000 --> 00:05:12,000 +So we can call both the methods and that perfectly works. + +133 +00:05:12,000 --> 00:05:14,000 +If I compile and run this code, + +134 +00:05:14,000 --> 00:05:17,000 +you can see it says, "in show," "in config." + +135 +00:05:17,000 --> 00:05:20,000 +Okay, what else? Can I have? + +136 +00:05:20,000 --> 00:05:23,000 +So the way you create variables here, + +137 +00:05:23,000 --> 00:05:24,000 +so the way you create methods here, + +138 +00:05:24,000 --> 00:05:26,000 +can I create variables? + +139 +00:05:26,000 --> 00:05:28,000 +Now, first of all, we don't define variables, right? + +140 +00:05:28,000 --> 00:05:30,000 +We declare them. So we can actually. + +141 +00:05:30,000 --> 00:05:34,000 +So if I say int, let's say age, that's a variable. I know. + +142 +00:05:34,000 --> 00:05:36,000 +I don't know why I'm taking this age variable every time, + +143 +00:05:36,000 --> 00:05:38,000 +but let's say we have int age here, + +144 +00:05:38,000 --> 00:05:43,000 +and let's say we have string area, so that's a area name, + +145 +00:05:43,000 --> 00:05:45,000 +but you can see we got an error. + +146 +00:05:45,000 --> 00:05:48,000 +That means I cannot create variables. That's weird. + +147 +00:05:48,000 --> 00:05:50,000 +I just said we can, right? + +148 +00:05:50,000 --> 00:05:53,000 +See, the only thing is every variable in the interface, + +149 +00:05:53,000 --> 00:05:55,000 +we can create actually those variables. + +150 +00:05:55,000 --> 00:06:00,000 +The only thing is they are by default, final and static. + +151 +00:06:00,000 --> 00:06:03,000 +So all the variables inside the interface are by default, + +152 +00:06:03,000 --> 00:06:07,000 +final and static, okay? + +153 +00:06:07,000 --> 00:06:10,000 +So that means we have to first initialize the variable. + +154 +00:06:10,000 --> 00:06:11,000 +So when you say it's final, + +155 +00:06:11,000 --> 00:06:13,000 +we have to initialize the variable, right? + +156 +00:06:13,000 --> 00:06:15,000 +Okay, that's not my age, but let's say 44, + +157 +00:06:15,000 --> 00:06:18,000 +and let's say area is Mumbai, okay? + +158 +00:06:18,000 --> 00:06:20,000 +So basically what we're defining is + +159 +00:06:20,000 --> 00:06:21,000 +we are defining the age, which is 44, + +160 +00:06:21,000 --> 00:06:23,000 +and the area is Mumbai. + +161 +00:06:23,000 --> 00:06:28,000 +Now these variables are final and static. + +162 +00:06:28,000 --> 00:06:30,000 +Now, first of all, since they are static, + +163 +00:06:30,000 --> 00:06:33,000 +which means we can directly use the interface name. + +164 +00:06:33,000 --> 00:06:34,000 +Remember when we use static variable, + +165 +00:06:34,000 --> 00:06:38,000 +we don't have to use the object. + +166 +00:06:38,000 --> 00:06:38,000 +Example, if I want + +167 +00:06:38,000 --> 00:06:41,000 +to print the value of name or area here, + +168 +00:06:41,000 --> 00:06:46,000 +I can simply say A., A is the interface, A.area. + +169 +00:06:47,000 --> 00:06:49,000 +I can directly use it, not with the object. + +170 +00:06:50,000 --> 00:06:51,000 +And if I compile and run, it works. + +171 +00:06:51,000 --> 00:06:53,000 +You can say we got Mumbai. + +172 +00:06:53,000 --> 00:06:56,000 +If I try to change the value of this, + +173 +00:06:56,000 --> 00:07:00,000 +if I say A.area is equal to, let's say if I want + +174 +00:07:00,000 --> 00:07:05,000 +to change the area to Hyderabad, it's not working. + +175 +00:07:05,000 --> 00:07:08,000 +You can see it says "The final field," that's right. + +176 +00:07:08,000 --> 00:07:11,000 +The area is final, it is static. + +177 +00:07:11,000 --> 00:07:13,000 +We are using directly the interface name + +178 +00:07:13,000 --> 00:07:15,000 +and it's also final, okay? + +179 +00:07:15,000 --> 00:07:17,000 +So that's one thing you have to remember. + +180 +00:07:17,000 --> 00:07:20,000 +Now, you might be thinking why it has to be static final. + +181 +00:07:20,000 --> 00:07:22,000 +See, first of all, + +182 +00:07:22,000 --> 00:07:24,000 +you don't instantiate the interface, right? + +183 +00:07:24,000 --> 00:07:26,000 +Whatever you instantiate is a class + +184 +00:07:26,000 --> 00:07:29,000 +and you're not doing extends, you're doing implements. + +185 +00:07:29,000 --> 00:07:33,000 +What you get in implements is only the methods, right? + +186 +00:07:33,000 --> 00:07:35,000 +You don't get the variables there in implements. + +187 +00:07:35,000 --> 00:07:37,000 +So that's why it is static, + +188 +00:07:37,000 --> 00:07:39,000 +because we want to use the interface name, + +189 +00:07:39,000 --> 00:07:42,000 +and final, because interface objects + +190 +00:07:42,000 --> 00:07:46,000 +or interface don't have their own memory in the heap, right? + +191 +00:07:46,000 --> 00:07:49,000 +What you create object of is of B, not of A. + +192 +00:07:50,000 --> 00:07:52,000 +So if you don't have your own memory, + +193 +00:07:52,000 --> 00:07:54,000 +how can you have a non final variable? + +194 +00:07:54,000 --> 00:07:56,000 +So this is interface. + +195 +00:07:56,000 --> 00:07:57,000 +Now, in the next video, + +196 +00:07:57,000 --> 00:07:59,000 +let's try to understand why do we need interface + +197 +00:07:59,000 --> 00:08:00,000 +and why do we even need abstract class? + +198 +00:08:00,000 --> 00:08:02,000 +Okay, let's talk about those things in the next video. + diff --git a/03 - Advance Java/011 More on Interfaces_en.srt b/03 - Advance Java/011 More on Interfaces_en.srt new file mode 100644 index 0000000000000000000000000000000000000000..af94a39f659e70958527916acf3d9ccd30393735 --- /dev/null +++ b/03 - Advance Java/011 More on Interfaces_en.srt @@ -0,0 +1,312 @@ +1 +00:00:00,000 --> 00:00:02,000 +-: In fact, before talking about the need for interface, + +2 +00:00:02,000 --> 00:00:04,000 +we have one more thing to add here. + +3 +00:00:04,000 --> 00:00:08,000 +So let's say if I have a interface and I'm creating a class + +4 +00:00:08,000 --> 00:00:10,000 +which implements A, one interface here, + +5 +00:00:10,000 --> 00:00:13,000 +can a class implement multiple interface? + +6 +00:00:13,000 --> 00:00:16,000 +Example, let's say if I have interface, which is called X. + +7 +00:00:16,000 --> 00:00:21,000 +So if I say interface X, so B class implements A and X both. + +8 +00:00:22,000 --> 00:00:23,000 +And the answer is yes. + +9 +00:00:23,000 --> 00:00:24,000 +If you can see, there's no error here. + +10 +00:00:24,000 --> 00:00:26,000 +The only thing is whatever method you define here, + +11 +00:00:26,000 --> 00:00:29,000 +so let's say the method name here is run. + +12 +00:00:29,000 --> 00:00:31,000 +You can see we got an error here. + +13 +00:00:31,000 --> 00:00:32,000 +The only thing you have to do is + +14 +00:00:32,000 --> 00:00:34,000 +you have to define run as well. + +15 +00:00:34,000 --> 00:00:37,000 +So you will say public void run. + +16 +00:00:37,000 --> 00:00:39,000 +So basically we can have one class + +17 +00:00:39,000 --> 00:00:41,000 +implementing multiple interfaces. + +18 +00:00:41,000 --> 00:00:43,000 +Now that's not the case with abstract class, right? + +19 +00:00:43,000 --> 00:00:46,000 +Because in abstract class, we can extend only one class. + +20 +00:00:46,000 --> 00:00:47,000 +In interface, + +21 +00:00:47,000 --> 00:00:51,000 +you can have a class implementing multiple interfaces. + +22 +00:00:51,000 --> 00:00:56,000 +So here I can simply print running, okay? + +23 +00:00:56,000 --> 00:00:58,000 +So what we are doing is we are using multiple methods here. + +24 +00:00:58,000 --> 00:01:00,000 +We have three methods coming from two different interfaces. + +25 +00:01:00,000 --> 00:01:03,000 +This interface says two methods, show and config. + +26 +00:01:03,000 --> 00:01:07,000 +And this interface is, has only one method, which is run. + +27 +00:01:07,000 --> 00:01:10,000 +And you can see we got three methods here. + +28 +00:01:10,000 --> 00:01:10,000 +There's one more thing. + +29 +00:01:10,000 --> 00:01:12,000 +Let's say we have one more interface, + +30 +00:01:12,000 --> 00:01:15,000 +which is called interface Y. + +31 +00:01:15,000 --> 00:01:17,000 +Now the thing is when you talk about class and class, + +32 +00:01:17,000 --> 00:01:19,000 +a class can extend another class, right? + +33 +00:01:19,000 --> 00:01:23,000 +What about interface? But can we do interface and interface? + +34 +00:01:23,000 --> 00:01:25,000 +What keyword we have to use here? + +35 +00:01:25,000 --> 00:01:27,000 +See, whenever you have interface to interface, + +36 +00:01:27,000 --> 00:01:28,000 +we say extends. + +37 +00:01:28,000 --> 00:01:31,000 +So interface Y extends X. + +38 +00:01:31,000 --> 00:01:34,000 +So inheritance is valid here as well. + +39 +00:01:34,000 --> 00:01:36,000 +Now in this Y interface, + +40 +00:01:36,000 --> 00:01:38,000 +you'll be having a method called run. + +41 +00:01:38,000 --> 00:01:40,000 +So that's the inheritance, okay? + +42 +00:01:40,000 --> 00:01:44,000 +So you will get this declaration in the interface Y as well. + +43 +00:01:44,000 --> 00:01:45,000 +Let me show you. + +44 +00:01:45,000 --> 00:01:48,000 +If I remove this X and if I keep Y, + +45 +00:01:48,000 --> 00:01:49,000 +even now we have to use run. + +46 +00:01:49,000 --> 00:01:51,000 +Otherwise, if you remove run from here, + +47 +00:01:51,000 --> 00:01:54,000 +if you come in this part, you can see, it'll give you error. + +48 +00:01:54,000 --> 00:01:57,000 +It says "The type B must + +49 +00:01:57,000 --> 00:02:01,000 +implement the inherited abstract method run," okay? + +50 +00:02:01,000 --> 00:02:03,000 +But we are implementing Y, right? + +51 +00:02:03,000 --> 00:02:08,000 +It's just that Y extends X, so we have to use run. + +52 +00:02:08,000 --> 00:02:09,000 +Now point to remember here is + +53 +00:02:09,000 --> 00:02:14,000 +whenever you have a class to class, we say they extends. + +54 +00:02:15,000 --> 00:02:17,000 +Whenever you have class to interface, + +55 +00:02:17,000 --> 00:02:20,000 +basically a class is implementing interface, + +56 +00:02:20,000 --> 00:02:22,000 +so we have to say implements. + +57 +00:02:22,000 --> 00:02:25,000 +And whenever you have interface to interface, + +58 +00:02:25,000 --> 00:02:28,000 +we say that extends again. + +59 +00:02:28,000 --> 00:02:30,000 +Okay, so this is the point you have to remember. + +60 +00:02:30,000 --> 00:02:33,000 +So again, I will uncomment this part, okay? + +61 +00:02:33,000 --> 00:02:35,000 +Now there's only one thing you have to remember. + +62 +00:02:35,000 --> 00:02:38,000 +Whenever you create a reference of A interface + +63 +00:02:38,000 --> 00:02:40,000 +and object of B, which is a class, + +64 +00:02:40,000 --> 00:02:43,000 +I can only call two methods, which is of part A. + +65 +00:02:43,000 --> 00:02:46,000 +If I try to call run with A, it will not work, + +66 +00:02:46,000 --> 00:02:50,000 +because A has no idea what run method is. + +67 +00:02:50,000 --> 00:02:53,000 +If you can see in A, we don't have run. + +68 +00:02:53,000 --> 00:02:54,000 +In fact, what I will do is + +69 +00:02:54,000 --> 00:02:57,000 +I will just remove this code for time being, yeah. + +70 +00:02:57,000 --> 00:02:59,000 +And then if you want to implement run, + +71 +00:02:59,000 --> 00:03:01,000 +if you want to call run, in that case, + +72 +00:03:01,000 --> 00:03:04,000 +we need to create the reference of X, + +73 +00:03:04,000 --> 00:03:07,000 +which is obj1 is equal to new B. + +74 +00:03:07,000 --> 00:03:10,000 +Now, with this obj1, I can call run, + +75 +00:03:10,000 --> 00:03:13,000 +but again, I can't call two other methods, okay? + +76 +00:03:13,000 --> 00:03:15,000 +So that's one thing you have to remember. + +77 +00:03:15,000 --> 00:03:18,000 +Okay now, once we are good with this, let's try + +78 +00:03:18,000 --> 00:03:21,000 +to understand what's the need of the interface. + diff --git a/03 - Advance Java/011 Source-code.url b/03 - Advance Java/011 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/03 - Advance Java/011 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/03 - Advance Java/012 Source-code.url b/03 - Advance Java/012 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/03 - Advance Java/012 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file diff --git a/03 - Advance Java/013 Source-code.url b/03 - Advance Java/013 Source-code.url new file mode 100644 index 0000000000000000000000000000000000000000..f77c52ec3a0c9fa0c49360cf16ad3a6e212fb948 --- /dev/null +++ b/03 - Advance Java/013 Source-code.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://github.com/navinreddy20/Javacode.git \ No newline at end of file