OOP 5 | Abstract Classes, Interfaces, Annotations — Transcript
Full transcript
- 0:03Hey everyone, welcome back to another
- 0:05video. This is the fifth video in the
- 0:06complete object-oriented programming
- 0:08series. And we're doing a complete data
- 0:10structures algorithms boot camp, so make
- 0:12sure you check out the links in the
- 0:12description. We've done some amazing
- 0:14content. But let's move forward with a
- 0:16very important topic. So we have already
- 0:18learned about inheritance and all these
- 0:19other things, okay? The link is in the
- 0:21description. There's a separate
- 0:22object-oriented programming playlist on
- 0:24the channel. You can check it out the
- 0:25previous videos. But now let's talk a
- 0:27little bit more about the
- 0:30problems we were facing in inheritance,
- 0:32right? So the problem we was facing in
- 0:33we were facing in the multiple
- 0:35inheritance was that if you have a class
- 0:36A,
- 0:38you have a class A, this has a function
- 0:40called fun. You have a class B.
- 0:43This also has a function called fun,
- 0:45exactly like same function, but the body
- 0:47is different, let's say. And you inherit
- 0:49that in class C. Now you call an object
- 0:52C dot fun. Now this will get confused.
- 0:55Should I call this one or should I call
- 0:57this one? It will get confused.
- 1:00That's why Java does not support this.
- 1:04Multiple inheritance, okay?
- 1:06Cool.
- 1:07But what I'm going to teach you now is
- 1:12that let's say sometimes you want to
- 1:13create a parent class, okay? So a parent
- 1:17class like this that you want to create.
- 1:20So sometimes you want to just create a
- 1:23parent class that just
- 1:25tells us what to do and not how to do
- 1:28it.
- 1:29Okay? So it will just give us a
- 1:30generalized form that will be shared by
- 1:32all the child classes.
- 1:35And parent class is going to be like,
- 1:37"Hey child class,
- 1:38fill in the details yourself. Be what
- 1:40you want." You know our parents tell us,
- 1:42"You can be what you want." So it will
- 1:44give us a resource, okay? It will just
- 1:46say, "Here's a career resource."
- 1:48Okay? So there will be a parent class
- 1:50like this.
- 1:52There's going to be a parent class like
- 1:54this. Let's say this is a parent class.
- 1:58This is a function called carrier.
- 2:04This has a
- 2:06function called
- 2:08carrier.
- 2:10It takes in a few arguments.
- 2:12It takes in a few arguments like um
- 2:15for example
- 2:16you know, name of the carrier.
- 2:19That's it.
- 2:20That's the only argument it takes right
- 2:22now.
- 2:23Okay? And it prints something.
- 2:26It's like set the name to
- 2:28something.
- 2:30The something depends on you, okay?
- 2:32So
- 2:33this body or whatever things that are
- 2:35happening, how you are doing your
- 2:36carrier or whatever. So your parent has
- 2:38given have given you a choice.
- 2:41So your parents are like this is a
- 2:42function.
- 2:44Put the what you what you want your
- 2:45carrier to be, that depends on you. I've
- 2:47just given you an option.
- 2:48So any child class, let's say this is
- 2:50going to be a child class.
- 2:52This child is going to be like there's a
- 2:53carrier option for me.
- 2:56Okay? I will make my body. In my body I
- 2:58will say name is equal to something like
- 3:01coder.
- 3:04A second child
- 3:05For a second child it's going to be
- 3:06like, "Oh, my parent has given me a
- 3:08choice. Okay? Inside the body whatever
- 3:10you want to do." Body is not given by
- 3:12parent.
- 3:13What the what your carrier represents,
- 3:15that depends on you. Means what the
- 3:17function is actually doing, that depends
- 3:19on the child classes or the children. So
- 3:22you're like, "Okay, my carrier
- 3:25the body what it goes over here, that
- 3:27depends on me."
- 3:28So example name is equal to doctor or
- 3:30something.
- 3:32Okay? Something like that.
- 3:34So this is what is meant by like what we
- 3:37mean when we say that uh
- 3:40your super class, the parent class, is
- 3:42only going to give us the function like
- 3:45about it, like a generalized form for
- 3:47it, not the body.
- 3:50Okay?
- 3:51Not the body.
- 3:53So, such a class will determine the
- 3:55nature of the methods
- 3:58that the sub class must implement.
- 4:00It's not implementing the method. It's
- 4:02just giving the name, the definition of
- 4:04it.
- 4:05But not the body or anything.
- 4:07These are known as abstract methods.
- 4:11Okay?
- 4:12So, for example, you have an abstract
- 4:14method over here, abstract method over
- 4:16here. Since this does not have a body,
- 4:18what does it mean? It means that when
- 4:20your function does not have a body, it
- 4:23totally depends on the child classes to
- 4:25provide the body for that function.
- 4:29Okay? So, now it does not depend on what
- 4:32the body is here or here. It will define
- 4:34the body for itself.
- 4:38Hence, and how can we do it? How can we
- 4:40change the body of something that is in
- 4:41the parent class? We have already done
- 4:43this. We have already done this,
- 4:45overriding.
- 4:47Okay? So, you may have method that
- 4:50methods that must be overridden in the
- 4:52child class in order for the child
- 4:54class, you know, to have any meaning.
- 4:59Okay? So, you want to ensure
- 5:02that the child class overrides all the
- 5:05necessary methods.
- 5:08These are known as abstract methods.
- 5:11These are just some empty methods over
- 5:13here.
- 5:14And child class, in order to use it,
- 5:16would have to override it.
- 5:20Okay?
- 5:22So, let's say your carrier returns
- 5:23nothing. So, the format is something
- 5:26like this. Abstract keyword.
- 5:29For to create a method into an abstract
- 5:31method. Type.
- 5:33Let's say void. Name.
- 5:36Carrier. Parameters.
- 5:38String.
- 5:40Name.
- 5:42That's it. No curly brace or body.
- 5:45You're just defining it.
- 5:47So, these methods are sometimes like
- 5:48referred to as the child classes
- 5:50responsibility because there is no
- 5:53implementation provided in the parent
- 5:55class.
- 5:56Hence, the child class must override
- 5:59them. It cannot simply use the
- 6:02one defined in the parent class because
- 6:04the one defined in the parent class does
- 6:05not have any body.
- 6:09Okay?
- 6:10And one more thing, any class, by the
- 6:12way, I'll put all these notes in the
- 6:14text file in the GitHub repository.
- 6:17Any class that contains one or more
- 6:20abstract methods must also be declared
- 6:23as abstract.
- 6:25Very important point. If a class
- 6:27contains any one of the abstract
- 6:29methods, it should also be declared as
- 6:31abstract.
- 6:33Okay?
- 6:35Let's try to code this and then I'll
- 6:37tell you some rules and all these other
- 6:38things. Okay? So, let me create another
- 6:40like package over here, abstract demo or
- 6:42something, ab
- 6:43stract demo. Something like that. Create
- 6:47a new Java class. Let's say I want to
- 6:49create a Java class of like uh parent
- 6:52only. Okay?
- 6:53Parent.
- 6:56Okay? Something like that. So, parent
- 6:58will give you a choice. Parent will be
- 7:00like abstract
- 7:02void
- 7:04carrier
- 7:07string name. That's it. Since if a class
- 7:10contains more than one one or more than
- 7:13one abstract methods, that class also
- 7:15needs to be declared as abstract. Right?
- 7:18So, I can
- 7:19make parent abstract as well.
- 7:21I have to make it.
- 7:23I I'll go over the properties and
- 7:25everything later on, but first let's
- 7:26look into the implementation of it. I'll
- 7:28create a main one.
- 7:31Okay? And I'll create a class called
- 7:34son.
- 7:36Something like that.
- 7:37Now, notice something.
- 7:38This is a parent class, this is a son
- 7:40class. This is obviously going to
- 7:41inherit the parent class and obviously
- 7:43parent class contains an abstract method
- 7:45and I just told you
- 7:46I just told you that every child class
- 7:49has to override all the abstract methods
- 7:52in the parent class. Let's try to see.
- 7:54Extends
- 7:56parent. Why? For obvious reasons.
- 7:58Because the method in the parent class
- 8:00does not have any body. That's why it's
- 8:02giving an error. It's saying sun must
- 8:04either be declared abstract or implement
- 8:05the abstract methods in the parent
- 8:07class. Okay?
- 8:09I'll also give one more thing uh career
- 8:11and uh partner. Something like this.
- 8:14Okay?
- 8:15Parents are like you choose your
- 8:16partner. Okay?
- 8:19Partner name and
- 8:22uh something like um age. Okay? That's
- 8:26it.
- 8:27So, now they're going to be like they
- 8:28contains two abstract methods.
- 8:30Please try to you know uh declare
- 8:32implement those. So, they're going to be
- 8:33like yeah, sure. No worries.
- 8:35I'll do that.
- 8:36So, it's overriding it. I've already We
- 8:38have already learned about overriding.
- 8:40Right? We already learned about
- 8:42polymorphism uh runtime polymorphism and
- 8:44stuff. Already learned about this in
- 8:45detail. Already we have covered
- 8:47overriding. That's exactly the same
- 8:49thing happening over here.
- 8:51Okay?
- 8:52Sun is going to be like
- 8:54um I am going to be a
- 8:59doctor or something.
- 9:01Okay?
- 9:02And um
- 9:05something like
- 9:06I love
- 9:08um
- 9:11whom?
- 9:12Any random name.
- 9:15Um
- 9:22I'm going to be not a doctor. I'm just
- 9:23going to say
- 9:25name.
- 9:26Right?
- 9:27I love name plus
- 9:35she is
- 9:38age.
- 9:39Okay? Something like that. Similarly, I
- 9:41can create a daughter.
- 9:45Something like this, right?
- 9:47So, daughter can be, you know, it can be
- 9:48anything. I'm going to be a name. I love
- 9:51this. Um he is something like that. No
- 9:55problem. That's it.
- 9:57Whatever things I just told you that
- 9:59parent class will have some abstract
- 10:01methods. Class would have to be need to
- 10:02declared as abstract. Now,
- 10:05all the child classes would have to
- 10:06override these methods.
- 10:08Okay? So, sometimes now don't ask me
- 10:10question like Kunal, that is fine. We
- 10:12are doing this.
- 10:13Uh it's pretty simple, Kunal, that uh
- 10:15whatever the parent class may contain
- 10:17any abstract methods, we would have to
- 10:19like, you know, override those over
- 10:21here. We understand that. We have
- 10:22already learned about overriding and
- 10:23everything. No worries. The only thing
- 10:25difference is that you are not providing
- 10:26a body over here. But why are we doing
- 10:29this? What's the use case? Well, it
- 10:31looks much more cleaner.
- 10:33If you definitely know
- 10:35if you definitely know that the
- 10:38functions that are the methods that are
- 10:40in the parent class needs to be
- 10:42overridden, then just make those
- 10:44abstract.
- 10:45That's the reason. An example is right
- 10:47in front of you. I'll give a real world
- 10:48example as well. Let's all talk about
- 10:50cars, for example. So, every single car
- 10:52will have a different engine. We'll
- 10:53create a main engine class, and that
- 10:55will be dependent on different different
- 10:57engines. Okay? I'll give you an example
- 10:58for that as well in a in a little while.
- 11:01So, let's first try to run this thing.
- 11:04Public static void main. I'm going to
- 11:05say son son is equal to
- 11:10new son.
- 11:11Something like that. And then I'm going
- 11:12to say son dot,
- 11:14you know, career. I'm going to be let's
- 11:16say a doctor.
- 11:22Doctor.
- 11:24Okay? And then I say
- 11:28daughter
- 11:29daughter is equal to new
- 11:32daughter
- 11:33then I can say daughter.career
- 11:39Wait a minute.
- 11:40No.
- 11:41It does not really make sense because
- 11:43the
- 11:44This is literally the same. So, I'm just
- 11:46going to What I'm going to do is I'm
- 11:47going to remove this from here.
- 11:50Okay? You'll be like Kunal, the function
- 11:52body is like them like the
- 11:54it's literally the same. So,
- 11:56how are you doing it? Let me just remove
- 11:58this and I'll just put this over here.
- 12:00I'm going to be a Now, you will know
- 12:02which one is running, right?
- 12:05Uh I love something like um
- 12:08um
- 12:14What do I put here? I have no idea.
- 12:16Um let me just uh
- 12:19remove this for a while.
- 12:22Override it.
- 12:24I want to be let's say daughter daughter
- 12:26wants to be a
- 12:27coder.
- 12:28Um I love
- 12:35Iron Man.
- 12:37Okay?
- 12:38Something like this.
- 12:40And son is going to be like, I love
- 12:42um
- 12:47Pepper Potts. Okay? Sounds good?
- 12:50Cool.
- 12:51So, this makes sense now.
- 12:53And
- 12:55um
- 12:55see if there's any
- 13:06Don't need this.
- 13:08Don't need this.
- 13:09Okay? Son.career daughter.career. So, if
- 13:12I run this, when I run son.career, it's
- 13:14obviously going to run the son's career,
- 13:16this one. And when I run
- 13:17daughter.career, it'll run the
- 13:19daughter's career. This is very simple
- 13:21because it's just simple overriding, run
- 13:23run time polymorphism or whatever. I'm
- 13:25going to be a doctor. I'm going to be
- 13:26coder. No problem. Exactly the same. The
- 13:29only thing difference is that here I'm
- 13:31just adding some abstract methods. It's
- 13:33exactly the same as overriding. In
- 13:34detail, I've explained to you the
- 13:36dynamic method dispatch and everything
- 13:37in the overriding video, the object
- 13:39oriented programming properties video.
- 13:40Please check it out.
- 13:42Okay?
- 13:43Cool. So, this is already explained in
- 13:45the overriding video. This is actually
- 13:47overriding it.
- 13:48Okay. So, this is what I meant by
- 13:50abstract classes.
- 13:52Uh this is a basic example of it. Like
- 13:53you would have some uh abstract methods
- 13:55over here and then you will have just
- 13:58your uh you would have to override those
- 14:00in the
- 14:01child classes.
- 14:03Okay? Now, let's do a deep dive into
- 14:06this. Now, another question you may have
- 14:07is, "Kunal, can we have uh variables
- 14:09over here?"
- 14:11Sure. No problem. int age
- 14:14That's it. You would have to assign it
- 14:16over here, somewhere over here. Let's
- 14:18say if I create a constructor.
- 14:21Okay. If I create a
- 14:22If I create a constructor over here,
- 14:24constructor
- 14:25and it's just going to pass
- 14:27int age. this.age is equal to age.
- 14:32Okay? Something like that.
- 14:36Now, I'll have to put some age over
- 14:38here, 30.
- 14:40Okay? Similarly for daughter.
- 14:47daughter
- 14:49daughter as well.
- 14:5328, something like that.
- 14:55You can do that. No worries at all.
- 14:59Right? Just the simple stuff.
- 15:01Okay?
- 15:02But, can we create constructor of an
- 15:04abstract class?
- 15:10It's allowing us to do that like this.
- 15:12Okay? No problem because this is
- 15:14actually just allowing us to because we
- 15:16can call this constructor by the like
- 15:18over here you can see using the super
- 15:20keyword. Okay? So, here I can just add
- 15:23my age or whatever.
- 15:25I can remove this. I can just pass the
- 15:27age over here like this.
- 15:30Okay?
- 15:31Something like that.
- 15:32That is also totally fine. Cuz in the
- 15:34end it's just doing this.age only.
- 15:37Okay?
- 15:38Can you call this constructor? Let's try
- 15:40to do that.
- 15:41Let's try to say something like
- 15:43parent
- 15:46mom is equal to new
- 15:49parent.
- 15:53Error.
- 15:55So, you cannot create objects of an
- 15:57abstract class.
- 15:59You can't create objects of an abstract
- 16:01class.
- 16:04Okay?
- 16:05So, here you can see that you cannot
- 16:06create an object of an abstract class.
- 16:08It's asking you to implement the methods
- 16:10while you are creating it like this.
- 16:12It's asking you to do that.
- 16:14Over here.
- 16:15Okay? But still it's saying
- 16:18error.
- 16:20Error. Like this. I mean, this is fine.
- 16:22I mean, I have to
- 16:23add some age over here or whatever, but
- 16:26uh
- 16:26you get the idea. You can't just
- 16:28directly like create a object of this.
- 16:30It's actually not allowing me. It's
- 16:31actually saying, "Please override it."
- 16:35Okay? For For abstract classes, you
- 16:37cannot create objects like this.
- 16:40Okay?
- 16:41Another point I would like to mention,
- 16:42I'm going to leave with you some really
- 16:44amazing points now. Can you create
- 16:45abstract constructors?
- 16:48Can we create abstract constructors?
- 16:50Abstract constructors.
- 16:57No.
- 16:59Abstract construc- constructors are not
- 17:01allowed.
- 17:02Can you create abstract set- Now, one
- 17:04more thing I would like to share. Since
- 17:05you cannot create an object of abstract
- 17:08class. Okay? So, this is an important
- 17:10point. You cannot create objects of
- 17:12abstract classes.
- 17:13Okay?
- 17:14So, if If cannot Why can't we create
- 17:16objects of abstract classes?
- 17:18Because this if you are able to create
- 17:21an object of parent class and you try to
- 17:22call the carrier one, it will give you
- 17:24an error. It will be like, "Hey, it is
- 17:25does not have anybody. How can I call
- 17:27it?"
- 17:29Right? So, you cannot create objects of
- 17:31abstract classes.
- 17:33Similarly, for the same reason, you
- 17:34cannot create abstract constructors.
- 17:39Okay? Can you create abstract static
- 17:41methods?
- 17:44Do static methods get overridden?
- 17:47No.
- 17:48Then what is the point of creating
- 17:50abstract static methods?
- 17:53Abstract methods are what? That needs to
- 17:54be overridden. Static methods cannot be
- 17:56overridden. So, can you create abstract
- 17:58static methods? No.
- 18:01Okay?
- 18:04But can you create static methods in
- 18:06abstract classes?
- 18:08That you can do.
- 18:09Okay?
- 18:10Static
- 18:11void
- 18:14hello
- 18:15That you can do.
- 18:18Hey.
- 18:20Something like this.
- 18:22Because there can be no objects for
- 18:23abstract classes. If they are allowed to
- 18:25call like, you know, abstract static
- 18:28methods, it would mean that we are
- 18:29calling like them through the class name
- 18:31because it's static. You all already
- 18:33know it. So, I can say something like um
- 18:37parent dot
- 18:39hello No problem in that.
- 18:42That's totally fine.
- 18:51Okay?
- 18:52Cool?
- 18:53Sound good?
- 18:55Can abstract classes contain normal
- 18:56methods? Void
- 18:59normal
- 19:03This is a
- 19:04normal
- 19:05method. Most definitely they can.
- 19:08This is going to behave the same way as
- 19:09in uh
- 19:10as normal ones does. So, for example, if
- 19:12you want to just uh say
- 19:16just, you know, something like
- 19:18sun.normal, totally fine.
- 19:20You can also override it over here. You
- 19:22can say override the normal one. That's
- 19:24also totally fine.
- 19:27Totally cool. No problem.
- 19:29These are just the concepts we already
- 19:30learned before. Exactly the same thing
- 19:32is applying over here.
- 19:34The important points I already
- 19:35mentioned, you cannot create static
- 19:38constructors in this. You can't create
- 19:40static method abstract static methods in
- 19:42this. Abstract static constructor,
- 19:44sorry. Uh sorry, abstract constructors.
- 19:46You can't create abstract constructors
- 19:47in this. You can't create abstract
- 19:48static methods in this. And um
- 19:52at least one of the methods should be
- 19:53like abstract as well.
- 19:54Okay. I mean it does not mean sorry if
- 19:57at least one of the methods is abstract,
- 19:58then the class needs to be abstract
- 20:00itself.
- 20:01That's it. Yeah. Rest you can do
- 20:02everything else. No problem.
- 20:05These variables and everything works
- 20:06similar to like it works in like the
- 20:08previous lectures.
- 20:10That is it.
- 20:12Okay. You can also have methods with
- 20:14bodies and stuff. That's also totally
- 20:15fine.
- 20:18Okay.
- 20:19Cool.
- 20:20One thing I would like to share is even
- 20:22though you cannot create an object of a
- 20:23parent class like this of an abstract
- 20:25class, you can use it like this
- 20:28as a reference variable.
- 20:31Okay.
- 20:32Because Java's runtime polymorphism, you
- 20:35know, we can use it for like the
- 20:37superclass
- 20:39is implemented through the use of the
- 20:40superclass right? We we actually learned
- 20:43about this in the previous lecture, the
- 20:44runtime polymorphism for this thing.
- 20:46Like, you know,
- 20:48what it accesses depends on this thing.
- 20:50You remember that point we discussed.
- 20:52This is also the same thing.
- 20:55But which one is actually accessing the
- 20:56overriding one?
- 20:58That is going to be determined by the
- 20:59type of the object.
- 21:01We already covered this thing.
- 21:03This thing and this thing already
- 21:04covered in the over in the old
- 21:06properties lecture. Please check it out.
- 21:09Exactly the same thing.
- 21:11I'm not going to scare you and tell you
- 21:14that this is a different thing. I will
- 21:15not do that.
- 21:17It's the ditto same copy paste thing.
- 21:20Okay, and it makes sense also.
- 21:22Just it's the with the abstract class
- 21:24there's this thing like okay, this is an
- 21:25abstract class. That's it.
- 21:29Okay? And why can't we create like um
- 21:32I mean we are creating a public
- 21:33constructor, but then why does it not
- 21:35make sense?
- 21:36Because you can't you know in
- 21:37instantiate an abstract class directly.
- 21:41Okay? So, you can only instantiate via a
- 21:42derived type.
- 21:44Okay?
- 21:45That should also not be abstract.
- 21:47Right?
- 21:49So, that's pretty much about it. Yeah.
- 21:53Okay, one more thing.
- 21:55Abstract classes need to be like
- 21:56extended so that they their methods can
- 21:58be overridden.
- 21:59If I put final behind a class, it
- 22:02prevents it from inherited, right? If I
- 22:04put final behind a class, it prevents
- 22:06the class from being inherited. Can I
- 22:08have a final abstract class then?
- 22:11Common sense. No.
- 22:13No, you can't.
- 22:15No, you can't.
- 22:18For obvious reasons.
- 22:21Right? Because you want it to be
- 22:22inherited.
- 22:24That's why you can't have it.
- 22:26That is it. Normal stuff.
- 22:29Please check out the overriding video.
- 22:31It will clear all your doubts. I'm not
- 22:33skipping anything. I want this course to
- 22:35be the best course on YouTube. I will
- 22:38never ever skip anything. Every single
- 22:40line I'm explaining to you.
- 22:42That's why I'm saying lectures are tied
- 22:44together.
- 22:45Please check out the OOPs property
- 22:47video.
- 22:48Okay?
- 22:49In that I've deeply explained dynamic
- 22:50method dispatch and everything how this
- 22:52determines and everything. But the only
- 22:54new thing we have over here is abstract.
- 22:58Okay?
- 22:58Talked about constructors, it does does
- 23:00really make sense and stuff like that.
- 23:01Okay? So, here sometimes you can put
- 23:04like uh abstract like sorry, a
- 23:06constructor over here when you have
- 23:07let's say some final values. Final int
- 23:11value, something like this. Okay? One
- 23:14second.
- 23:15Add a constructor. I would have to add
- 23:16obviously because it's final. So, you
- 23:18can cannot have it like without it. So,
- 23:20I can say
- 23:21um something like um
- 23:26value is equal to
- 23:29something like this.
- 23:32Okay?
- 23:33Now, where is this being called?
- 23:34Obviously, you cannot call it directly
- 23:36to create an object. You can only call
- 23:37it like this.
- 23:39In another constructor like this.
- 23:42Okay? So, even though you create a
- 23:43constructor and you're not able to
- 23:44create an object from it, you can use
- 23:46the super keyword to reference to it.
- 23:49Okay? That way you can initialize some
- 23:51final variables over there.
- 23:53Can you have static variables over here?
- 23:57Yeah.
- 23:58Sure.
- 23:59No problem.
- 24:02You can have static methods as well.
- 24:04You can have static variables as well.
- 24:05No problem in that.
- 24:07I think I covered like every single
- 24:09possibility.
- 24:11Right?
- 24:13That's it. Yeah.
- 24:16Those are abstract classes.
- 24:18It still does not support
- 24:19It still will not support like
- 24:22multiple inheritance. Okay? This does
- 24:24This does not support multiple
- 24:25inheritance. If you have two abstract
- 24:27classes, something like parent two,
- 24:29okay? You can't do You can't extend both
- 24:32the classes in one class.
- 24:34Multiple inheritance is not allowed.
- 24:36Class cannot extend multiple classes.
- 24:39For that, we have to learn about
- 24:41interfaces.
- 24:42Okay? Cool.
- 24:45Because here we have so many
- 24:46limitations, right? We actually have
- 24:48like okay,
- 24:49the the problem is not solved. It still
- 24:51allows
- 24:52allows us to have normal methods and
- 24:54stuff like that. The normal method. Same
- 24:56problem will come.
- 24:58If two normal methods have the same
- 24:59name.
- 25:01Okay?
- 25:02This is about abstract classes.
- 25:06Okay?
- 25:07Cool?
- 25:10Let's move forward. Okay, so we know
- 25:11that multiple inheritance are not
- 25:13supported in Java due to this one reason
- 25:17that we just saw.
- 25:20This one. Right? So, if the two bodies
- 25:22of functions are there, it will get
- 25:23confused like which one to pick. Now,
- 25:26instead in order to resolve this, I've
- 25:27told this multiple times, we use Java
- 25:29interfaces.
- 25:31Okay? It's not a class, it's called an
- 25:33interface. Okay, that's the name of it.
- 25:35We still use the dot Java extension of
- 25:38the file, but it's actually called an
- 25:40interface. We don't write class A, we
- 25:42write interface A. Now, what are
- 25:43interfaces and how are they different
- 25:45than uh classes for example, right? So,
- 25:48let's talk a little bit more about that.
- 25:50Interfaces basically contain abstract
- 25:52functions. Okay? You all know what
- 25:54abstract functions are. No body of the
- 25:56function is allowed. It is like a class
- 25:59only, but not completely. Okay, it's
- 26:01like an abstract class and by default
- 26:03the functions uh are public and abstract
- 26:07in the interface and the variables are
- 26:10what? You tell me.
- 26:12What are the variables?
- 26:14If these are like interfaces, these are
- 26:16not classes, you cannot create their
- 26:18objects. Okay, and let's say it's not
- 26:20allowed for you to have like any objects
- 26:22for that or whatever, then
- 26:24what are the things that are not
- 26:25dependent on objects? Static stuff.
- 26:29Static stuff. Right? So, the variables
- 26:31are static and final by default in
- 26:34interfaces. I'll talk more about that
- 26:36later. Right? So, interfaces mostly
- 26:39specify like what the class is doing,
- 26:41not how it's doing it. Similar to
- 26:43abstract classes is abstract methods,
- 26:46but there you cannot do multiple
- 26:47inheritance. Okay, because of this
- 26:49problem that you you on the screen. Cuz
- 26:51the sub class won't be able to choose
- 26:53like, you know, which one to pick or
- 26:55whatever.
- 26:56Cool. So, some of the key differences
- 26:58between like a class and an interface,
- 27:00uh a class will maintain like some state
- 27:02information like through some instance
- 27:04variables or whatever, but interface
- 27:06cannot.
- 27:08Okay?
- 27:09That's why the variables in interfaces
- 27:10are static and final. You can't change
- 27:14those. It's dependent on don't like not
- 27:16dependent on object either because
- 27:18interfaces do not allow you to have such
- 27:20things.
- 27:21You know, like your uh function bodies
- 27:22or whatever, but it's like a default
- 27:24body is allowed. I will tell you more
- 27:26about that later, but for now,
- 27:28um
- 27:29let's just try to imagine that, okay,
- 27:31there isn't.
- 27:32Okay? But
- 27:34for now, let's just imagine there isn't,
- 27:36but we'll talk more about that later.
- 27:38Okay. So, every every single method,
- 27:41everything will be abstract. You cannot
- 27:42create
- 27:43uh like
- 27:45objects of like interfaces and stuff.
- 27:47So, obviously, all the
- 27:49variables and stuff inside it are like
- 27:51final and static. Okay.
- 27:54Cool.
- 27:55Um
- 27:57Yeah, uh interface only has like
- 27:59abstract methods, abstract classes
- 28:00versus interfaces I'm talking about. So,
- 28:02interface has abstract methods, abstract
- 28:04classes can have like abstract method as
- 28:06well, non-abstract as well.
- 28:07Um and from Java 8, it can also have uh
- 28:10default and static methods also. I'll
- 28:13talk more about the default methods.
- 28:14Final variables are what? Final
- 28:16variables when variables declared in the
- 28:19Java interface, uh they are by default
- 28:21final and in the abstract, they it may
- 28:24contain final and non-final both.
- 28:27Okay?
- 28:28Um why are they final? Because if you
- 28:30just create a variable in uh
- 28:33in an interface, and you know, the
- 28:34interface cannot have like a
- 28:35constructor, then how will you
- 28:36initialize that? That's why they are
- 28:38final because final variables always
- 28:40needs to be initialized. Okay, there's a
- 28:42reason for you. Okay, implementation.
- 28:44Abstract class can provide the
- 28:45implementation of an interface, but the
- 28:47interface can't provide the
- 28:48implementation of abstract class. This
- 28:50I'll show you in practice. Okay.
- 28:53Inheritance versus abstraction. Let's
- 28:55talk about that. And I'll also
- 28:57definitely give you an example of all
- 28:58these points as well. So, an a Java
- 29:00interface can be implemented using the
- 29:02we use the implements keyword over here.
- 29:05And in the abstract class, we use what?
- 29:07Extends keyword. We already saw that
- 29:09just now. Extends keyword. Here we will
- 29:11use the implements keyword.
- 29:14Okay.
- 29:14The main point where it shines, you can
- 29:17implement multiple interfaces together.
- 29:22Okay.
- 29:22And multiple implementation also means
- 29:24that an interface can also extend
- 29:26another Java Java interface. That is
- 29:29also something I'll show you in
- 29:30practice. But an in But an abstract
- 29:32class can only extend another Java class
- 29:35and like uh only only single one only.
- 29:38Right? It can't It can't do multiple
- 29:39inheritance or whatever. This is the
- 29:40beauty of it. We'll We'll see this in
- 29:42action also.
- 29:44Okay.
- 29:45So, about the accessibility, members of
- 29:47the Java interface are public by
- 29:49default. And uh the Java abstract class
- 29:52can have all kinds like private,
- 29:54protected, or whatever.
- 29:57Okay. So, these are like sort of like
- 29:58the key differences.
- 30:00So, as I mentioned already, using like
- 30:02interfaces you can specify, you know, a
- 30:04set of methods and stuff. And then you
- 30:06can be like, "Okay, um they can be now
- 30:08implemented by more than one classes."
- 30:11Okay. So, they are sort of like it's
- 30:13you'll be like, "Hey Kunal, this is
- 30:14similar to abstract classes." Okay?
- 30:16That's true, but it has an additional
- 30:17functionality. A class can implement
- 30:19more than one interfaces.
- 30:21But on the other hand, a class can only
- 30:23inherit a single superclass
- 30:26or abstract class or whatever.
- 30:28The multiple inheritance is not allowed,
- 30:29but here it's allowed.
- 30:31Here it's allowed.
- 30:34So, using the keyword interface, you can
- 30:35fully abstract a class interface from
- 30:38its implementation. Classes interface
- 30:40from its implementation.
- 30:42It means that you can specify what a
- 30:43class must do, but not how it does it.
- 30:46How it does it will be specified by the
- 30:48child classes by overriding the abstract
- 30:51methods in the interface.
- 30:54Okay?
- 30:55That's it.
- 30:57In interfaces, you don't have like
- 30:58instance variables or whatever. You have
- 30:59final final variables, final and static.
- 31:03Okay?
- 31:04So, obviously, abstract methods declared
- 31:06without any body and things like that.
- 31:08Yeah.
- 31:10So, you can now utilize that one
- 31:11interface and multiple methods.
- 31:14So, we can use something like this.
- 31:17Yeah. Now, this will actually be mak-
- 31:20making more sense. I'll take you I'll
- 31:21give you like a little bit of real-world
- 31:23example as well.
- 31:24Okay?
- 31:25That's pretty much about it. Let's jump
- 31:26into the code and look at all these
- 31:28things in action. Okay? So, let's see
- 31:30what we're trying to do.
- 31:31Let's imagine you work at a factory of a
- 31:34car company, right? So,
- 31:37you have various cars that have various
- 31:39engines. For example, there's some has
- 31:41like high-powered engine, diesel engine,
- 31:43electric engine. Tesla motors have that,
- 31:45right? For example. So, what we're
- 31:47trying to do is, let's say
- 31:50even though the type of engine is
- 31:51different, but every engine has like a
- 31:53different functionality, like the way it
- 31:55works. Okay? So, an engine class can be
- 31:58something like this, let's say.
- 32:00Okay? How the engine starts
- 32:04is going to be different. How the engine
- 32:06stops
- 32:07is going to be different. And how it
- 32:09accelerates is going to be different.
- 32:13Right?
- 32:15Similarly, what all things happen? We
- 32:16can have
- 32:18another thing like
- 32:19brake,
- 32:20for example.
- 32:22Right? We can have a We can have a
- 32:23brake. So, for example, if you just uh
- 32:27s- brake,
- 32:30every car will stop like differently.
- 32:32Okay?
- 32:33Every car will have something else, like
- 32:35let's say it has some media player.
- 32:38Right?
- 32:39So, you can say media player, this is my
- 32:40engine.
- 32:42This is let's say my break.
- 32:44This is my media player.
- 32:47Okay? And in your media player, you're
- 32:48going to be like
- 32:50hey
- 32:51um something like start
- 32:56and stop my media player.
- 32:59That's it.
- 33:02Okay?
- 33:04Sound good?
- 33:08Cool?
- 33:23Okay? That's about it.
- 33:27That is it.
- 33:31Okay?
- 33:32So, the problem over here is that um
- 33:36for every car, this is going to be
- 33:37different. Okay? For every car, this is
- 33:40going to be different. For the petrol
- 33:42car, it will be like hey, I start as a
- 33:44petrol car, I stop as a petrol car, I'm
- 33:46using the petrol car brakes, and I
- 33:48accelerate as a petrol car, for example.
- 33:50For diesel car, it will be like I start
- 33:52as a diesel car, I stop as a diesel car,
- 33:54I'm accelerating like a diesel car, and
- 33:55I put brakes as a diesel car. So, you
- 33:57can be like okay, Kunal, it's like
- 33:58different for each and every one. That's
- 34:00totally fine.
- 34:01Can you maybe like just create these as
- 34:03abstract classes and just override these
- 34:05methods? And I'm like yeah, sure, we can
- 34:07do that.
- 34:08But then we cannot utilize the multi
- 34:10multiple inheritance that we want to do.
- 34:13Right? We can't really utilize that.
- 34:16I want a single car. And let's say if
- 34:18I'm creating a petrol engine class,
- 34:21right? I'm creating a petrol engine
- 34:22class or whatever. I want it to utilize
- 34:25all these things.
- 34:27Let's say all of these things, for
- 34:28example.
- 34:31Right? And let's say I'm just creating
- 34:32one custom car. Okay, just one single
- 34:35car.
- 34:35That car needs to utilize all of these
- 34:37three things.
- 34:41In that case, what will you do?
- 34:43You can't extend three things at once in
- 34:46abstract classes or classes. Multiple
- 34:48inheritance is not allowed.
- 34:50Right?
- 34:51In that case, we can use interfaces.
- 34:53So, I can create these as interfaces.
- 34:56And then I can create custom classes
- 34:57from it.
- 34:59Let's see this in action. See this in
- 35:01action. So, I can just create a new
- 35:04package over here.
- 35:06I can say
- 35:09inter
- 35:10faces.
- 35:12Okay? I can create, let's say, what did
- 35:15we have over here? I can create one
- 35:17called engine.
- 35:18No problem. So, I'll not create a class.
- 35:20I'll actually create an interface.
- 35:23An interface
- 35:25known as engine.
- 35:27So, you can see the definition, public
- 35:29interface engine.
- 35:30Okay?
- 35:32This is going to have what?
- 35:33This is going to have something like
- 35:35start, stop, and accelerate. Okay? No
- 35:38problem.
- 35:39So, I can say void start,
- 35:44void
- 35:45stop,
- 35:47void accelerate.
- 35:49Something like that.
- 35:51Okay?
- 35:53Cool?
- 35:56Any variable you need to have, let's
- 35:57say, int um something like price
- 36:01is equal to has to be final, right? Is
- 36:04that too
- 36:0578,000 or whatever.
- 36:08Okay? This is by default what? Static
- 36:10and final.
- 36:15Okay, that's why it's blurred out
- 36:16because it's like, "Hey, you don't have
- 36:17to write this. It's already
- 36:19like that."
- 36:22Okay?
- 36:23What is the next thing you need?
- 36:24Next thing you need is like, let's say,
- 36:26a break, right?
- 36:28So, break basically means just like a
- 36:30new interface.
- 36:35Break.
- 36:37That's it. It's going to say break.
- 36:41Void
- 36:44break.
- 36:45b r a k e
- 36:47Something like that.
- 36:49Okay?
- 36:51You have another one called
- 36:53media. So, you can imagine these as like
- 36:55just abstract classes. But, the only
- 36:56difference is
- 36:58I've already explained the difference,
- 37:00right? What the difference is between
- 37:01these.
- 37:02Let's create one more called media.
- 37:06Media. Okay? Now, it will contain let's
- 37:09say the same ones, start and stop.
- 37:12Like this, for example.
- 37:15Start and stop.
- 37:18Okay?
- 37:20Something like that.
- 37:25Cool?
- 37:27Sound good?
- 37:32Okay?
- 37:34So, let's say you want to create a car
- 37:36now.
- 37:37So, let's say you want to create a Java
- 37:38class. You create a car. A car needs to
- 37:41have let's say these two
- 37:42functionalities, break and engine.
- 37:46So, previously you were not able to do
- 37:48it.
- 37:49If you need both of these
- 37:50functionalities, you were not able to
- 37:51add it in one single one.
- 37:53Because you cannot implement two files
- 37:55into one.
- 37:56Multiple inheritance not allowed.
- 37:59You can't You weren't able to do it like
- 38:01this previously, were you? No, right?
- 38:04If you want to make a car like this.
- 38:06Now, you can with the implements
- 38:07keyword. Implements, now you can put as
- 38:10many classes as you want.
- 38:12Engine
- 38:13and break. Let's say I only want two.
- 38:16Obviously, these are abstract methods,
- 38:18so you would have to implement those. No
- 38:19problem, I I do it.
- 38:21Implement methods like this.
- 38:25So, you can say
- 38:28I break
- 38:29like a normal
- 38:33car.
- 38:37I start like a normal car.
- 38:41Okay?
- 38:42I stop like a normal car.
- 38:45Okay, and I accelerate.
- 38:51What is the spelling? Accelerate.
- 38:54Like a normal car.
- 38:56Were you able to do this previously?
- 38:58Putting two
- 39:00two inheriting the properties of more
- 39:02than one classes into one. Were you able
- 39:04to do this before? No. Now you can.
- 39:08Even if this had a start over here,
- 39:10let's see.
- 39:12Would it really matter? Because it is
- 39:14already adding its own value, own
- 39:17function body.
- 39:19Right? It wouldn't matter.
- 39:21Right? If there was like a start over
- 39:23here, void start or something like this.
- 39:25Would it matter much? No, because it's
- 39:27providing its own body. Does not care
- 39:29which one it's overriding. It's just
- 39:31overriding, we know that.
- 39:33Okay?
- 39:35So, it wouldn't matter.
- 39:38So, are you able to see how the conflict
- 39:39resolution is also resolved? This was
- 39:41being resolved in abstract classes also,
- 39:42but like in that this multiple
- 39:44inheritance was not allowed. Here it's
- 39:46allowed.
- 39:48Create a main file.
- 39:50Java class.
- 39:52Main.
- 39:54Okay?
- 39:56Something like this. You can say public
- 39:57static void main.
- 40:00Right?
- 40:02You can say my
- 40:04car car is equal to new car.
- 40:08Okay? Something like that. And then you
- 40:09do
- 40:10car dot um
- 40:13accelerate.
- 40:15Then say car dot start.
- 40:18Car dot stop or whatever.
- 40:21It's going to run what? What is this
- 40:22thing? Overriding already explained.
- 40:24You already know what overriding is.
- 40:26Dynamic method dispatch, everything you
- 40:27know. How it will handle it, how will it
- 40:29determine, everything I've covered in
- 40:31the overriding oops property lecture.
- 40:33All right? Please watch that without
- 40:35asking doubts because I've already
- 40:36covered that. I want this to be the best
- 40:38course on YouTube for data structures
- 40:40algorithms and object programming and
- 40:41interview preparation. Hence, I'm not
- 40:43skipping anything. I've already
- 40:44explained this in detail. Okay? Check
- 40:46out the links in the description. So.
- 40:48How which particular method to run it's
- 40:50actually determining on at run time.
- 40:53At run time.
- 40:57Okay?
- 40:58So.
- 40:59That's pretty much about it.
- 41:02Okay? One more thing I'd like to mention
- 41:04is that when we use classes, right? I
- 41:06told you that the parent class has no
- 41:08idea about child classes, but the child
- 41:10class has idea about parent classes.
- 41:12Okay? So, when you are calling one
- 41:15function from a child class and that
- 41:18function, let's say, exists in the
- 41:19parent class. Then the parent class and
- 41:22child class both have to be present at
- 41:25the compile time.
- 41:26So, that the Java compiler can check
- 41:28that everything is compatible or
- 41:30everything.
- 41:31Okay? Simple stuff. Makes sense. Cool.
- 41:33So, this way for more functions to be
- 41:36available to child classes, functions
- 41:39get pushed up higher and higher in the
- 41:41parent classes.
- 41:43Okay? And
- 41:45this is basically um
- 41:47a problem that interfaces solve. Because
- 41:50they disconnect the definition
- 41:53of a method from the this hierarchy of
- 41:57inheritance. Okay? Because you are
- 41:59implementing it in the
- 42:01subclass only, the main body.
- 42:04Okay?
- 42:06So, interfaces are not like in the
- 42:08hierarchy of classes. Okay?
- 42:11Uh it's totally possible for like
- 42:12classes that are
- 42:14two classes let's say that are unrelated
- 42:15to each other in terms of who their
- 42:17parents are or whatever.
- 42:20They can implement the same interface.
- 42:22This is where the power of interfaces
- 42:24shines.
- 42:26Okay.
- 42:28Remember I told you you can go up but
- 42:30you can't go down like this, right? In
- 42:32that way makes sense to put
- 42:34functions that are being used by
- 42:35multiple classes up and up and up.
- 42:37Interface does not really care about
- 42:39what the inter what like whether two
- 42:41classes are related or not or whatever
- 42:42like that. It does not really care.
- 42:44But in normal inheritance it does care.
- 42:48Right? Two classes are
- 42:50inherited or not, right?
- 42:52Like we already discussed like you can
- 42:54you have two child classes you can't
- 42:55really go like this. You can only go up.
- 42:57Interface it does not really care.
- 42:59Two classes that are basically unrelated
- 43:01to each other, right? They can also
- 43:02implement the same interface.
- 43:05Is what I'm saying.
- 43:08This is awesome.
- 43:10As simple as that.
- 43:13Two classes that are unrelated to each
- 43:14other can also implement the same
- 43:16interface.
- 43:18Okay.
- 43:21Cool.
- 43:22So this is dynamic method dispatch
- 43:24method or like override overriding and
- 43:26stuff. It works exactly the same way
- 43:28like dynamic method dispatch that we
- 43:29taught previously. At run time it will
- 43:31decide which one to run. That obviously
- 43:33depends on the type of this.
- 43:36Okay. Can you create
- 43:37like one more thing I would like to
- 43:38mention is that uh
- 43:41Can you actually add like uh
- 43:43type of the variable as the type of the
- 43:45interface like we can with abstract
- 43:47classes and parent classes.
- 43:49Yes, you can.
- 43:52We are able to do it.
- 43:54Again same same rules will apply then.
- 43:57If you have something that is inside
- 43:58this class you will not be able to
- 43:59access that.
- 44:01int a is equal to 30 or something like
- 44:03that.
- 44:05car.a
- 44:07You will not be able to access that.
- 44:09Why? Because already explained a
- 44:11thousand times by me. What things you
- 44:14access depends on this, which one of
- 44:16those versions to access depends on
- 44:18this. Overriding and this is just
- 44:21putting reference variables as parent
- 44:23classes. Already explained previously
- 44:24with examples.
- 44:26Okay? If you did not understand this
- 44:28means I I can't teach you
- 44:30object-oriented programming because you
- 44:31skipped all the previous lectures.
- 44:33This is a very basic stuff that I
- 44:34already explained multiple times I even
- 44:36pointed it out very clearly. We spent
- 44:38like so much time on this.
- 44:40You know, I told you that story also I
- 44:42told you, which version to access will
- 44:44be mentioned by the object, what to
- 44:46access will be mentioned by this. It's
- 44:48type engine so engine access it can only
- 44:50access these things.
- 44:52It cannot access what is inside the car
- 44:54even though the object is of car type.
- 44:57But
- 44:57which one to actually access depends on
- 45:00the type of the object. Type of the
- 45:01object is car. So when you do acc, it
- 45:04will actually access the car type's acc.
- 45:07This one.
- 45:09Okay?
- 45:11Make sense? So you can definitely do
- 45:13this. Okay? And since we've already
- 45:14covered this thing like
- 45:16overriding and dynamic method dispatch
- 45:18and everything, it's the same same thing
- 45:20that is going on over here but this
- 45:22dynamic lookup of like the method at
- 45:24runtime which we've already covered many
- 45:26many times.
- 45:27Uh it's it's like a it has a little bit
- 45:29of an overhead when compared to like the
- 45:31normal method invocation in Java like
- 45:32how normal methods work. That is why you
- 45:35should be careful not to use interfaces
- 45:37casually in performance-critical code.
- 45:41Okay, that is something I wanted to
- 45:42point out.
- 45:44Okay, because it happens at runtime.
- 45:46Right?
- 45:47Okay, let's move forward. Okay, now let
- 45:49me show you one more thing.
- 45:50Let me show you something like uh if I
- 45:52have a car over here and let's say it
- 45:54extends um
- 45:57media also.
- 45:58Okay? Now media also contains start and
- 46:01stop and uh engine also contains start
- 46:04and stop. And this car also then has
- 46:06start and stop.
- 46:08Okay? I like I start like a I start
- 46:10engine
- 46:12like a normal car. I stop engine like a
- 46:15normal car. Now when I say stop, let's
- 46:17see what will happen.
- 46:19Okay? Something like this.
- 46:21Let's say I have my car. Let's say I
- 46:23create let's say something else. I say
- 46:25something like
- 46:28you know, my media. Let's say I want to
- 46:30create a media type. Okay? For example.
- 46:32Media, my car media. Okay? This is a
- 46:35class for my reference variable for my
- 46:37car's radio or whatever. Is equal to new
- 46:40car or something like that.
- 46:42Right? And then I say car media.stop.
- 46:47Let's see what will happen.
- 46:55So it actually stopped the engine. Now
- 46:57would how would it how weird would it
- 46:59be? You're driving your car. You try to
- 47:01stop the car's in car's music player and
- 47:03the engine stops.
- 47:07That's what's happening over here.
- 47:09Okay? Similarly, if you want to change
- 47:11the engine over here,
- 47:12okay? You want to change the engine of
- 47:14this thing, then what would you have to
- 47:16do? You would have to create a new
- 47:17object of this. You would have to change
- 47:18the entire car.
- 47:21Let's say you go to your mechanic and
- 47:22you're like, "Hey, I want to actually
- 47:23change the engine." He's like, "Hey, you
- 47:24want to change engine? You have to
- 47:26change the entire car." Or seats. Let's
- 47:28say you want to change the seats and
- 47:29then you answer you have to change the
- 47:31entire seat.
- 47:32There's a little bit of problem over
- 47:33here.
- 47:35Okay?
- 47:36That's why we can create separate
- 47:37classes for these interfaces as well.
- 47:40Okay?
- 47:41Let's see how we can do that. So I'll
- 47:42create like the separate classes. First
- 47:44class I'll create as let's for my
- 47:46CD player. So my
- 47:49CD player.
- 47:51Okay? Something like that. It implements
- 47:53what? It only implements media, let's
- 47:54say. Implements media. Okay?
- 48:00Okay? So, I can say
- 48:07rock
- 48:08music or something like I can say
- 48:12music
- 48:13start.
- 48:14Stop means
- 48:18music stop.
- 48:20Okay, it's only implementing media for
- 48:22example right now.
- 48:24Okay? Let's say I create another engine.
- 48:26I can say I create engine I can say um
- 48:32power engine.
- 48:35Okay, I can create a power engine. It
- 48:37implements all the things that are in
- 48:39the engine.
- 48:40For example,
- 48:43implement the methods.
- 48:46Power engine
- 48:49start.
- 48:54Power engine
- 48:56stop.
- 48:57Power engine accelerate.
- 49:01Okay?
- 49:03Something like that.
- 49:05Good.
- 49:07And then I can create let's say a
- 49:11electric engine as well.
- 49:13Java class
- 49:14electric
- 49:16engine.
- 49:19Electric engine
- 49:21implements
- 49:22engine.
- 49:25Like this.
- 49:26Okay.
- 49:28I can say electric
- 49:33electric engine start.
- 49:39Okay?
- 49:40Electric engine
- 49:41stop.
- 49:42Electric engine
- 49:44accelerate.
- 49:46Okay?
- 49:47Something like that.
- 49:49Now I can create my nice car.
- 49:55Nice car.
- 49:57Okay, this class this car is going to
- 50:00have an internal engine of its own. That
- 50:02is going to be a what type engine type?
- 50:05That's it.
- 50:07Okay, cuz constructor uh I will not
- 50:09select anything.
- 50:11I'll just say that by default I'm going
- 50:13to say let's say engine is equal to
- 50:16new uh power engine for example at
- 50:19first. So, I use these interfaces and
- 50:22everything in diff- different classes,
- 50:23but overall class I'm using it like
- 50:26this.
- 50:27Now, you can add anything you want. If
- 50:29you want to add an engine of your own,
- 50:31so you can just do it like this.
- 50:34That's it.
- 50:37Okay?
- 50:39You can add a start over here. I can say
- 50:41public start Notice how I put this as
- 50:43private. Data hiding, right? Covered
- 50:46that previously.
- 50:48Public void start, it's just going to
- 50:50say engine.start.
- 50:53Now, if this engine that is initialized
- 50:55is power engine, it will call this. If
- 50:58it's electric engine, it will call this.
- 51:01How cool is that?
- 51:03Okay? Similarly, I can do stop.
- 51:09engine.stop
- 51:10No problem.
- 51:12Okay? Similarly, I can do
- 51:14um
- 51:16something like
- 51:18um
- 51:19start music. It's going to say
- 51:24What? I need to have a music player as
- 51:26well. So, then I'll say this car has an
- 51:28engine. This car also has a music
- 51:31player. So, I can say my
- 51:34um
- 51:36media
- 51:37um player
- 51:40something like that.
- 51:42Right?
- 51:43Um so, I can say this is equal to new CD
- 51:46player.
- 51:47Something like that.
- 51:50Okay, start music. Now I'll not say
- 51:52engine.start. This is the problem we
- 51:53were facing previously. When you did
- 51:57in the main car, when you were saying
- 51:58start or stop with even the if even the
- 52:01type of this was music like over here,
- 52:03type was music and you were saying start
- 52:05or stop, it was actually starting and
- 52:06stopping the engine. Now when I say
- 52:08start or stop, start music, it's
- 52:10actually not going to say engine.start.
- 52:12It's going to say
- 52:14player.start.
- 52:16Similarly, I can say stop music.
- 52:22player.stop.
- 52:27Okay.
- 52:28Similarly, you can say upgrade your
- 52:30engine. So you can say public
- 52:32void
- 52:33um
- 52:36upgrade
- 52:38engine and you pass an engine over here.
- 52:41Okay, engine engine.
- 52:43And you can just say
- 52:45what?
- 52:46You can say this.engine
- 52:49is equal to the engine that I have
- 52:50passed. That's it.
- 52:52How cool is that? You can change your
- 52:54electric to electric engine from power
- 52:55engine or whatever.
- 52:57Makes sense? Let's try to do it.
- 53:00So you have your
- 53:02uh
- 53:03car,
- 53:04which is your nice car.
- 53:06Nice car. Car is equal to new nice car.
- 53:10Okay, if I say car.start
- 53:14and then I say car.start music.
- 53:17So you can see it will be like I am
- 53:18starting and by default the engine is
- 53:21going to be what? I just called the
- 53:22default one, so power engine. It will be
- 53:24like power engine starts.
- 53:25And then it will be like music starts.
- 53:28Okay, here also it's running the start
- 53:30one. Here is also running the start one,
- 53:32but here it's running the player.start.
- 53:34Here it's running the engine.start.
- 53:37You can also change the engine.
- 53:39So if I change my engine, I can say
- 53:41car.dot upgrade engine
- 53:45and um
- 53:49I can just add a new engine over here.
- 53:54I'll have to create a new engine or I
- 53:55can just upgrade my engine in the car
- 53:57only.
- 53:57Okay? I can say upgrade my engine.
- 54:00And I'm going to say, let's say I'm not
- 54:02passing it directly. I'm just saying
- 54:04upgrade my engine is equal to what?
- 54:06This is equal to new electric engine.
- 54:10Something like that.
- 54:12Right?
- 54:13So now you're going to upgrade my engine
- 54:14and then I will if I start my car again,
- 54:17that's going to be like now I'm running
- 54:18electric engine.
- 54:22Electric engine started.
- 54:24Without creating a new object, you were
- 54:25able to create you were able to just
- 54:27change the part of the engine.
- 54:31Okay? As compared to this,
- 54:33here you would have to change the entire
- 54:35car in order to change the engine.
- 54:37Stopping the CD player was actually
- 54:39stopping the entire car.
- 54:44Okay?
- 54:45Hopefully you understand interfaces now.
- 54:49Right? I'll share all the notes and
- 54:51everything and I I covered everything
- 54:52like that I mentioned.
- 54:54Right?
- 54:56That's basically about it.
- 54:58Right?
- 54:59Cool.
- 55:01One more thing you can do is interfaces
- 55:02you can also extend.
- 55:04Right? So you can say
- 55:07create another package
- 55:09extend demo.
- 55:12So here you can create two interfaces,
- 55:13Java interface A.
- 55:16And you can create another one, let's
- 55:17say
- 55:19interface B.
- 55:22Okay? Something like that. And here if
- 55:24you have something like um
- 55:28void fun.
- 55:30Something like that. And here in B, I've
- 55:32also already talked about, right? The
- 55:34the variables and everything I already
- 55:35talked about what type of variables are
- 55:37available in interfaces and stuff. We
- 55:39didn't skip that. Static and final,
- 55:40right?
- 55:41So, yeah.
- 55:43Um here you can say extends A.
- 55:47And here you can have void
- 55:50fun for example if you have then
- 55:52that's like not going to work because
- 55:54it's like the same name. So, it does not
- 55:56really matter.
- 55:57Um
- 55:58Instead we can have something like
- 55:59greet.
- 56:01Okay, now let's create a class.
- 56:04Um main class that implements B.
- 56:09Then you would have to implement both
- 56:11the methods.
- 56:12Greet as well and fun as well. If B is
- 56:15extending A, you can imagine that it
- 56:17looks something like this. You can
- 56:18imagine that it looks something like
- 56:20this.
- 56:23Okay? So, you can extend it as well. All
- 56:26of these properties will be like
- 56:27together combined.
- 56:29And here you can then use it. Both the
- 56:32ones you would have to implement.
- 56:34You would implement the abstract methods
- 56:35of B as well and the class the interface
- 56:38that B is inheriting from that also
- 56:40something you would have to
- 56:42um do it. So, you can see I'm using
- 56:44extends over here not implements.
- 56:46Implements is only for classes to
- 56:47interfaces. Interface to interface
- 56:49inheritance it is extends.
- 56:55Okay?
- 56:59Cool.
- 57:01So, that's about interfaces. Now, before
- 57:03we move on to other thing like default
- 57:05interface methods that are like after
- 57:07Java 8 they were provided and like JDK 8
- 57:10and some in inter interface inner
- 57:12interfaces and stuff static stuff in
- 57:14this how static works over here which is
- 57:16pretty self-explanatory, but uh let's
- 57:18look at one more thing which is
- 57:20something that I wanted to share with
- 57:21you about this thing. Annotation.
- 57:24So, annotation also internally you can
- 57:25see is an interface.
- 57:28If you want to create your own
- 57:29annotation you can do it via this. At
- 57:32interface.
- 57:35Okay, it itself has some annotations.
- 57:37Target and retention or something like
- 57:38that.
- 57:40Okay?
- 57:43Cool.
- 57:46So, enums and stuff we'll look into the
- 57:47next lecture.
- 57:48But, here you can see that in the the
- 57:52this thing the annotations that we
- 57:54talked about, the checks basically, I
- 57:56explained this in the overriding lecture
- 57:58what this actually does. Um that is also
- 58:00internally an interface only. Interface.
- 58:03Okay? Public interface like this.
- 58:08Public interface override.
- 58:11Okay?
- 58:11That's what I want to share. Like
- 58:12internally override is also an
- 58:14interface. Now, starting from Java 8,
- 58:16there's actually possibility to provide
- 58:19some default implementation.
- 58:22Okay? To interfaces. Let me close
- 58:24everything. Let me just put this over
- 58:25here.
- 58:27So, default implementation means
- 58:29something like this. Let me just copy
- 58:30paste this.
- 58:36Extend demo two.
- 58:44Um rename.
- 58:49Factor.
- 58:56Okay?
- 58:58So, here let's say I'm not extending it.
- 59:02Um close all.
- 59:06Let's say not extending it and just
- 59:08having it like normally like
- 59:11void
- 59:12fun or whatever.
- 59:14Okay? And then I have a main class
- 59:15implements
- 59:16A {comma} B both, let's say.
- 59:18Right?
- 59:19Cool.
- 59:20So,
- 59:21starting from JDK 8, you can actually
- 59:24provide some default implementation. You
- 59:26just put default over here.
- 59:28Like this.
- 59:32I am in A.
- 59:35Okay? Similarly, you can provide default
- 59:38if you just run it like uh
- 59:40you just remove it like this. Right? So,
- 59:43here it's actually saying that uh okay,
- 59:45for implementing for B, let me just
- 59:47comment this out right now. So, even
- 59:49though
- 59:50there's an abstract method over here
- 59:51that I have like not really, you know,
- 59:54um
- 59:57it overridden over there's a method in
- 59:58this interface that I have not
- 1:00:00overridden, and it's not giving me an
- 1:00:01error because there's a default
- 1:00:02available over here.
- 1:00:04Okay, but what if I try to put the
- 1:00:05default over here as well?
- 1:00:07Now, we're getting an error.
- 1:00:10Okay?
- 1:00:11It's saying like it's collapsing. So,
- 1:00:13the initial problem that you're trying
- 1:00:14to solve, now we're getting it with
- 1:00:16this.
- 1:00:17So,
- 1:00:19um
- 1:00:20this is basically saying that it's a
- 1:00:22it's a it's just a special use feature.
- 1:00:24Okay? And the the original intent behind
- 1:00:26the interface still remains the same.
- 1:00:28The primary motivation Kunal Kunal, why
- 1:00:30would we want to do this then if it's
- 1:00:32giving us the same problem that we used
- 1:00:34interfaces to resolve?
- 1:00:36So, it's basically, you know, the
- 1:00:37primary motivation for this default
- 1:00:39method was to provide a means by which
- 1:00:41interfaces could be expanded without
- 1:00:44breaking the existing code.
- 1:00:46So, for example, you add another method
- 1:00:48without a body in an interface.
- 1:00:50Then you would have to provide the body
- 1:00:52of that method in all the classes that
- 1:00:54implement that interface.
- 1:00:57Does that make sense?
- 1:00:59Okay?
- 1:01:00So, for example, you may have one class
- 1:01:02that interme- in like in this class,
- 1:01:04it's implementing two interfaces. Okay?
- 1:01:07If each of these interfaces provide some
- 1:01:09default methods, then I'm getting some
- 1:01:11error.
- 1:01:13Okay?
- 1:01:16And obviously, the class's
- 1:01:17implementation will obviously be the
- 1:01:18priority over an interface default
- 1:01:20method.
- 1:01:25Okay, and we already got an error in
- 1:01:27case there were like two overridden
- 1:01:28methods.
- 1:01:30Okay.
- 1:01:31Cool.
- 1:01:35That's basically like the primary use
- 1:01:36case. Okay?
- 1:01:41So, you can expand the interfaces
- 1:01:42without breaking like some existing code
- 1:01:44or whatever.
- 1:01:45But, I would not like recommend using
- 1:01:47it.
- 1:01:47Um
- 1:01:48if you're getting some errors or
- 1:01:49anything like that, don't.
- 1:01:51Okay?
- 1:01:52But, if you have like multiple names,
- 1:01:54then don't. If you don't have multiple
- 1:01:56names, then you can.
- 1:01:58It's like an additional feature.
- 1:02:01Okay?
- 1:02:03Cool. Let's move forward. Okay, now
- 1:02:05let's talk about one more important
- 1:02:06thing, which is uh static methods over
- 1:02:08here.
- 1:02:09We know that static methods do not get
- 1:02:12inherited. We know that.
- 1:02:15Okay? You know static methods will never
- 1:02:16get inherited. Why? We already covered
- 1:02:18it in overriding stuff and
- 1:02:21in the second video of object-oriented
- 1:02:22programming programming principles. It's
- 1:02:24pretty self-explanatory as well.
- 1:02:26Inheritance means like if you inherit
- 1:02:27it, you have to override. Overriding
- 1:02:29means it depends on objects because it's
- 1:02:31runtime polymorphism, and static methods
- 1:02:33do not depend on objects. That's why
- 1:02:34they cannot be inherited. Simple
- 1:02:36explanation.
- 1:02:37So, if you have static method over here,
- 1:02:40and they cannot be inherited, it means
- 1:02:42that they cannot be overridden. It means
- 1:02:44they should have a body over here.
- 1:02:46Because the static methods in interfaces
- 1:02:48are the ones that will be used only. You
- 1:02:50can't override those or whatever. You
- 1:02:51can't inherit those. It will be used
- 1:02:53from interface only.
- 1:02:55That's why static interface methods
- 1:02:58should always have a body.
- 1:03:06All these reasons because you can't
- 1:03:09static
- 1:03:10void
- 1:03:11greeting.
- 1:03:15So, you can't have it something like
- 1:03:16this. This is not allowed. Okay? Body is
- 1:03:18required.
- 1:03:21Hey, I am static method.
- 1:03:24You can call it via whatever you want.
- 1:03:27Okay?
- 1:03:31Main
- 1:03:33object is equal to new main.
- 1:03:36Okay? And here you can call it via just
- 1:03:37the name of the class because we know
- 1:03:39static methods are called by the name of
- 1:03:41the class. So, it's actually going to be
- 1:03:43like it's not going to be inherited or
- 1:03:44anything. A. greeting. Like that you
- 1:03:47have to call it. You have to call static
- 1:03:49and you have to call these via the stat
- 1:03:51via the interface name. Okay?
- 1:03:55Call via the
- 1:03:56interface name.
- 1:03:58Simple as that. No problem.
- 1:04:00That's it.
- 1:04:03All right?
- 1:04:05Cool.
- 1:04:06One more thing I want to share is
- 1:04:09when you are overriding methods, right?
- 1:04:11When you're overriding like this.
- 1:04:12Here I know it's like uh
- 1:04:16abstract and whatever. But uh
- 1:04:19the the overridden methods, this is not
- 1:04:22just for interfaces. It's like in
- 1:04:23general. The access modifier for the
- 1:04:26overridden methods should be same or
- 1:04:28better. So, for example, if the parent
- 1:04:30class, we talk about the previous
- 1:04:32inheritance, if the parent class uh if
- 1:04:34in the parent class the method was let's
- 1:04:37say protected, then in the overriding
- 1:04:40the overridden method should be
- 1:04:42protected or something better like
- 1:04:44public. It cannot be more restrictive
- 1:04:47than protected. It cannot be private.
- 1:04:50Okay? Should be better than that.
- 1:04:53If I try to put this as private, see
- 1:04:54what happens. Getting an error.
- 1:04:57Right? Because I know
- 1:04:59that uh
- 1:05:04I put this over here, public. We know
- 1:05:06this is public and abstract by default.
- 1:05:09So, you can have public and better than
- 1:05:11public, but no, nothing is better than
- 1:05:12public.
- 1:05:14Right?
- 1:05:15So, that's the reason we have You can't
- 1:05:17add this public. If these were like
- 1:05:19protected or something, you can have
- 1:05:20protected and something better than
- 1:05:22protected. Nothing less
- 1:05:24Nothing less restrictive can be
- 1:05:26available over here.
- 1:05:29Okay.
- 1:05:38Key class
- 1:05:45Cool. Sound good?
- 1:06:00Same thing
- 1:06:01because
- 1:06:02this is default modifier and that is
- 1:06:04also less restrictive, not allowed.
- 1:06:07Okay?
- 1:06:09So, that's one more rule I wanted to
- 1:06:10share about.
- 1:06:14Cool.
- 1:06:16Let's move up, okay? One more thing
- 1:06:17about interfaces I want to share is like
- 1:06:19nested interfaces, okay?
- 1:06:21Package
- 1:06:23nested nest it.
- 1:06:27So, let's say you have a class called A
- 1:06:29and inside that class you have an
- 1:06:31interface
- 1:06:33nested
- 1:06:35interface.
- 1:06:36You have something like
- 1:06:38public
- 1:06:40interface
- 1:06:42you know, something like um
- 1:06:47nested
- 1:06:50interface
- 1:06:53something like that.
- 1:06:55Okay?
- 1:06:57Cool.
- 1:06:59We have let's say a function called is
- 1:07:03um odd or even.
- 1:07:05The a is odd or even and you just pass a
- 1:07:08number over here.
- 1:07:10Okay, that's it.
- 1:07:13Now, you have a let's say a class B
- 1:07:15and that implements A. nested interface.
- 1:07:20You'll have to obviously override
- 1:07:21like this.
- 1:07:23Okay, so you can say what is an odd
- 1:07:25number? An odd number is if the number
- 1:07:27and with one
- 1:07:29is equal to equal to one.
- 1:07:30We have covered this in bitwise
- 1:07:31operators.
- 1:07:33Okay, that is pretty much about it. Yep.
- 1:07:38Now, I can create another class. You can
- 1:07:39create a main class if you want.
- 1:07:41Right? So, I can create a class main
- 1:07:44public static, or I can create it
- 1:07:46separately also. It does not
- 1:07:48really matter.
- 1:07:52Main like this.
- 1:07:53Public static void main.
- 1:07:55Okay.
- 1:07:56Then I can say let's close this, close
- 1:07:59this,
- 1:08:00close this, close this. Just
- 1:08:04put it over here like this. I can say
- 1:08:09B object is equal to new B.
- 1:08:13Okay. Then I can say object. is odd
- 1:08:17five.
- 1:08:19Um going to print it.
- 1:08:25Run it.
- 1:08:30True.
- 1:08:31Object. is odd seven.
- 1:08:38True.
- 1:08:39What am I doing? Six.
- 1:08:41False.
- 1:08:42Should show false.
- 1:08:44Okay.
- 1:08:45So, that's how it's working. Now, one
- 1:08:47new thing over here that I would like to
- 1:08:49share is that uh the only difference
- 1:08:52that this has from like a top-level
- 1:08:53interface is that the um
- 1:08:59the nested interface can be declared as
- 1:09:00like public, private, or protected.
- 1:09:03But, the top-level interface
- 1:09:05has to be declared as public or the
- 1:09:08default one.
- 1:09:09Okay, that's the difference.
- 1:09:12Cool.
- 1:09:13All the standard stuff applies.
- 1:09:15Okay.
- 1:09:16You have to create an object of B and
- 1:09:17stuff and use it like that. Yeah.
- 1:09:20I'm using A.nested interface, means A
- 1:09:23inside A there's a nested interface,
- 1:09:25using that.
- 1:09:28Okay.
- 1:09:29So, that's nested interface. Okay. Well,
- 1:09:32that was about this video, but this was
- 1:09:33a very important video. We learned about
- 1:09:35a very important concept of interfaces,
- 1:09:36abstract classes, and stuff. Um yeah, in
- 1:09:39the next video we'll learn about
- 1:09:41generics and stuff, collection
- 1:09:42framework, and all these other things,
- 1:09:44and then we move on to some like
- 1:09:45advanced data structures after that
- 1:09:47because now we know OOP, so we'll be
- 1:09:48able to write our own data structures,
- 1:09:50linked list, stacks, queues, and dynamic
- 1:09:53programming stuff, graph algorithms,
- 1:09:55trees, and heaps, and hash maps. So, we
- 1:09:59will be literally writing these on our
- 1:10:02own using classes. That's why I had to
- 1:10:04do object-oriented
- 1:10:06programming first. So, you will not only
- 1:10:08just learn how to use it, you'll
- 1:10:10actually learn how to implement it as
- 1:10:12well, every single function. So, you'll
- 1:10:14use the internal one as well, the one
- 1:10:15provided like collections framework, but
- 1:10:17we'll actually create our own as well.
- 1:10:19So, make sure you like, share, and
- 1:10:20subscribe, and share about it on
- 1:10:21socials,
- 1:10:23so that it raises awareness, and share
- 1:10:24with your friends. And yeah, make sure
- 1:10:27you like, share, and subscribe, and
- 1:10:28comment as well right now. Um and I'll
- 1:10:30see you in the next one. It will really
- 1:10:32help me, so I would really appreciate
- 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.