YouTube2Text

OOP 5 | Abstract Classes, Interfaces, Annotations — Transcript

by Kunal Kushwaha · 10,144 words · 1,958 segments · language en · Watch on YouTube

Full transcript

  1. 0:03Hey everyone, welcome back to another
  2. 0:05video. This is the fifth video in the
  3. 0:06complete object-oriented programming
  4. 0:08series. And we're doing a complete data
  5. 0:10structures algorithms boot camp, so make
  6. 0:12sure you check out the links in the
  7. 0:12description. We've done some amazing
  8. 0:14content. But let's move forward with a
  9. 0:16very important topic. So we have already
  10. 0:18learned about inheritance and all these
  11. 0:19other things, okay? The link is in the
  12. 0:21description. There's a separate
  13. 0:22object-oriented programming playlist on
  14. 0:24the channel. You can check it out the
  15. 0:25previous videos. But now let's talk a
  16. 0:27little bit more about the
  17. 0:30problems we were facing in inheritance,
  18. 0:32right? So the problem we was facing in
  19. 0:33we were facing in the multiple
  20. 0:35inheritance was that if you have a class
  21. 0:36A,
  22. 0:38you have a class A, this has a function
  23. 0:40called fun. You have a class B.
  24. 0:43This also has a function called fun,
  25. 0:45exactly like same function, but the body
  26. 0:47is different, let's say. And you inherit
  27. 0:49that in class C. Now you call an object
  28. 0:52C dot fun. Now this will get confused.
  29. 0:55Should I call this one or should I call
  30. 0:57this one? It will get confused.
  31. 1:00That's why Java does not support this.
  32. 1:04Multiple inheritance, okay?
  33. 1:06Cool.
  34. 1:07But what I'm going to teach you now is
  35. 1:12that let's say sometimes you want to
  36. 1:13create a parent class, okay? So a parent
  37. 1:17class like this that you want to create.
  38. 1:20So sometimes you want to just create a
  39. 1:23parent class that just
  40. 1:25tells us what to do and not how to do
  41. 1:28it.
  42. 1:29Okay? So it will just give us a
  43. 1:30generalized form that will be shared by
  44. 1:32all the child classes.
  45. 1:35And parent class is going to be like,
  46. 1:37"Hey child class,
  47. 1:38fill in the details yourself. Be what
  48. 1:40you want." You know our parents tell us,
  49. 1:42"You can be what you want." So it will
  50. 1:44give us a resource, okay? It will just
  51. 1:46say, "Here's a career resource."
  52. 1:48Okay? So there will be a parent class
  53. 1:50like this.
  54. 1:52There's going to be a parent class like
  55. 1:54this. Let's say this is a parent class.
  56. 1:58This is a function called carrier.
  57. 2:04This has a
  58. 2:06function called
  59. 2:08carrier.
  60. 2:10It takes in a few arguments.
  61. 2:12It takes in a few arguments like um
  62. 2:15for example
  63. 2:16you know, name of the carrier.
  64. 2:19That's it.
  65. 2:20That's the only argument it takes right
  66. 2:22now.
  67. 2:23Okay? And it prints something.
  68. 2:26It's like set the name to
  69. 2:28something.
  70. 2:30The something depends on you, okay?
  71. 2:32So
  72. 2:33this body or whatever things that are
  73. 2:35happening, how you are doing your
  74. 2:36carrier or whatever. So your parent has
  75. 2:38given have given you a choice.
  76. 2:41So your parents are like this is a
  77. 2:42function.
  78. 2:44Put the what you what you want your
  79. 2:45carrier to be, that depends on you. I've
  80. 2:47just given you an option.
  81. 2:48So any child class, let's say this is
  82. 2:50going to be a child class.
  83. 2:52This child is going to be like there's a
  84. 2:53carrier option for me.
  85. 2:56Okay? I will make my body. In my body I
  86. 2:58will say name is equal to something like
  87. 3:01coder.
  88. 3:04A second child
  89. 3:05For a second child it's going to be
  90. 3:06like, "Oh, my parent has given me a
  91. 3:08choice. Okay? Inside the body whatever
  92. 3:10you want to do." Body is not given by
  93. 3:12parent.
  94. 3:13What the what your carrier represents,
  95. 3:15that depends on you. Means what the
  96. 3:17function is actually doing, that depends
  97. 3:19on the child classes or the children. So
  98. 3:22you're like, "Okay, my carrier
  99. 3:25the body what it goes over here, that
  100. 3:27depends on me."
  101. 3:28So example name is equal to doctor or
  102. 3:30something.
  103. 3:32Okay? Something like that.
  104. 3:34So this is what is meant by like what we
  105. 3:37mean when we say that uh
  106. 3:40your super class, the parent class, is
  107. 3:42only going to give us the function like
  108. 3:45about it, like a generalized form for
  109. 3:47it, not the body.
  110. 3:50Okay?
  111. 3:51Not the body.
  112. 3:53So, such a class will determine the
  113. 3:55nature of the methods
  114. 3:58that the sub class must implement.
  115. 4:00It's not implementing the method. It's
  116. 4:02just giving the name, the definition of
  117. 4:04it.
  118. 4:05But not the body or anything.
  119. 4:07These are known as abstract methods.
  120. 4:11Okay?
  121. 4:12So, for example, you have an abstract
  122. 4:14method over here, abstract method over
  123. 4:16here. Since this does not have a body,
  124. 4:18what does it mean? It means that when
  125. 4:20your function does not have a body, it
  126. 4:23totally depends on the child classes to
  127. 4:25provide the body for that function.
  128. 4:29Okay? So, now it does not depend on what
  129. 4:32the body is here or here. It will define
  130. 4:34the body for itself.
  131. 4:38Hence, and how can we do it? How can we
  132. 4:40change the body of something that is in
  133. 4:41the parent class? We have already done
  134. 4:43this. We have already done this,
  135. 4:45overriding.
  136. 4:47Okay? So, you may have method that
  137. 4:50methods that must be overridden in the
  138. 4:52child class in order for the child
  139. 4:54class, you know, to have any meaning.
  140. 4:59Okay? So, you want to ensure
  141. 5:02that the child class overrides all the
  142. 5:05necessary methods.
  143. 5:08These are known as abstract methods.
  144. 5:11These are just some empty methods over
  145. 5:13here.
  146. 5:14And child class, in order to use it,
  147. 5:16would have to override it.
  148. 5:20Okay?
  149. 5:22So, let's say your carrier returns
  150. 5:23nothing. So, the format is something
  151. 5:26like this. Abstract keyword.
  152. 5:29For to create a method into an abstract
  153. 5:31method. Type.
  154. 5:33Let's say void. Name.
  155. 5:36Carrier. Parameters.
  156. 5:38String.
  157. 5:40Name.
  158. 5:42That's it. No curly brace or body.
  159. 5:45You're just defining it.
  160. 5:47So, these methods are sometimes like
  161. 5:48referred to as the child classes
  162. 5:50responsibility because there is no
  163. 5:53implementation provided in the parent
  164. 5:55class.
  165. 5:56Hence, the child class must override
  166. 5:59them. It cannot simply use the
  167. 6:02one defined in the parent class because
  168. 6:04the one defined in the parent class does
  169. 6:05not have any body.
  170. 6:09Okay?
  171. 6:10And one more thing, any class, by the
  172. 6:12way, I'll put all these notes in the
  173. 6:14text file in the GitHub repository.
  174. 6:17Any class that contains one or more
  175. 6:20abstract methods must also be declared
  176. 6:23as abstract.
  177. 6:25Very important point. If a class
  178. 6:27contains any one of the abstract
  179. 6:29methods, it should also be declared as
  180. 6:31abstract.
  181. 6:33Okay?
  182. 6:35Let's try to code this and then I'll
  183. 6:37tell you some rules and all these other
  184. 6:38things. Okay? So, let me create another
  185. 6:40like package over here, abstract demo or
  186. 6:42something, ab
  187. 6:43stract demo. Something like that. Create
  188. 6:47a new Java class. Let's say I want to
  189. 6:49create a Java class of like uh parent
  190. 6:52only. Okay?
  191. 6:53Parent.
  192. 6:56Okay? Something like that. So, parent
  193. 6:58will give you a choice. Parent will be
  194. 7:00like abstract
  195. 7:02void
  196. 7:04carrier
  197. 7:07string name. That's it. Since if a class
  198. 7:10contains more than one one or more than
  199. 7:13one abstract methods, that class also
  200. 7:15needs to be declared as abstract. Right?
  201. 7:18So, I can
  202. 7:19make parent abstract as well.
  203. 7:21I have to make it.
  204. 7:23I I'll go over the properties and
  205. 7:25everything later on, but first let's
  206. 7:26look into the implementation of it. I'll
  207. 7:28create a main one.
  208. 7:31Okay? And I'll create a class called
  209. 7:34son.
  210. 7:36Something like that.
  211. 7:37Now, notice something.
  212. 7:38This is a parent class, this is a son
  213. 7:40class. This is obviously going to
  214. 7:41inherit the parent class and obviously
  215. 7:43parent class contains an abstract method
  216. 7:45and I just told you
  217. 7:46I just told you that every child class
  218. 7:49has to override all the abstract methods
  219. 7:52in the parent class. Let's try to see.
  220. 7:54Extends
  221. 7:56parent. Why? For obvious reasons.
  222. 7:58Because the method in the parent class
  223. 8:00does not have any body. That's why it's
  224. 8:02giving an error. It's saying sun must
  225. 8:04either be declared abstract or implement
  226. 8:05the abstract methods in the parent
  227. 8:07class. Okay?
  228. 8:09I'll also give one more thing uh career
  229. 8:11and uh partner. Something like this.
  230. 8:14Okay?
  231. 8:15Parents are like you choose your
  232. 8:16partner. Okay?
  233. 8:19Partner name and
  234. 8:22uh something like um age. Okay? That's
  235. 8:26it.
  236. 8:27So, now they're going to be like they
  237. 8:28contains two abstract methods.
  238. 8:30Please try to you know uh declare
  239. 8:32implement those. So, they're going to be
  240. 8:33like yeah, sure. No worries.
  241. 8:35I'll do that.
  242. 8:36So, it's overriding it. I've already We
  243. 8:38have already learned about overriding.
  244. 8:40Right? We already learned about
  245. 8:42polymorphism uh runtime polymorphism and
  246. 8:44stuff. Already learned about this in
  247. 8:45detail. Already we have covered
  248. 8:47overriding. That's exactly the same
  249. 8:49thing happening over here.
  250. 8:51Okay?
  251. 8:52Sun is going to be like
  252. 8:54um I am going to be a
  253. 8:59doctor or something.
  254. 9:01Okay?
  255. 9:02And um
  256. 9:05something like
  257. 9:06I love
  258. 9:08um
  259. 9:11whom?
  260. 9:12Any random name.
  261. 9:15Um
  262. 9:22I'm going to be not a doctor. I'm just
  263. 9:23going to say
  264. 9:25name.
  265. 9:26Right?
  266. 9:27I love name plus
  267. 9:35she is
  268. 9:38age.
  269. 9:39Okay? Something like that. Similarly, I
  270. 9:41can create a daughter.
  271. 9:45Something like this, right?
  272. 9:47So, daughter can be, you know, it can be
  273. 9:48anything. I'm going to be a name. I love
  274. 9:51this. Um he is something like that. No
  275. 9:55problem. That's it.
  276. 9:57Whatever things I just told you that
  277. 9:59parent class will have some abstract
  278. 10:01methods. Class would have to be need to
  279. 10:02declared as abstract. Now,
  280. 10:05all the child classes would have to
  281. 10:06override these methods.
  282. 10:08Okay? So, sometimes now don't ask me
  283. 10:10question like Kunal, that is fine. We
  284. 10:12are doing this.
  285. 10:13Uh it's pretty simple, Kunal, that uh
  286. 10:15whatever the parent class may contain
  287. 10:17any abstract methods, we would have to
  288. 10:19like, you know, override those over
  289. 10:21here. We understand that. We have
  290. 10:22already learned about overriding and
  291. 10:23everything. No worries. The only thing
  292. 10:25difference is that you are not providing
  293. 10:26a body over here. But why are we doing
  294. 10:29this? What's the use case? Well, it
  295. 10:31looks much more cleaner.
  296. 10:33If you definitely know
  297. 10:35if you definitely know that the
  298. 10:38functions that are the methods that are
  299. 10:40in the parent class needs to be
  300. 10:42overridden, then just make those
  301. 10:44abstract.
  302. 10:45That's the reason. An example is right
  303. 10:47in front of you. I'll give a real world
  304. 10:48example as well. Let's all talk about
  305. 10:50cars, for example. So, every single car
  306. 10:52will have a different engine. We'll
  307. 10:53create a main engine class, and that
  308. 10:55will be dependent on different different
  309. 10:57engines. Okay? I'll give you an example
  310. 10:58for that as well in a in a little while.
  311. 11:01So, let's first try to run this thing.
  312. 11:04Public static void main. I'm going to
  313. 11:05say son son is equal to
  314. 11:10new son.
  315. 11:11Something like that. And then I'm going
  316. 11:12to say son dot,
  317. 11:14you know, career. I'm going to be let's
  318. 11:16say a doctor.
  319. 11:22Doctor.
  320. 11:24Okay? And then I say
  321. 11:28daughter
  322. 11:29daughter is equal to new
  323. 11:32daughter
  324. 11:33then I can say daughter.career
  325. 11:39Wait a minute.
  326. 11:40No.
  327. 11:41It does not really make sense because
  328. 11:43the
  329. 11:44This is literally the same. So, I'm just
  330. 11:46going to What I'm going to do is I'm
  331. 11:47going to remove this from here.
  332. 11:50Okay? You'll be like Kunal, the function
  333. 11:52body is like them like the
  334. 11:54it's literally the same. So,
  335. 11:56how are you doing it? Let me just remove
  336. 11:58this and I'll just put this over here.
  337. 12:00I'm going to be a Now, you will know
  338. 12:02which one is running, right?
  339. 12:05Uh I love something like um
  340. 12:08um
  341. 12:14What do I put here? I have no idea.
  342. 12:16Um let me just uh
  343. 12:19remove this for a while.
  344. 12:22Override it.
  345. 12:24I want to be let's say daughter daughter
  346. 12:26wants to be a
  347. 12:27coder.
  348. 12:28Um I love
  349. 12:35Iron Man.
  350. 12:37Okay?
  351. 12:38Something like this.
  352. 12:40And son is going to be like, I love
  353. 12:42um
  354. 12:47Pepper Potts. Okay? Sounds good?
  355. 12:50Cool.
  356. 12:51So, this makes sense now.
  357. 12:53And
  358. 12:55um
  359. 12:55see if there's any
  360. 13:06Don't need this.
  361. 13:08Don't need this.
  362. 13:09Okay? Son.career daughter.career. So, if
  363. 13:12I run this, when I run son.career, it's
  364. 13:14obviously going to run the son's career,
  365. 13:16this one. And when I run
  366. 13:17daughter.career, it'll run the
  367. 13:19daughter's career. This is very simple
  368. 13:21because it's just simple overriding, run
  369. 13:23run time polymorphism or whatever. I'm
  370. 13:25going to be a doctor. I'm going to be
  371. 13:26coder. No problem. Exactly the same. The
  372. 13:29only thing difference is that here I'm
  373. 13:31just adding some abstract methods. It's
  374. 13:33exactly the same as overriding. In
  375. 13:34detail, I've explained to you the
  376. 13:36dynamic method dispatch and everything
  377. 13:37in the overriding video, the object
  378. 13:39oriented programming properties video.
  379. 13:40Please check it out.
  380. 13:42Okay?
  381. 13:43Cool. So, this is already explained in
  382. 13:45the overriding video. This is actually
  383. 13:47overriding it.
  384. 13:48Okay. So, this is what I meant by
  385. 13:50abstract classes.
  386. 13:52Uh this is a basic example of it. Like
  387. 13:53you would have some uh abstract methods
  388. 13:55over here and then you will have just
  389. 13:58your uh you would have to override those
  390. 14:00in the
  391. 14:01child classes.
  392. 14:03Okay? Now, let's do a deep dive into
  393. 14:06this. Now, another question you may have
  394. 14:07is, "Kunal, can we have uh variables
  395. 14:09over here?"
  396. 14:11Sure. No problem. int age
  397. 14:14That's it. You would have to assign it
  398. 14:16over here, somewhere over here. Let's
  399. 14:18say if I create a constructor.
  400. 14:21Okay. If I create a
  401. 14:22If I create a constructor over here,
  402. 14:24constructor
  403. 14:25and it's just going to pass
  404. 14:27int age. this.age is equal to age.
  405. 14:32Okay? Something like that.
  406. 14:36Now, I'll have to put some age over
  407. 14:38here, 30.
  408. 14:40Okay? Similarly for daughter.
  409. 14:47daughter
  410. 14:49daughter as well.
  411. 14:5328, something like that.
  412. 14:55You can do that. No worries at all.
  413. 14:59Right? Just the simple stuff.
  414. 15:01Okay?
  415. 15:02But, can we create constructor of an
  416. 15:04abstract class?
  417. 15:10It's allowing us to do that like this.
  418. 15:12Okay? No problem because this is
  419. 15:14actually just allowing us to because we
  420. 15:16can call this constructor by the like
  421. 15:18over here you can see using the super
  422. 15:20keyword. Okay? So, here I can just add
  423. 15:23my age or whatever.
  424. 15:25I can remove this. I can just pass the
  425. 15:27age over here like this.
  426. 15:30Okay?
  427. 15:31Something like that.
  428. 15:32That is also totally fine. Cuz in the
  429. 15:34end it's just doing this.age only.
  430. 15:37Okay?
  431. 15:38Can you call this constructor? Let's try
  432. 15:40to do that.
  433. 15:41Let's try to say something like
  434. 15:43parent
  435. 15:46mom is equal to new
  436. 15:49parent.
  437. 15:53Error.
  438. 15:55So, you cannot create objects of an
  439. 15:57abstract class.
  440. 15:59You can't create objects of an abstract
  441. 16:01class.
  442. 16:04Okay?
  443. 16:05So, here you can see that you cannot
  444. 16:06create an object of an abstract class.
  445. 16:08It's asking you to implement the methods
  446. 16:10while you are creating it like this.
  447. 16:12It's asking you to do that.
  448. 16:14Over here.
  449. 16:15Okay? But still it's saying
  450. 16:18error.
  451. 16:20Error. Like this. I mean, this is fine.
  452. 16:22I mean, I have to
  453. 16:23add some age over here or whatever, but
  454. 16:26uh
  455. 16:26you get the idea. You can't just
  456. 16:28directly like create a object of this.
  457. 16:30It's actually not allowing me. It's
  458. 16:31actually saying, "Please override it."
  459. 16:35Okay? For For abstract classes, you
  460. 16:37cannot create objects like this.
  461. 16:40Okay?
  462. 16:41Another point I would like to mention,
  463. 16:42I'm going to leave with you some really
  464. 16:44amazing points now. Can you create
  465. 16:45abstract constructors?
  466. 16:48Can we create abstract constructors?
  467. 16:50Abstract constructors.
  468. 16:57No.
  469. 16:59Abstract construc- constructors are not
  470. 17:01allowed.
  471. 17:02Can you create abstract set- Now, one
  472. 17:04more thing I would like to share. Since
  473. 17:05you cannot create an object of abstract
  474. 17:08class. Okay? So, this is an important
  475. 17:10point. You cannot create objects of
  476. 17:12abstract classes.
  477. 17:13Okay?
  478. 17:14So, if If cannot Why can't we create
  479. 17:16objects of abstract classes?
  480. 17:18Because this if you are able to create
  481. 17:21an object of parent class and you try to
  482. 17:22call the carrier one, it will give you
  483. 17:24an error. It will be like, "Hey, it is
  484. 17:25does not have anybody. How can I call
  485. 17:27it?"
  486. 17:29Right? So, you cannot create objects of
  487. 17:31abstract classes.
  488. 17:33Similarly, for the same reason, you
  489. 17:34cannot create abstract constructors.
  490. 17:39Okay? Can you create abstract static
  491. 17:41methods?
  492. 17:44Do static methods get overridden?
  493. 17:47No.
  494. 17:48Then what is the point of creating
  495. 17:50abstract static methods?
  496. 17:53Abstract methods are what? That needs to
  497. 17:54be overridden. Static methods cannot be
  498. 17:56overridden. So, can you create abstract
  499. 17:58static methods? No.
  500. 18:01Okay?
  501. 18:04But can you create static methods in
  502. 18:06abstract classes?
  503. 18:08That you can do.
  504. 18:09Okay?
  505. 18:10Static
  506. 18:11void
  507. 18:14hello
  508. 18:15That you can do.
  509. 18:18Hey.
  510. 18:20Something like this.
  511. 18:22Because there can be no objects for
  512. 18:23abstract classes. If they are allowed to
  513. 18:25call like, you know, abstract static
  514. 18:28methods, it would mean that we are
  515. 18:29calling like them through the class name
  516. 18:31because it's static. You all already
  517. 18:33know it. So, I can say something like um
  518. 18:37parent dot
  519. 18:39hello No problem in that.
  520. 18:42That's totally fine.
  521. 18:51Okay?
  522. 18:52Cool?
  523. 18:53Sound good?
  524. 18:55Can abstract classes contain normal
  525. 18:56methods? Void
  526. 18:59normal
  527. 19:03This is a
  528. 19:04normal
  529. 19:05method. Most definitely they can.
  530. 19:08This is going to behave the same way as
  531. 19:09in uh
  532. 19:10as normal ones does. So, for example, if
  533. 19:12you want to just uh say
  534. 19:16just, you know, something like
  535. 19:18sun.normal, totally fine.
  536. 19:20You can also override it over here. You
  537. 19:22can say override the normal one. That's
  538. 19:24also totally fine.
  539. 19:27Totally cool. No problem.
  540. 19:29These are just the concepts we already
  541. 19:30learned before. Exactly the same thing
  542. 19:32is applying over here.
  543. 19:34The important points I already
  544. 19:35mentioned, you cannot create static
  545. 19:38constructors in this. You can't create
  546. 19:40static method abstract static methods in
  547. 19:42this. Abstract static constructor,
  548. 19:44sorry. Uh sorry, abstract constructors.
  549. 19:46You can't create abstract constructors
  550. 19:47in this. You can't create abstract
  551. 19:48static methods in this. And um
  552. 19:52at least one of the methods should be
  553. 19:53like abstract as well.
  554. 19:54Okay. I mean it does not mean sorry if
  555. 19:57at least one of the methods is abstract,
  556. 19:58then the class needs to be abstract
  557. 20:00itself.
  558. 20:01That's it. Yeah. Rest you can do
  559. 20:02everything else. No problem.
  560. 20:05These variables and everything works
  561. 20:06similar to like it works in like the
  562. 20:08previous lectures.
  563. 20:10That is it.
  564. 20:12Okay. You can also have methods with
  565. 20:14bodies and stuff. That's also totally
  566. 20:15fine.
  567. 20:18Okay.
  568. 20:19Cool.
  569. 20:20One thing I would like to share is even
  570. 20:22though you cannot create an object of a
  571. 20:23parent class like this of an abstract
  572. 20:25class, you can use it like this
  573. 20:28as a reference variable.
  574. 20:31Okay.
  575. 20:32Because Java's runtime polymorphism, you
  576. 20:35know, we can use it for like the
  577. 20:37superclass
  578. 20:39is implemented through the use of the
  579. 20:40superclass right? We we actually learned
  580. 20:43about this in the previous lecture, the
  581. 20:44runtime polymorphism for this thing.
  582. 20:46Like, you know,
  583. 20:48what it accesses depends on this thing.
  584. 20:50You remember that point we discussed.
  585. 20:52This is also the same thing.
  586. 20:55But which one is actually accessing the
  587. 20:56overriding one?
  588. 20:58That is going to be determined by the
  589. 20:59type of the object.
  590. 21:01We already covered this thing.
  591. 21:03This thing and this thing already
  592. 21:04covered in the over in the old
  593. 21:06properties lecture. Please check it out.
  594. 21:09Exactly the same thing.
  595. 21:11I'm not going to scare you and tell you
  596. 21:14that this is a different thing. I will
  597. 21:15not do that.
  598. 21:17It's the ditto same copy paste thing.
  599. 21:20Okay, and it makes sense also.
  600. 21:22Just it's the with the abstract class
  601. 21:24there's this thing like okay, this is an
  602. 21:25abstract class. That's it.
  603. 21:29Okay? And why can't we create like um
  604. 21:32I mean we are creating a public
  605. 21:33constructor, but then why does it not
  606. 21:35make sense?
  607. 21:36Because you can't you know in
  608. 21:37instantiate an abstract class directly.
  609. 21:41Okay? So, you can only instantiate via a
  610. 21:42derived type.
  611. 21:44Okay?
  612. 21:45That should also not be abstract.
  613. 21:47Right?
  614. 21:49So, that's pretty much about it. Yeah.
  615. 21:53Okay, one more thing.
  616. 21:55Abstract classes need to be like
  617. 21:56extended so that they their methods can
  618. 21:58be overridden.
  619. 21:59If I put final behind a class, it
  620. 22:02prevents it from inherited, right? If I
  621. 22:04put final behind a class, it prevents
  622. 22:06the class from being inherited. Can I
  623. 22:08have a final abstract class then?
  624. 22:11Common sense. No.
  625. 22:13No, you can't.
  626. 22:15No, you can't.
  627. 22:18For obvious reasons.
  628. 22:21Right? Because you want it to be
  629. 22:22inherited.
  630. 22:24That's why you can't have it.
  631. 22:26That is it. Normal stuff.
  632. 22:29Please check out the overriding video.
  633. 22:31It will clear all your doubts. I'm not
  634. 22:33skipping anything. I want this course to
  635. 22:35be the best course on YouTube. I will
  636. 22:38never ever skip anything. Every single
  637. 22:40line I'm explaining to you.
  638. 22:42That's why I'm saying lectures are tied
  639. 22:44together.
  640. 22:45Please check out the OOPs property
  641. 22:47video.
  642. 22:48Okay?
  643. 22:49In that I've deeply explained dynamic
  644. 22:50method dispatch and everything how this
  645. 22:52determines and everything. But the only
  646. 22:54new thing we have over here is abstract.
  647. 22:58Okay?
  648. 22:58Talked about constructors, it does does
  649. 23:00really make sense and stuff like that.
  650. 23:01Okay? So, here sometimes you can put
  651. 23:04like uh abstract like sorry, a
  652. 23:06constructor over here when you have
  653. 23:07let's say some final values. Final int
  654. 23:11value, something like this. Okay? One
  655. 23:14second.
  656. 23:15Add a constructor. I would have to add
  657. 23:16obviously because it's final. So, you
  658. 23:18can cannot have it like without it. So,
  659. 23:20I can say
  660. 23:21um something like um
  661. 23:26value is equal to
  662. 23:29something like this.
  663. 23:32Okay?
  664. 23:33Now, where is this being called?
  665. 23:34Obviously, you cannot call it directly
  666. 23:36to create an object. You can only call
  667. 23:37it like this.
  668. 23:39In another constructor like this.
  669. 23:42Okay? So, even though you create a
  670. 23:43constructor and you're not able to
  671. 23:44create an object from it, you can use
  672. 23:46the super keyword to reference to it.
  673. 23:49Okay? That way you can initialize some
  674. 23:51final variables over there.
  675. 23:53Can you have static variables over here?
  676. 23:57Yeah.
  677. 23:58Sure.
  678. 23:59No problem.
  679. 24:02You can have static methods as well.
  680. 24:04You can have static variables as well.
  681. 24:05No problem in that.
  682. 24:07I think I covered like every single
  683. 24:09possibility.
  684. 24:11Right?
  685. 24:13That's it. Yeah.
  686. 24:16Those are abstract classes.
  687. 24:18It still does not support
  688. 24:19It still will not support like
  689. 24:22multiple inheritance. Okay? This does
  690. 24:24This does not support multiple
  691. 24:25inheritance. If you have two abstract
  692. 24:27classes, something like parent two,
  693. 24:29okay? You can't do You can't extend both
  694. 24:32the classes in one class.
  695. 24:34Multiple inheritance is not allowed.
  696. 24:36Class cannot extend multiple classes.
  697. 24:39For that, we have to learn about
  698. 24:41interfaces.
  699. 24:42Okay? Cool.
  700. 24:45Because here we have so many
  701. 24:46limitations, right? We actually have
  702. 24:48like okay,
  703. 24:49the the problem is not solved. It still
  704. 24:51allows
  705. 24:52allows us to have normal methods and
  706. 24:54stuff like that. The normal method. Same
  707. 24:56problem will come.
  708. 24:58If two normal methods have the same
  709. 24:59name.
  710. 25:01Okay?
  711. 25:02This is about abstract classes.
  712. 25:06Okay?
  713. 25:07Cool?
  714. 25:10Let's move forward. Okay, so we know
  715. 25:11that multiple inheritance are not
  716. 25:13supported in Java due to this one reason
  717. 25:17that we just saw.
  718. 25:20This one. Right? So, if the two bodies
  719. 25:22of functions are there, it will get
  720. 25:23confused like which one to pick. Now,
  721. 25:26instead in order to resolve this, I've
  722. 25:27told this multiple times, we use Java
  723. 25:29interfaces.
  724. 25:31Okay? It's not a class, it's called an
  725. 25:33interface. Okay, that's the name of it.
  726. 25:35We still use the dot Java extension of
  727. 25:38the file, but it's actually called an
  728. 25:40interface. We don't write class A, we
  729. 25:42write interface A. Now, what are
  730. 25:43interfaces and how are they different
  731. 25:45than uh classes for example, right? So,
  732. 25:48let's talk a little bit more about that.
  733. 25:50Interfaces basically contain abstract
  734. 25:52functions. Okay? You all know what
  735. 25:54abstract functions are. No body of the
  736. 25:56function is allowed. It is like a class
  737. 25:59only, but not completely. Okay, it's
  738. 26:01like an abstract class and by default
  739. 26:03the functions uh are public and abstract
  740. 26:07in the interface and the variables are
  741. 26:10what? You tell me.
  742. 26:12What are the variables?
  743. 26:14If these are like interfaces, these are
  744. 26:16not classes, you cannot create their
  745. 26:18objects. Okay, and let's say it's not
  746. 26:20allowed for you to have like any objects
  747. 26:22for that or whatever, then
  748. 26:24what are the things that are not
  749. 26:25dependent on objects? Static stuff.
  750. 26:29Static stuff. Right? So, the variables
  751. 26:31are static and final by default in
  752. 26:34interfaces. I'll talk more about that
  753. 26:36later. Right? So, interfaces mostly
  754. 26:39specify like what the class is doing,
  755. 26:41not how it's doing it. Similar to
  756. 26:43abstract classes is abstract methods,
  757. 26:46but there you cannot do multiple
  758. 26:47inheritance. Okay, because of this
  759. 26:49problem that you you on the screen. Cuz
  760. 26:51the sub class won't be able to choose
  761. 26:53like, you know, which one to pick or
  762. 26:55whatever.
  763. 26:56Cool. So, some of the key differences
  764. 26:58between like a class and an interface,
  765. 27:00uh a class will maintain like some state
  766. 27:02information like through some instance
  767. 27:04variables or whatever, but interface
  768. 27:06cannot.
  769. 27:08Okay?
  770. 27:09That's why the variables in interfaces
  771. 27:10are static and final. You can't change
  772. 27:14those. It's dependent on don't like not
  773. 27:16dependent on object either because
  774. 27:18interfaces do not allow you to have such
  775. 27:20things.
  776. 27:21You know, like your uh function bodies
  777. 27:22or whatever, but it's like a default
  778. 27:24body is allowed. I will tell you more
  779. 27:26about that later, but for now,
  780. 27:28um
  781. 27:29let's just try to imagine that, okay,
  782. 27:31there isn't.
  783. 27:32Okay? But
  784. 27:34for now, let's just imagine there isn't,
  785. 27:36but we'll talk more about that later.
  786. 27:38Okay. So, every every single method,
  787. 27:41everything will be abstract. You cannot
  788. 27:42create
  789. 27:43uh like
  790. 27:45objects of like interfaces and stuff.
  791. 27:47So, obviously, all the
  792. 27:49variables and stuff inside it are like
  793. 27:51final and static. Okay.
  794. 27:54Cool.
  795. 27:55Um
  796. 27:57Yeah, uh interface only has like
  797. 27:59abstract methods, abstract classes
  798. 28:00versus interfaces I'm talking about. So,
  799. 28:02interface has abstract methods, abstract
  800. 28:04classes can have like abstract method as
  801. 28:06well, non-abstract as well.
  802. 28:07Um and from Java 8, it can also have uh
  803. 28:10default and static methods also. I'll
  804. 28:13talk more about the default methods.
  805. 28:14Final variables are what? Final
  806. 28:16variables when variables declared in the
  807. 28:19Java interface, uh they are by default
  808. 28:21final and in the abstract, they it may
  809. 28:24contain final and non-final both.
  810. 28:27Okay?
  811. 28:28Um why are they final? Because if you
  812. 28:30just create a variable in uh
  813. 28:33in an interface, and you know, the
  814. 28:34interface cannot have like a
  815. 28:35constructor, then how will you
  816. 28:36initialize that? That's why they are
  817. 28:38final because final variables always
  818. 28:40needs to be initialized. Okay, there's a
  819. 28:42reason for you. Okay, implementation.
  820. 28:44Abstract class can provide the
  821. 28:45implementation of an interface, but the
  822. 28:47interface can't provide the
  823. 28:48implementation of abstract class. This
  824. 28:50I'll show you in practice. Okay.
  825. 28:53Inheritance versus abstraction. Let's
  826. 28:55talk about that. And I'll also
  827. 28:57definitely give you an example of all
  828. 28:58these points as well. So, an a Java
  829. 29:00interface can be implemented using the
  830. 29:02we use the implements keyword over here.
  831. 29:05And in the abstract class, we use what?
  832. 29:07Extends keyword. We already saw that
  833. 29:09just now. Extends keyword. Here we will
  834. 29:11use the implements keyword.
  835. 29:14Okay.
  836. 29:14The main point where it shines, you can
  837. 29:17implement multiple interfaces together.
  838. 29:22Okay.
  839. 29:22And multiple implementation also means
  840. 29:24that an interface can also extend
  841. 29:26another Java Java interface. That is
  842. 29:29also something I'll show you in
  843. 29:30practice. But an in But an abstract
  844. 29:32class can only extend another Java class
  845. 29:35and like uh only only single one only.
  846. 29:38Right? It can't It can't do multiple
  847. 29:39inheritance or whatever. This is the
  848. 29:40beauty of it. We'll We'll see this in
  849. 29:42action also.
  850. 29:44Okay.
  851. 29:45So, about the accessibility, members of
  852. 29:47the Java interface are public by
  853. 29:49default. And uh the Java abstract class
  854. 29:52can have all kinds like private,
  855. 29:54protected, or whatever.
  856. 29:57Okay. So, these are like sort of like
  857. 29:58the key differences.
  858. 30:00So, as I mentioned already, using like
  859. 30:02interfaces you can specify, you know, a
  860. 30:04set of methods and stuff. And then you
  861. 30:06can be like, "Okay, um they can be now
  862. 30:08implemented by more than one classes."
  863. 30:11Okay. So, they are sort of like it's
  864. 30:13you'll be like, "Hey Kunal, this is
  865. 30:14similar to abstract classes." Okay?
  866. 30:16That's true, but it has an additional
  867. 30:17functionality. A class can implement
  868. 30:19more than one interfaces.
  869. 30:21But on the other hand, a class can only
  870. 30:23inherit a single superclass
  871. 30:26or abstract class or whatever.
  872. 30:28The multiple inheritance is not allowed,
  873. 30:29but here it's allowed.
  874. 30:31Here it's allowed.
  875. 30:34So, using the keyword interface, you can
  876. 30:35fully abstract a class interface from
  877. 30:38its implementation. Classes interface
  878. 30:40from its implementation.
  879. 30:42It means that you can specify what a
  880. 30:43class must do, but not how it does it.
  881. 30:46How it does it will be specified by the
  882. 30:48child classes by overriding the abstract
  883. 30:51methods in the interface.
  884. 30:54Okay?
  885. 30:55That's it.
  886. 30:57In interfaces, you don't have like
  887. 30:58instance variables or whatever. You have
  888. 30:59final final variables, final and static.
  889. 31:03Okay?
  890. 31:04So, obviously, abstract methods declared
  891. 31:06without any body and things like that.
  892. 31:08Yeah.
  893. 31:10So, you can now utilize that one
  894. 31:11interface and multiple methods.
  895. 31:14So, we can use something like this.
  896. 31:17Yeah. Now, this will actually be mak-
  897. 31:20making more sense. I'll take you I'll
  898. 31:21give you like a little bit of real-world
  899. 31:23example as well.
  900. 31:24Okay?
  901. 31:25That's pretty much about it. Let's jump
  902. 31:26into the code and look at all these
  903. 31:28things in action. Okay? So, let's see
  904. 31:30what we're trying to do.
  905. 31:31Let's imagine you work at a factory of a
  906. 31:34car company, right? So,
  907. 31:37you have various cars that have various
  908. 31:39engines. For example, there's some has
  909. 31:41like high-powered engine, diesel engine,
  910. 31:43electric engine. Tesla motors have that,
  911. 31:45right? For example. So, what we're
  912. 31:47trying to do is, let's say
  913. 31:50even though the type of engine is
  914. 31:51different, but every engine has like a
  915. 31:53different functionality, like the way it
  916. 31:55works. Okay? So, an engine class can be
  917. 31:58something like this, let's say.
  918. 32:00Okay? How the engine starts
  919. 32:04is going to be different. How the engine
  920. 32:06stops
  921. 32:07is going to be different. And how it
  922. 32:09accelerates is going to be different.
  923. 32:13Right?
  924. 32:15Similarly, what all things happen? We
  925. 32:16can have
  926. 32:18another thing like
  927. 32:19brake,
  928. 32:20for example.
  929. 32:22Right? We can have a We can have a
  930. 32:23brake. So, for example, if you just uh
  931. 32:27s- brake,
  932. 32:30every car will stop like differently.
  933. 32:32Okay?
  934. 32:33Every car will have something else, like
  935. 32:35let's say it has some media player.
  936. 32:38Right?
  937. 32:39So, you can say media player, this is my
  938. 32:40engine.
  939. 32:42This is let's say my break.
  940. 32:44This is my media player.
  941. 32:47Okay? And in your media player, you're
  942. 32:48going to be like
  943. 32:50hey
  944. 32:51um something like start
  945. 32:56and stop my media player.
  946. 32:59That's it.
  947. 33:02Okay?
  948. 33:04Sound good?
  949. 33:08Cool?
  950. 33:23Okay? That's about it.
  951. 33:27That is it.
  952. 33:31Okay?
  953. 33:32So, the problem over here is that um
  954. 33:36for every car, this is going to be
  955. 33:37different. Okay? For every car, this is
  956. 33:40going to be different. For the petrol
  957. 33:42car, it will be like hey, I start as a
  958. 33:44petrol car, I stop as a petrol car, I'm
  959. 33:46using the petrol car brakes, and I
  960. 33:48accelerate as a petrol car, for example.
  961. 33:50For diesel car, it will be like I start
  962. 33:52as a diesel car, I stop as a diesel car,
  963. 33:54I'm accelerating like a diesel car, and
  964. 33:55I put brakes as a diesel car. So, you
  965. 33:57can be like okay, Kunal, it's like
  966. 33:58different for each and every one. That's
  967. 34:00totally fine.
  968. 34:01Can you maybe like just create these as
  969. 34:03abstract classes and just override these
  970. 34:05methods? And I'm like yeah, sure, we can
  971. 34:07do that.
  972. 34:08But then we cannot utilize the multi
  973. 34:10multiple inheritance that we want to do.
  974. 34:13Right? We can't really utilize that.
  975. 34:16I want a single car. And let's say if
  976. 34:18I'm creating a petrol engine class,
  977. 34:21right? I'm creating a petrol engine
  978. 34:22class or whatever. I want it to utilize
  979. 34:25all these things.
  980. 34:27Let's say all of these things, for
  981. 34:28example.
  982. 34:31Right? And let's say I'm just creating
  983. 34:32one custom car. Okay, just one single
  984. 34:35car.
  985. 34:35That car needs to utilize all of these
  986. 34:37three things.
  987. 34:41In that case, what will you do?
  988. 34:43You can't extend three things at once in
  989. 34:46abstract classes or classes. Multiple
  990. 34:48inheritance is not allowed.
  991. 34:50Right?
  992. 34:51In that case, we can use interfaces.
  993. 34:53So, I can create these as interfaces.
  994. 34:56And then I can create custom classes
  995. 34:57from it.
  996. 34:59Let's see this in action. See this in
  997. 35:01action. So, I can just create a new
  998. 35:04package over here.
  999. 35:06I can say
  1000. 35:09inter
  1001. 35:10faces.
  1002. 35:12Okay? I can create, let's say, what did
  1003. 35:15we have over here? I can create one
  1004. 35:17called engine.
  1005. 35:18No problem. So, I'll not create a class.
  1006. 35:20I'll actually create an interface.
  1007. 35:23An interface
  1008. 35:25known as engine.
  1009. 35:27So, you can see the definition, public
  1010. 35:29interface engine.
  1011. 35:30Okay?
  1012. 35:32This is going to have what?
  1013. 35:33This is going to have something like
  1014. 35:35start, stop, and accelerate. Okay? No
  1015. 35:38problem.
  1016. 35:39So, I can say void start,
  1017. 35:44void
  1018. 35:45stop,
  1019. 35:47void accelerate.
  1020. 35:49Something like that.
  1021. 35:51Okay?
  1022. 35:53Cool?
  1023. 35:56Any variable you need to have, let's
  1024. 35:57say, int um something like price
  1025. 36:01is equal to has to be final, right? Is
  1026. 36:04that too
  1027. 36:0578,000 or whatever.
  1028. 36:08Okay? This is by default what? Static
  1029. 36:10and final.
  1030. 36:15Okay, that's why it's blurred out
  1031. 36:16because it's like, "Hey, you don't have
  1032. 36:17to write this. It's already
  1033. 36:19like that."
  1034. 36:22Okay?
  1035. 36:23What is the next thing you need?
  1036. 36:24Next thing you need is like, let's say,
  1037. 36:26a break, right?
  1038. 36:28So, break basically means just like a
  1039. 36:30new interface.
  1040. 36:35Break.
  1041. 36:37That's it. It's going to say break.
  1042. 36:41Void
  1043. 36:44break.
  1044. 36:45b r a k e
  1045. 36:47Something like that.
  1046. 36:49Okay?
  1047. 36:51You have another one called
  1048. 36:53media. So, you can imagine these as like
  1049. 36:55just abstract classes. But, the only
  1050. 36:56difference is
  1051. 36:58I've already explained the difference,
  1052. 37:00right? What the difference is between
  1053. 37:01these.
  1054. 37:02Let's create one more called media.
  1055. 37:06Media. Okay? Now, it will contain let's
  1056. 37:09say the same ones, start and stop.
  1057. 37:12Like this, for example.
  1058. 37:15Start and stop.
  1059. 37:18Okay?
  1060. 37:20Something like that.
  1061. 37:25Cool?
  1062. 37:27Sound good?
  1063. 37:32Okay?
  1064. 37:34So, let's say you want to create a car
  1065. 37:36now.
  1066. 37:37So, let's say you want to create a Java
  1067. 37:38class. You create a car. A car needs to
  1068. 37:41have let's say these two
  1069. 37:42functionalities, break and engine.
  1070. 37:46So, previously you were not able to do
  1071. 37:48it.
  1072. 37:49If you need both of these
  1073. 37:50functionalities, you were not able to
  1074. 37:51add it in one single one.
  1075. 37:53Because you cannot implement two files
  1076. 37:55into one.
  1077. 37:56Multiple inheritance not allowed.
  1078. 37:59You can't You weren't able to do it like
  1079. 38:01this previously, were you? No, right?
  1080. 38:04If you want to make a car like this.
  1081. 38:06Now, you can with the implements
  1082. 38:07keyword. Implements, now you can put as
  1083. 38:10many classes as you want.
  1084. 38:12Engine
  1085. 38:13and break. Let's say I only want two.
  1086. 38:16Obviously, these are abstract methods,
  1087. 38:18so you would have to implement those. No
  1088. 38:19problem, I I do it.
  1089. 38:21Implement methods like this.
  1090. 38:25So, you can say
  1091. 38:28I break
  1092. 38:29like a normal
  1093. 38:33car.
  1094. 38:37I start like a normal car.
  1095. 38:41Okay?
  1096. 38:42I stop like a normal car.
  1097. 38:45Okay, and I accelerate.
  1098. 38:51What is the spelling? Accelerate.
  1099. 38:54Like a normal car.
  1100. 38:56Were you able to do this previously?
  1101. 38:58Putting two
  1102. 39:00two inheriting the properties of more
  1103. 39:02than one classes into one. Were you able
  1104. 39:04to do this before? No. Now you can.
  1105. 39:08Even if this had a start over here,
  1106. 39:10let's see.
  1107. 39:12Would it really matter? Because it is
  1108. 39:14already adding its own value, own
  1109. 39:17function body.
  1110. 39:19Right? It wouldn't matter.
  1111. 39:21Right? If there was like a start over
  1112. 39:23here, void start or something like this.
  1113. 39:25Would it matter much? No, because it's
  1114. 39:27providing its own body. Does not care
  1115. 39:29which one it's overriding. It's just
  1116. 39:31overriding, we know that.
  1117. 39:33Okay?
  1118. 39:35So, it wouldn't matter.
  1119. 39:38So, are you able to see how the conflict
  1120. 39:39resolution is also resolved? This was
  1121. 39:41being resolved in abstract classes also,
  1122. 39:42but like in that this multiple
  1123. 39:44inheritance was not allowed. Here it's
  1124. 39:46allowed.
  1125. 39:48Create a main file.
  1126. 39:50Java class.
  1127. 39:52Main.
  1128. 39:54Okay?
  1129. 39:56Something like this. You can say public
  1130. 39:57static void main.
  1131. 40:00Right?
  1132. 40:02You can say my
  1133. 40:04car car is equal to new car.
  1134. 40:08Okay? Something like that. And then you
  1135. 40:09do
  1136. 40:10car dot um
  1137. 40:13accelerate.
  1138. 40:15Then say car dot start.
  1139. 40:18Car dot stop or whatever.
  1140. 40:21It's going to run what? What is this
  1141. 40:22thing? Overriding already explained.
  1142. 40:24You already know what overriding is.
  1143. 40:26Dynamic method dispatch, everything you
  1144. 40:27know. How it will handle it, how will it
  1145. 40:29determine, everything I've covered in
  1146. 40:31the overriding oops property lecture.
  1147. 40:33All right? Please watch that without
  1148. 40:35asking doubts because I've already
  1149. 40:36covered that. I want this to be the best
  1150. 40:38course on YouTube for data structures
  1151. 40:40algorithms and object programming and
  1152. 40:41interview preparation. Hence, I'm not
  1153. 40:43skipping anything. I've already
  1154. 40:44explained this in detail. Okay? Check
  1155. 40:46out the links in the description. So.
  1156. 40:48How which particular method to run it's
  1157. 40:50actually determining on at run time.
  1158. 40:53At run time.
  1159. 40:57Okay?
  1160. 40:58So.
  1161. 40:59That's pretty much about it.
  1162. 41:02Okay? One more thing I'd like to mention
  1163. 41:04is that when we use classes, right? I
  1164. 41:06told you that the parent class has no
  1165. 41:08idea about child classes, but the child
  1166. 41:10class has idea about parent classes.
  1167. 41:12Okay? So, when you are calling one
  1168. 41:15function from a child class and that
  1169. 41:18function, let's say, exists in the
  1170. 41:19parent class. Then the parent class and
  1171. 41:22child class both have to be present at
  1172. 41:25the compile time.
  1173. 41:26So, that the Java compiler can check
  1174. 41:28that everything is compatible or
  1175. 41:30everything.
  1176. 41:31Okay? Simple stuff. Makes sense. Cool.
  1177. 41:33So, this way for more functions to be
  1178. 41:36available to child classes, functions
  1179. 41:39get pushed up higher and higher in the
  1180. 41:41parent classes.
  1181. 41:43Okay? And
  1182. 41:45this is basically um
  1183. 41:47a problem that interfaces solve. Because
  1184. 41:50they disconnect the definition
  1185. 41:53of a method from the this hierarchy of
  1186. 41:57inheritance. Okay? Because you are
  1187. 41:59implementing it in the
  1188. 42:01subclass only, the main body.
  1189. 42:04Okay?
  1190. 42:06So, interfaces are not like in the
  1191. 42:08hierarchy of classes. Okay?
  1192. 42:11Uh it's totally possible for like
  1193. 42:12classes that are
  1194. 42:14two classes let's say that are unrelated
  1195. 42:15to each other in terms of who their
  1196. 42:17parents are or whatever.
  1197. 42:20They can implement the same interface.
  1198. 42:22This is where the power of interfaces
  1199. 42:24shines.
  1200. 42:26Okay.
  1201. 42:28Remember I told you you can go up but
  1202. 42:30you can't go down like this, right? In
  1203. 42:32that way makes sense to put
  1204. 42:34functions that are being used by
  1205. 42:35multiple classes up and up and up.
  1206. 42:37Interface does not really care about
  1207. 42:39what the inter what like whether two
  1208. 42:41classes are related or not or whatever
  1209. 42:42like that. It does not really care.
  1210. 42:44But in normal inheritance it does care.
  1211. 42:48Right? Two classes are
  1212. 42:50inherited or not, right?
  1213. 42:52Like we already discussed like you can
  1214. 42:54you have two child classes you can't
  1215. 42:55really go like this. You can only go up.
  1216. 42:57Interface it does not really care.
  1217. 42:59Two classes that are basically unrelated
  1218. 43:01to each other, right? They can also
  1219. 43:02implement the same interface.
  1220. 43:05Is what I'm saying.
  1221. 43:08This is awesome.
  1222. 43:10As simple as that.
  1223. 43:13Two classes that are unrelated to each
  1224. 43:14other can also implement the same
  1225. 43:16interface.
  1226. 43:18Okay.
  1227. 43:21Cool.
  1228. 43:22So this is dynamic method dispatch
  1229. 43:24method or like override overriding and
  1230. 43:26stuff. It works exactly the same way
  1231. 43:28like dynamic method dispatch that we
  1232. 43:29taught previously. At run time it will
  1233. 43:31decide which one to run. That obviously
  1234. 43:33depends on the type of this.
  1235. 43:36Okay. Can you create
  1236. 43:37like one more thing I would like to
  1237. 43:38mention is that uh
  1238. 43:41Can you actually add like uh
  1239. 43:43type of the variable as the type of the
  1240. 43:45interface like we can with abstract
  1241. 43:47classes and parent classes.
  1242. 43:49Yes, you can.
  1243. 43:52We are able to do it.
  1244. 43:54Again same same rules will apply then.
  1245. 43:57If you have something that is inside
  1246. 43:58this class you will not be able to
  1247. 43:59access that.
  1248. 44:01int a is equal to 30 or something like
  1249. 44:03that.
  1250. 44:05car.a
  1251. 44:07You will not be able to access that.
  1252. 44:09Why? Because already explained a
  1253. 44:11thousand times by me. What things you
  1254. 44:14access depends on this, which one of
  1255. 44:16those versions to access depends on
  1256. 44:18this. Overriding and this is just
  1257. 44:21putting reference variables as parent
  1258. 44:23classes. Already explained previously
  1259. 44:24with examples.
  1260. 44:26Okay? If you did not understand this
  1261. 44:28means I I can't teach you
  1262. 44:30object-oriented programming because you
  1263. 44:31skipped all the previous lectures.
  1264. 44:33This is a very basic stuff that I
  1265. 44:34already explained multiple times I even
  1266. 44:36pointed it out very clearly. We spent
  1267. 44:38like so much time on this.
  1268. 44:40You know, I told you that story also I
  1269. 44:42told you, which version to access will
  1270. 44:44be mentioned by the object, what to
  1271. 44:46access will be mentioned by this. It's
  1272. 44:48type engine so engine access it can only
  1273. 44:50access these things.
  1274. 44:52It cannot access what is inside the car
  1275. 44:54even though the object is of car type.
  1276. 44:57But
  1277. 44:57which one to actually access depends on
  1278. 45:00the type of the object. Type of the
  1279. 45:01object is car. So when you do acc, it
  1280. 45:04will actually access the car type's acc.
  1281. 45:07This one.
  1282. 45:09Okay?
  1283. 45:11Make sense? So you can definitely do
  1284. 45:13this. Okay? And since we've already
  1285. 45:14covered this thing like
  1286. 45:16overriding and dynamic method dispatch
  1287. 45:18and everything, it's the same same thing
  1288. 45:20that is going on over here but this
  1289. 45:22dynamic lookup of like the method at
  1290. 45:24runtime which we've already covered many
  1291. 45:26many times.
  1292. 45:27Uh it's it's like a it has a little bit
  1293. 45:29of an overhead when compared to like the
  1294. 45:31normal method invocation in Java like
  1295. 45:32how normal methods work. That is why you
  1296. 45:35should be careful not to use interfaces
  1297. 45:37casually in performance-critical code.
  1298. 45:41Okay, that is something I wanted to
  1299. 45:42point out.
  1300. 45:44Okay, because it happens at runtime.
  1301. 45:46Right?
  1302. 45:47Okay, let's move forward. Okay, now let
  1303. 45:49me show you one more thing.
  1304. 45:50Let me show you something like uh if I
  1305. 45:52have a car over here and let's say it
  1306. 45:54extends um
  1307. 45:57media also.
  1308. 45:58Okay? Now media also contains start and
  1309. 46:01stop and uh engine also contains start
  1310. 46:04and stop. And this car also then has
  1311. 46:06start and stop.
  1312. 46:08Okay? I like I start like a I start
  1313. 46:10engine
  1314. 46:12like a normal car. I stop engine like a
  1315. 46:15normal car. Now when I say stop, let's
  1316. 46:17see what will happen.
  1317. 46:19Okay? Something like this.
  1318. 46:21Let's say I have my car. Let's say I
  1319. 46:23create let's say something else. I say
  1320. 46:25something like
  1321. 46:28you know, my media. Let's say I want to
  1322. 46:30create a media type. Okay? For example.
  1323. 46:32Media, my car media. Okay? This is a
  1324. 46:35class for my reference variable for my
  1325. 46:37car's radio or whatever. Is equal to new
  1326. 46:40car or something like that.
  1327. 46:42Right? And then I say car media.stop.
  1328. 46:47Let's see what will happen.
  1329. 46:55So it actually stopped the engine. Now
  1330. 46:57would how would it how weird would it
  1331. 46:59be? You're driving your car. You try to
  1332. 47:01stop the car's in car's music player and
  1333. 47:03the engine stops.
  1334. 47:07That's what's happening over here.
  1335. 47:09Okay? Similarly, if you want to change
  1336. 47:11the engine over here,
  1337. 47:12okay? You want to change the engine of
  1338. 47:14this thing, then what would you have to
  1339. 47:16do? You would have to create a new
  1340. 47:17object of this. You would have to change
  1341. 47:18the entire car.
  1342. 47:21Let's say you go to your mechanic and
  1343. 47:22you're like, "Hey, I want to actually
  1344. 47:23change the engine." He's like, "Hey, you
  1345. 47:24want to change engine? You have to
  1346. 47:26change the entire car." Or seats. Let's
  1347. 47:28say you want to change the seats and
  1348. 47:29then you answer you have to change the
  1349. 47:31entire seat.
  1350. 47:32There's a little bit of problem over
  1351. 47:33here.
  1352. 47:35Okay?
  1353. 47:36That's why we can create separate
  1354. 47:37classes for these interfaces as well.
  1355. 47:40Okay?
  1356. 47:41Let's see how we can do that. So I'll
  1357. 47:42create like the separate classes. First
  1358. 47:44class I'll create as let's for my
  1359. 47:46CD player. So my
  1360. 47:49CD player.
  1361. 47:51Okay? Something like that. It implements
  1362. 47:53what? It only implements media, let's
  1363. 47:54say. Implements media. Okay?
  1364. 48:00Okay? So, I can say
  1365. 48:07rock
  1366. 48:08music or something like I can say
  1367. 48:12music
  1368. 48:13start.
  1369. 48:14Stop means
  1370. 48:18music stop.
  1371. 48:20Okay, it's only implementing media for
  1372. 48:22example right now.
  1373. 48:24Okay? Let's say I create another engine.
  1374. 48:26I can say I create engine I can say um
  1375. 48:32power engine.
  1376. 48:35Okay, I can create a power engine. It
  1377. 48:37implements all the things that are in
  1378. 48:39the engine.
  1379. 48:40For example,
  1380. 48:43implement the methods.
  1381. 48:46Power engine
  1382. 48:49start.
  1383. 48:54Power engine
  1384. 48:56stop.
  1385. 48:57Power engine accelerate.
  1386. 49:01Okay?
  1387. 49:03Something like that.
  1388. 49:05Good.
  1389. 49:07And then I can create let's say a
  1390. 49:11electric engine as well.
  1391. 49:13Java class
  1392. 49:14electric
  1393. 49:16engine.
  1394. 49:19Electric engine
  1395. 49:21implements
  1396. 49:22engine.
  1397. 49:25Like this.
  1398. 49:26Okay.
  1399. 49:28I can say electric
  1400. 49:33electric engine start.
  1401. 49:39Okay?
  1402. 49:40Electric engine
  1403. 49:41stop.
  1404. 49:42Electric engine
  1405. 49:44accelerate.
  1406. 49:46Okay?
  1407. 49:47Something like that.
  1408. 49:49Now I can create my nice car.
  1409. 49:55Nice car.
  1410. 49:57Okay, this class this car is going to
  1411. 50:00have an internal engine of its own. That
  1412. 50:02is going to be a what type engine type?
  1413. 50:05That's it.
  1414. 50:07Okay, cuz constructor uh I will not
  1415. 50:09select anything.
  1416. 50:11I'll just say that by default I'm going
  1417. 50:13to say let's say engine is equal to
  1418. 50:16new uh power engine for example at
  1419. 50:19first. So, I use these interfaces and
  1420. 50:22everything in diff- different classes,
  1421. 50:23but overall class I'm using it like
  1422. 50:26this.
  1423. 50:27Now, you can add anything you want. If
  1424. 50:29you want to add an engine of your own,
  1425. 50:31so you can just do it like this.
  1426. 50:34That's it.
  1427. 50:37Okay?
  1428. 50:39You can add a start over here. I can say
  1429. 50:41public start Notice how I put this as
  1430. 50:43private. Data hiding, right? Covered
  1431. 50:46that previously.
  1432. 50:48Public void start, it's just going to
  1433. 50:50say engine.start.
  1434. 50:53Now, if this engine that is initialized
  1435. 50:55is power engine, it will call this. If
  1436. 50:58it's electric engine, it will call this.
  1437. 51:01How cool is that?
  1438. 51:03Okay? Similarly, I can do stop.
  1439. 51:09engine.stop
  1440. 51:10No problem.
  1441. 51:12Okay? Similarly, I can do
  1442. 51:14um
  1443. 51:16something like
  1444. 51:18um
  1445. 51:19start music. It's going to say
  1446. 51:24What? I need to have a music player as
  1447. 51:26well. So, then I'll say this car has an
  1448. 51:28engine. This car also has a music
  1449. 51:31player. So, I can say my
  1450. 51:34um
  1451. 51:36media
  1452. 51:37um player
  1453. 51:40something like that.
  1454. 51:42Right?
  1455. 51:43Um so, I can say this is equal to new CD
  1456. 51:46player.
  1457. 51:47Something like that.
  1458. 51:50Okay, start music. Now I'll not say
  1459. 51:52engine.start. This is the problem we
  1460. 51:53were facing previously. When you did
  1461. 51:57in the main car, when you were saying
  1462. 51:58start or stop with even the if even the
  1463. 52:01type of this was music like over here,
  1464. 52:03type was music and you were saying start
  1465. 52:05or stop, it was actually starting and
  1466. 52:06stopping the engine. Now when I say
  1467. 52:08start or stop, start music, it's
  1468. 52:10actually not going to say engine.start.
  1469. 52:12It's going to say
  1470. 52:14player.start.
  1471. 52:16Similarly, I can say stop music.
  1472. 52:22player.stop.
  1473. 52:27Okay.
  1474. 52:28Similarly, you can say upgrade your
  1475. 52:30engine. So you can say public
  1476. 52:32void
  1477. 52:33um
  1478. 52:36upgrade
  1479. 52:38engine and you pass an engine over here.
  1480. 52:41Okay, engine engine.
  1481. 52:43And you can just say
  1482. 52:45what?
  1483. 52:46You can say this.engine
  1484. 52:49is equal to the engine that I have
  1485. 52:50passed. That's it.
  1486. 52:52How cool is that? You can change your
  1487. 52:54electric to electric engine from power
  1488. 52:55engine or whatever.
  1489. 52:57Makes sense? Let's try to do it.
  1490. 53:00So you have your
  1491. 53:02uh
  1492. 53:03car,
  1493. 53:04which is your nice car.
  1494. 53:06Nice car. Car is equal to new nice car.
  1495. 53:10Okay, if I say car.start
  1496. 53:14and then I say car.start music.
  1497. 53:17So you can see it will be like I am
  1498. 53:18starting and by default the engine is
  1499. 53:21going to be what? I just called the
  1500. 53:22default one, so power engine. It will be
  1501. 53:24like power engine starts.
  1502. 53:25And then it will be like music starts.
  1503. 53:28Okay, here also it's running the start
  1504. 53:30one. Here is also running the start one,
  1505. 53:32but here it's running the player.start.
  1506. 53:34Here it's running the engine.start.
  1507. 53:37You can also change the engine.
  1508. 53:39So if I change my engine, I can say
  1509. 53:41car.dot upgrade engine
  1510. 53:45and um
  1511. 53:49I can just add a new engine over here.
  1512. 53:54I'll have to create a new engine or I
  1513. 53:55can just upgrade my engine in the car
  1514. 53:57only.
  1515. 53:57Okay? I can say upgrade my engine.
  1516. 54:00And I'm going to say, let's say I'm not
  1517. 54:02passing it directly. I'm just saying
  1518. 54:04upgrade my engine is equal to what?
  1519. 54:06This is equal to new electric engine.
  1520. 54:10Something like that.
  1521. 54:12Right?
  1522. 54:13So now you're going to upgrade my engine
  1523. 54:14and then I will if I start my car again,
  1524. 54:17that's going to be like now I'm running
  1525. 54:18electric engine.
  1526. 54:22Electric engine started.
  1527. 54:24Without creating a new object, you were
  1528. 54:25able to create you were able to just
  1529. 54:27change the part of the engine.
  1530. 54:31Okay? As compared to this,
  1531. 54:33here you would have to change the entire
  1532. 54:35car in order to change the engine.
  1533. 54:37Stopping the CD player was actually
  1534. 54:39stopping the entire car.
  1535. 54:44Okay?
  1536. 54:45Hopefully you understand interfaces now.
  1537. 54:49Right? I'll share all the notes and
  1538. 54:51everything and I I covered everything
  1539. 54:52like that I mentioned.
  1540. 54:54Right?
  1541. 54:56That's basically about it.
  1542. 54:58Right?
  1543. 54:59Cool.
  1544. 55:01One more thing you can do is interfaces
  1545. 55:02you can also extend.
  1546. 55:04Right? So you can say
  1547. 55:07create another package
  1548. 55:09extend demo.
  1549. 55:12So here you can create two interfaces,
  1550. 55:13Java interface A.
  1551. 55:16And you can create another one, let's
  1552. 55:17say
  1553. 55:19interface B.
  1554. 55:22Okay? Something like that. And here if
  1555. 55:24you have something like um
  1556. 55:28void fun.
  1557. 55:30Something like that. And here in B, I've
  1558. 55:32also already talked about, right? The
  1559. 55:34the variables and everything I already
  1560. 55:35talked about what type of variables are
  1561. 55:37available in interfaces and stuff. We
  1562. 55:39didn't skip that. Static and final,
  1563. 55:40right?
  1564. 55:41So, yeah.
  1565. 55:43Um here you can say extends A.
  1566. 55:47And here you can have void
  1567. 55:50fun for example if you have then
  1568. 55:52that's like not going to work because
  1569. 55:54it's like the same name. So, it does not
  1570. 55:56really matter.
  1571. 55:57Um
  1572. 55:58Instead we can have something like
  1573. 55:59greet.
  1574. 56:01Okay, now let's create a class.
  1575. 56:04Um main class that implements B.
  1576. 56:09Then you would have to implement both
  1577. 56:11the methods.
  1578. 56:12Greet as well and fun as well. If B is
  1579. 56:15extending A, you can imagine that it
  1580. 56:17looks something like this. You can
  1581. 56:18imagine that it looks something like
  1582. 56:20this.
  1583. 56:23Okay? So, you can extend it as well. All
  1584. 56:26of these properties will be like
  1585. 56:27together combined.
  1586. 56:29And here you can then use it. Both the
  1587. 56:32ones you would have to implement.
  1588. 56:34You would implement the abstract methods
  1589. 56:35of B as well and the class the interface
  1590. 56:38that B is inheriting from that also
  1591. 56:40something you would have to
  1592. 56:42um do it. So, you can see I'm using
  1593. 56:44extends over here not implements.
  1594. 56:46Implements is only for classes to
  1595. 56:47interfaces. Interface to interface
  1596. 56:49inheritance it is extends.
  1597. 56:55Okay?
  1598. 56:59Cool.
  1599. 57:01So, that's about interfaces. Now, before
  1600. 57:03we move on to other thing like default
  1601. 57:05interface methods that are like after
  1602. 57:07Java 8 they were provided and like JDK 8
  1603. 57:10and some in inter interface inner
  1604. 57:12interfaces and stuff static stuff in
  1605. 57:14this how static works over here which is
  1606. 57:16pretty self-explanatory, but uh let's
  1607. 57:18look at one more thing which is
  1608. 57:20something that I wanted to share with
  1609. 57:21you about this thing. Annotation.
  1610. 57:24So, annotation also internally you can
  1611. 57:25see is an interface.
  1612. 57:28If you want to create your own
  1613. 57:29annotation you can do it via this. At
  1614. 57:32interface.
  1615. 57:35Okay, it itself has some annotations.
  1616. 57:37Target and retention or something like
  1617. 57:38that.
  1618. 57:40Okay?
  1619. 57:43Cool.
  1620. 57:46So, enums and stuff we'll look into the
  1621. 57:47next lecture.
  1622. 57:48But, here you can see that in the the
  1623. 57:52this thing the annotations that we
  1624. 57:54talked about, the checks basically, I
  1625. 57:56explained this in the overriding lecture
  1626. 57:58what this actually does. Um that is also
  1627. 58:00internally an interface only. Interface.
  1628. 58:03Okay? Public interface like this.
  1629. 58:08Public interface override.
  1630. 58:11Okay?
  1631. 58:11That's what I want to share. Like
  1632. 58:12internally override is also an
  1633. 58:14interface. Now, starting from Java 8,
  1634. 58:16there's actually possibility to provide
  1635. 58:19some default implementation.
  1636. 58:22Okay? To interfaces. Let me close
  1637. 58:24everything. Let me just put this over
  1638. 58:25here.
  1639. 58:27So, default implementation means
  1640. 58:29something like this. Let me just copy
  1641. 58:30paste this.
  1642. 58:36Extend demo two.
  1643. 58:44Um rename.
  1644. 58:49Factor.
  1645. 58:56Okay?
  1646. 58:58So, here let's say I'm not extending it.
  1647. 59:02Um close all.
  1648. 59:06Let's say not extending it and just
  1649. 59:08having it like normally like
  1650. 59:11void
  1651. 59:12fun or whatever.
  1652. 59:14Okay? And then I have a main class
  1653. 59:15implements
  1654. 59:16A {comma} B both, let's say.
  1655. 59:18Right?
  1656. 59:19Cool.
  1657. 59:20So,
  1658. 59:21starting from JDK 8, you can actually
  1659. 59:24provide some default implementation. You
  1660. 59:26just put default over here.
  1661. 59:28Like this.
  1662. 59:32I am in A.
  1663. 59:35Okay? Similarly, you can provide default
  1664. 59:38if you just run it like uh
  1665. 59:40you just remove it like this. Right? So,
  1666. 59:43here it's actually saying that uh okay,
  1667. 59:45for implementing for B, let me just
  1668. 59:47comment this out right now. So, even
  1669. 59:49though
  1670. 59:50there's an abstract method over here
  1671. 59:51that I have like not really, you know,
  1672. 59:54um
  1673. 59:57it overridden over there's a method in
  1674. 59:58this interface that I have not
  1675. 1:00:00overridden, and it's not giving me an
  1676. 1:00:01error because there's a default
  1677. 1:00:02available over here.
  1678. 1:00:04Okay, but what if I try to put the
  1679. 1:00:05default over here as well?
  1680. 1:00:07Now, we're getting an error.
  1681. 1:00:10Okay?
  1682. 1:00:11It's saying like it's collapsing. So,
  1683. 1:00:13the initial problem that you're trying
  1684. 1:00:14to solve, now we're getting it with
  1685. 1:00:16this.
  1686. 1:00:17So,
  1687. 1:00:19um
  1688. 1:00:20this is basically saying that it's a
  1689. 1:00:22it's a it's just a special use feature.
  1690. 1:00:24Okay? And the the original intent behind
  1691. 1:00:26the interface still remains the same.
  1692. 1:00:28The primary motivation Kunal Kunal, why
  1693. 1:00:30would we want to do this then if it's
  1694. 1:00:32giving us the same problem that we used
  1695. 1:00:34interfaces to resolve?
  1696. 1:00:36So, it's basically, you know, the
  1697. 1:00:37primary motivation for this default
  1698. 1:00:39method was to provide a means by which
  1699. 1:00:41interfaces could be expanded without
  1700. 1:00:44breaking the existing code.
  1701. 1:00:46So, for example, you add another method
  1702. 1:00:48without a body in an interface.
  1703. 1:00:50Then you would have to provide the body
  1704. 1:00:52of that method in all the classes that
  1705. 1:00:54implement that interface.
  1706. 1:00:57Does that make sense?
  1707. 1:00:59Okay?
  1708. 1:01:00So, for example, you may have one class
  1709. 1:01:02that interme- in like in this class,
  1710. 1:01:04it's implementing two interfaces. Okay?
  1711. 1:01:07If each of these interfaces provide some
  1712. 1:01:09default methods, then I'm getting some
  1713. 1:01:11error.
  1714. 1:01:13Okay?
  1715. 1:01:16And obviously, the class's
  1716. 1:01:17implementation will obviously be the
  1717. 1:01:18priority over an interface default
  1718. 1:01:20method.
  1719. 1:01:25Okay, and we already got an error in
  1720. 1:01:27case there were like two overridden
  1721. 1:01:28methods.
  1722. 1:01:30Okay.
  1723. 1:01:31Cool.
  1724. 1:01:35That's basically like the primary use
  1725. 1:01:36case. Okay?
  1726. 1:01:41So, you can expand the interfaces
  1727. 1:01:42without breaking like some existing code
  1728. 1:01:44or whatever.
  1729. 1:01:45But, I would not like recommend using
  1730. 1:01:47it.
  1731. 1:01:47Um
  1732. 1:01:48if you're getting some errors or
  1733. 1:01:49anything like that, don't.
  1734. 1:01:51Okay?
  1735. 1:01:52But, if you have like multiple names,
  1736. 1:01:54then don't. If you don't have multiple
  1737. 1:01:56names, then you can.
  1738. 1:01:58It's like an additional feature.
  1739. 1:02:01Okay?
  1740. 1:02:03Cool. Let's move forward. Okay, now
  1741. 1:02:05let's talk about one more important
  1742. 1:02:06thing, which is uh static methods over
  1743. 1:02:08here.
  1744. 1:02:09We know that static methods do not get
  1745. 1:02:12inherited. We know that.
  1746. 1:02:15Okay? You know static methods will never
  1747. 1:02:16get inherited. Why? We already covered
  1748. 1:02:18it in overriding stuff and
  1749. 1:02:21in the second video of object-oriented
  1750. 1:02:22programming programming principles. It's
  1751. 1:02:24pretty self-explanatory as well.
  1752. 1:02:26Inheritance means like if you inherit
  1753. 1:02:27it, you have to override. Overriding
  1754. 1:02:29means it depends on objects because it's
  1755. 1:02:31runtime polymorphism, and static methods
  1756. 1:02:33do not depend on objects. That's why
  1757. 1:02:34they cannot be inherited. Simple
  1758. 1:02:36explanation.
  1759. 1:02:37So, if you have static method over here,
  1760. 1:02:40and they cannot be inherited, it means
  1761. 1:02:42that they cannot be overridden. It means
  1762. 1:02:44they should have a body over here.
  1763. 1:02:46Because the static methods in interfaces
  1764. 1:02:48are the ones that will be used only. You
  1765. 1:02:50can't override those or whatever. You
  1766. 1:02:51can't inherit those. It will be used
  1767. 1:02:53from interface only.
  1768. 1:02:55That's why static interface methods
  1769. 1:02:58should always have a body.
  1770. 1:03:06All these reasons because you can't
  1771. 1:03:09static
  1772. 1:03:10void
  1773. 1:03:11greeting.
  1774. 1:03:15So, you can't have it something like
  1775. 1:03:16this. This is not allowed. Okay? Body is
  1776. 1:03:18required.
  1777. 1:03:21Hey, I am static method.
  1778. 1:03:24You can call it via whatever you want.
  1779. 1:03:27Okay?
  1780. 1:03:31Main
  1781. 1:03:33object is equal to new main.
  1782. 1:03:36Okay? And here you can call it via just
  1783. 1:03:37the name of the class because we know
  1784. 1:03:39static methods are called by the name of
  1785. 1:03:41the class. So, it's actually going to be
  1786. 1:03:43like it's not going to be inherited or
  1787. 1:03:44anything. A. greeting. Like that you
  1788. 1:03:47have to call it. You have to call static
  1789. 1:03:49and you have to call these via the stat
  1790. 1:03:51via the interface name. Okay?
  1791. 1:03:55Call via the
  1792. 1:03:56interface name.
  1793. 1:03:58Simple as that. No problem.
  1794. 1:04:00That's it.
  1795. 1:04:03All right?
  1796. 1:04:05Cool.
  1797. 1:04:06One more thing I want to share is
  1798. 1:04:09when you are overriding methods, right?
  1799. 1:04:11When you're overriding like this.
  1800. 1:04:12Here I know it's like uh
  1801. 1:04:16abstract and whatever. But uh
  1802. 1:04:19the the overridden methods, this is not
  1803. 1:04:22just for interfaces. It's like in
  1804. 1:04:23general. The access modifier for the
  1805. 1:04:26overridden methods should be same or
  1806. 1:04:28better. So, for example, if the parent
  1807. 1:04:30class, we talk about the previous
  1808. 1:04:32inheritance, if the parent class uh if
  1809. 1:04:34in the parent class the method was let's
  1810. 1:04:37say protected, then in the overriding
  1811. 1:04:40the overridden method should be
  1812. 1:04:42protected or something better like
  1813. 1:04:44public. It cannot be more restrictive
  1814. 1:04:47than protected. It cannot be private.
  1815. 1:04:50Okay? Should be better than that.
  1816. 1:04:53If I try to put this as private, see
  1817. 1:04:54what happens. Getting an error.
  1818. 1:04:57Right? Because I know
  1819. 1:04:59that uh
  1820. 1:05:04I put this over here, public. We know
  1821. 1:05:06this is public and abstract by default.
  1822. 1:05:09So, you can have public and better than
  1823. 1:05:11public, but no, nothing is better than
  1824. 1:05:12public.
  1825. 1:05:14Right?
  1826. 1:05:15So, that's the reason we have You can't
  1827. 1:05:17add this public. If these were like
  1828. 1:05:19protected or something, you can have
  1829. 1:05:20protected and something better than
  1830. 1:05:22protected. Nothing less
  1831. 1:05:24Nothing less restrictive can be
  1832. 1:05:26available over here.
  1833. 1:05:29Okay.
  1834. 1:05:38Key class
  1835. 1:05:45Cool. Sound good?
  1836. 1:06:00Same thing
  1837. 1:06:01because
  1838. 1:06:02this is default modifier and that is
  1839. 1:06:04also less restrictive, not allowed.
  1840. 1:06:07Okay?
  1841. 1:06:09So, that's one more rule I wanted to
  1842. 1:06:10share about.
  1843. 1:06:14Cool.
  1844. 1:06:16Let's move up, okay? One more thing
  1845. 1:06:17about interfaces I want to share is like
  1846. 1:06:19nested interfaces, okay?
  1847. 1:06:21Package
  1848. 1:06:23nested nest it.
  1849. 1:06:27So, let's say you have a class called A
  1850. 1:06:29and inside that class you have an
  1851. 1:06:31interface
  1852. 1:06:33nested
  1853. 1:06:35interface.
  1854. 1:06:36You have something like
  1855. 1:06:38public
  1856. 1:06:40interface
  1857. 1:06:42you know, something like um
  1858. 1:06:47nested
  1859. 1:06:50interface
  1860. 1:06:53something like that.
  1861. 1:06:55Okay?
  1862. 1:06:57Cool.
  1863. 1:06:59We have let's say a function called is
  1864. 1:07:03um odd or even.
  1865. 1:07:05The a is odd or even and you just pass a
  1866. 1:07:08number over here.
  1867. 1:07:10Okay, that's it.
  1868. 1:07:13Now, you have a let's say a class B
  1869. 1:07:15and that implements A. nested interface.
  1870. 1:07:20You'll have to obviously override
  1871. 1:07:21like this.
  1872. 1:07:23Okay, so you can say what is an odd
  1873. 1:07:25number? An odd number is if the number
  1874. 1:07:27and with one
  1875. 1:07:29is equal to equal to one.
  1876. 1:07:30We have covered this in bitwise
  1877. 1:07:31operators.
  1878. 1:07:33Okay, that is pretty much about it. Yep.
  1879. 1:07:38Now, I can create another class. You can
  1880. 1:07:39create a main class if you want.
  1881. 1:07:41Right? So, I can create a class main
  1882. 1:07:44public static, or I can create it
  1883. 1:07:46separately also. It does not
  1884. 1:07:48really matter.
  1885. 1:07:52Main like this.
  1886. 1:07:53Public static void main.
  1887. 1:07:55Okay.
  1888. 1:07:56Then I can say let's close this, close
  1889. 1:07:59this,
  1890. 1:08:00close this, close this. Just
  1891. 1:08:04put it over here like this. I can say
  1892. 1:08:09B object is equal to new B.
  1893. 1:08:13Okay. Then I can say object. is odd
  1894. 1:08:17five.
  1895. 1:08:19Um going to print it.
  1896. 1:08:25Run it.
  1897. 1:08:30True.
  1898. 1:08:31Object. is odd seven.
  1899. 1:08:38True.
  1900. 1:08:39What am I doing? Six.
  1901. 1:08:41False.
  1902. 1:08:42Should show false.
  1903. 1:08:44Okay.
  1904. 1:08:45So, that's how it's working. Now, one
  1905. 1:08:47new thing over here that I would like to
  1906. 1:08:49share is that uh the only difference
  1907. 1:08:52that this has from like a top-level
  1908. 1:08:53interface is that the um
  1909. 1:08:59the nested interface can be declared as
  1910. 1:09:00like public, private, or protected.
  1911. 1:09:03But, the top-level interface
  1912. 1:09:05has to be declared as public or the
  1913. 1:09:08default one.
  1914. 1:09:09Okay, that's the difference.
  1915. 1:09:12Cool.
  1916. 1:09:13All the standard stuff applies.
  1917. 1:09:15Okay.
  1918. 1:09:16You have to create an object of B and
  1919. 1:09:17stuff and use it like that. Yeah.
  1920. 1:09:20I'm using A.nested interface, means A
  1921. 1:09:23inside A there's a nested interface,
  1922. 1:09:25using that.
  1923. 1:09:28Okay.
  1924. 1:09:29So, that's nested interface. Okay. Well,
  1925. 1:09:32that was about this video, but this was
  1926. 1:09:33a very important video. We learned about
  1927. 1:09:35a very important concept of interfaces,
  1928. 1:09:36abstract classes, and stuff. Um yeah, in
  1929. 1:09:39the next video we'll learn about
  1930. 1:09:41generics and stuff, collection
  1931. 1:09:42framework, and all these other things,
  1932. 1:09:44and then we move on to some like
  1933. 1:09:45advanced data structures after that
  1934. 1:09:47because now we know OOP, so we'll be
  1935. 1:09:48able to write our own data structures,
  1936. 1:09:50linked list, stacks, queues, and dynamic
  1937. 1:09:53programming stuff, graph algorithms,
  1938. 1:09:55trees, and heaps, and hash maps. So, we
  1939. 1:09:59will be literally writing these on our
  1940. 1:10:02own using classes. That's why I had to
  1941. 1:10:04do object-oriented
  1942. 1:10:06programming first. So, you will not only
  1943. 1:10:08just learn how to use it, you'll
  1944. 1:10:10actually learn how to implement it as
  1945. 1:10:12well, every single function. So, you'll
  1946. 1:10:14use the internal one as well, the one
  1947. 1:10:15provided like collections framework, but
  1948. 1:10:17we'll actually create our own as well.
  1949. 1:10:19So, make sure you like, share, and
  1950. 1:10:20subscribe, and share about it on
  1951. 1:10:21socials,
  1952. 1:10:23so that it raises awareness, and share
  1953. 1:10:24with your friends. And yeah, make sure
  1954. 1:10:27you like, share, and subscribe, and
  1955. 1:10:28comment as well right now. Um and I'll
  1956. 1:10:30see you in the next one. It will really
  1957. 1:10:32help me, so I would really appreciate
  1958. 1:10:33it.

About this transcript

This page contains the full transcript of OOP 5 | Abstract Classes, Interfaces, Annotations by Kunal Kushwaha, generated from the public captions YouTube serves with the video. The transcript has 10,144 words across 1,958 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.