- Podcasts
- The Java Developers Hub
- Java Abstraction & Polymorphism Explained: Abstract Classes vs Interfaces | Full Java Course
Java Abstraction & Polymorphism Explained: Abstract Classes vs Interfaces | Full Java Course
The Java Developers Hub
- December 3, 2026
- 1 h 28 min
- Episode 41
About this episodeUnderstand two fundamental concepts of object-oriented programming in Java: abstraction and polymorphism. This episode explores how Java…
- What abstraction means in Java and why it matters.
- How polymorphism works in object-oriented programming.
- The difference between compile-time and runtime polymorphism.
- What abstract classes are and how to use them.
- What interfaces are and how they define contracts.
- Abstract classes vs interfaces: key differences and use cases.
- How method overriding supports runtime polymorphism.
- How to design flexible and reusable Java applications.
- When to choose an abstract class over an interface.
- Beginners learning Java and object-oriented programming.
- Java developers strengthening their OOP knowledge.
- Computer science and software engineering students.
- Developers preparing for Java technical interviews.
- Programmers interested in writing maintainable and reusable code.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Abstraction & Polymorphism Explained: Abstract Classes vs Interfaces | Full Java Course
Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to go one step ahead in our object-oriented programming. And we are going to talk about abstraction and polymorphism. In the last lecture, we talked about encapsulation and inheritance. And we discussed how they are very important pillars of any oops. concepts, right? Similarly, the next two concepts which are very important and act like pillars for OOPS are abstraction and polymorphism. Today, we will understand this in-depth. If you have ever been confused about what is abstraction and what is the difference between encapsulation and what is polymorphism, then today all your problems will be solved because we are going to understand this topic in-depth. You are going to enjoy it. So, let's go to the screen and see. So, we are talking about abstraction. So, first of all, we will understand abstraction in a real-life scenario. And after that, we will relate it to how Java implements it. So, first of all, the most common question is What is an abstraction? First of all, let's start with the definition of abstraction. And we will understand that definition and study it in depth. So, first of all, let me tell you the definition of abstraction. Here I have written the definition of abstraction which is very simple. The process of focusing on what something does while ignoring how it does that. What does it mean? Focusing on what it does. Rather than focusing on how he does it. This is what we call simple word abstraction. Okay, what does it mean? Look, let's understand this from a real life scenario. The entire human population works on abstraction. The entire human population. Because you think, our world is abstract only. If we want to understand the reality of this world, this complex world, then we can't understand the whole thing together. So many details, our brain can't process. So what did we do? What did our civilization do? We started understanding things abstractly. Right? How? For example, if I tell you, car, very simple car, it's a real life object. So when we think about the car, we think about its abstract behaviors. We know what the car can do. The car can drive. If more things come into it, then we can apply brakes in the car. We can accelerate the car. These are all its functionalities. What you can do with the car. Apart from this, there will be its data. How does the car look? Let's say, what is its color? What is its type? Is it an SUV? Or hatchback, sedan, etc. Or you can do more things like what is its price, etc. So, we either know some data about the car, which we call data, and you know how it is represented in object-oriented programming, using variables. Or we know some behaviors about the class, like it can drive, brake, accelerate, etc. But when we think about the car, we don't think about the 30,000 parts inside it. Right, let's suppose that it is made up of 30,000 parts. So whenever we think that we accelerate the car, we don't focus on this. When we press the accelerator on our car, then what will happen in the background? You know, how does the combustion engine work? How does our fuel gauge work? So we basically don't understand the internals of the car. We understand what the behavior of the car is. Or what is the basic data of the car? Right? So what did we do to understand the car? We made the car abstract. Because obviously, if every person started understanding all the mechanics of the car, you think, if every human started understanding all the mechanics of the car, then every human won't have to become an engineer, right? To understand the whole car. In fact, I go one step further. Every engineer who is in this world, who makes a car, Obviously, to make a car's parts, he will have to understand physics, how it works, we said combustion engine, how it works, right? But actually, what is being applied in it? Physics is being applied. So, he will have to understand all the physics as well. So, what will the engineer do? He will abstract the whole physics. He will assume that I might have I don't know the exact physics of it, but I know how to make it work by combining its parts, right? So, if it didn't happen, if there was no abstraction, then every human would have to become an engineer, every engineer would have to become a physicist. In fact, every physics... or physicists, they have to become mathematicians. Because all physics is based on mathematics. So if every person in this world becomes a mathematician, then he will be able to understand the whole world. But that doesn't happen. A car can be driven by a normal person who doesn't know anything about the car. Or a person who knows everything about the car. So I just wanted to explain to you that our whole world is made up of abstractions. We think of everything as abstractions. Okay, let's take one more example. When I tell you ATM machine, what should come in your mind? What can I do in ATM machine? You will think that ATM machine means I can deposit my money in it, I can withdraw my money in it, or I can check my balance. So check balance. So, what are all these? These are the functionalities of an ATM machine. What can it do? What we discussed in programming, what do we call it? We call it its behaviors. Right? And if we had to represent it in computer science through classes, then what would we make of these behaviors? We would make these behaviors into methods. In a class, right? So look, how did we see this world? We saw that an ATM machine does deposit, withdraw and check balance, right? Did we understand its internal? No, we didn't understand. We didn't see how much its weight would be, how its electronics would work from inside. Why do we have to understand all that? Right? It doesn't make any sense for us. So again, what I mean to say is that the whole world works on abstractions. If you understand this, then you will understand the core principle of abstraction. Okay? Now, one more thing. We have always said, in fact, we discussed this in the last class, that the object-oriented programming, its work is to simulate the real world. To simulate the real world. But this is half-truth. Actually, object-oriented programming doesn't simulate the real world. It doesn't simulate the exact reality as it is. It simulates the perception or idea that humans think about reality. Kehne ka matlab ye, ki hum puri ki puri reality ko as it is to represent nahi kar sakte programming me. Reality ka matlab kya hua? Let's say, main bolta hu, teacher. Ek object hai, real life. To hum uske behaviors ke baare me sochte hai. Hum sochte hai, teacher ka kaam hai, padhana. To teacher, teaches. Right? Waise hi, agar hum bole car. Car ka kaam kya hai? Car ka kaam hai, drive hona. To car, drives. Thik hai? Hum basically, behaviors ke baare me sochte hai. To object-oriented programming, hume pata hai, real world ko simulate nahi karti as it is. It simulates what? Humans. think about reality. What we think about reality, not the exact reality, right? How do we think about reality in behaviors? We think, a teacher's job is to teach, a car's job is to drive. In fact, if I leave real life objects, in fact, if I talk about programming objects, if I talk about a programming object, let's say button, okay? We make buttons in programming, in front end. What is the job of button? To click, right? We think about behaviors, right? Now, if we think about the behaviors and the reality, then we have to represent the teacher as an object. When we represent in programming, we have to represent everything. A teacher is a human being. A human being is made of cells. It has organs. There are many things. The whole world is complicated. So, what did we say? We said, we don't want all this. We just have to represent the idea of an object. We have to represent the idea of a teacher, that he teaches. So, what did we do in programming? We made our class, class teacher. And in that, we will have the same behaviors as methods which we need. For example, one method will be teach or let's say one method will be take attendance which is basically the work of the teacher. We have represented that as behaviors. We can also represent some data of the teacher. It will have some name. Right? Let's say, it will have some age, it will have some gender, etc. So, we can represent its data, its behaviors. But which data, which behavior? Which basically we want. Just that behavior. Right? So, we have to understand two things. That abstraction says two things. Okay? What will it say first? It will say that obviously it is very complicated to understand the whole world. So if we talk about an object in the whole world, let's say for now we will talk about a car. So it is very difficult to understand the whole car as it is. So we will make the car abstract. So basically we will not represent all the parts of the car in programming. We will only represent some parts of the car which we need. So represent whatever necessary. So, we have to represent what is necessary. We don't have to represent the whole car, we have to represent some parts of it. Now, what does the second thing say? The second thing says that whatever we are representing, for example, what are the important things of the car? That the car can accelerate, sorry, let's write it here, that the car can accelerate, right? brake, clutch, etc. These are the operations that we know. We don't need to know how combustion engine works, how fuel gauge works, how brake pads work from inside, etc. We don't need to understand all this. We need to understand accelerate, brake, clutch because we need to use it. So basically, whatever we want to use, in fact, whatever we we want to model, whatever we want to model, we don't want everyone to understand its background but they can use it. Right? Like car manufacturers don't believe that even if you don't know that when you put your foot on the accelerator, what happens from inside? But even then you can drive it. Right? So I write this like this, that even what we model, whatever we are modeling, We don't want the whole world to know how it is implemented. They can just use it. So I wrote it like this. So see, I wrote that we don't want everyone to know how it works. We just want them to be able to use it. So what do we have to do? We have to hide some things for them. For example, what did the car manufacturer do? They made a car structure for you. You can sit in it. And it gave you some interfaces, some switches, some remotes with which you can operate. It gave you a steering wheel. If you turn it left or right, your car will go left or right. Brake, Clutch, Accelerator Gearbox Interfaces So you don't need to understand how a mobile or laptop works from the inside. It has given you an interface, a screen, through which you can see. So if I represent a mobile, I will think of a mobile through behaviors. What happens in a mobile? It can display some things to me. It can do some other operations. But I don't want to go into its internals. How does it do it? I don't want to understand it. Now if all these things are clear to you, then we can focus on this. That our language is Java. How does she implement this abstraction concept? Now look, here many people get confused who come to read abstraction. Because they understand the real life view that I have explained to you. It's about hiding the implementation details that we discussed. That things work when not known to everyone. Let's hide that. But when we represent it programmatically in Java or any language, then a little confusion starts. Don't have that confusion with you. We will go step by step. First of all, let me tell you a crystal clear thing. In Java, abstractions are also implemented in two ways. First, we call it low level abstraction. Low level abstraction. And second, we call it high level abstraction. Now, what is low level and high level? Now, low level is the same which we were discussing till now, that hiding implementation details. Basically, hide implementation details, whatever you are using, because you don't need to know everything, which we talked about. High level abstraction says, separate, separate what from how. Now, this is very interesting. Separate what from how. Separate what something does from how something does it. Okay, what does this mean? We will see this at the high level. Now, we will see how low-level abstraction is implemented in Java. Now, again, to understand our low-level abstraction, let's take the example of our car. So, let's say I made a class car. Okay. I have made a car class here. I want to represent a car as a class for programming. Very good. Okay. So, to make a car, we should have some data. Right? So, let's say we have a string type. Right? We can have some more data which we will ignore now. We will move towards its behaviors, towards its methods. You know, there will be some functions in a car. Let's say, first of all, let's assume A basic function of car will be start. You can start the car. It will have some implementation. We have written some code here. Here the bracket will open. Obviously, here I am not writing its return type. Void or whatever you want to give. But you understand. Now let's say I will perform some operation here. In the same way, we will have in the car. Accelerate is a function. It will also do something inside. Maybe it will take some arguments too. I am just writing a little pseudo code of it. Similarly, we will have a break function. We will have multiple functions. There can be many more functions. So let's assume we have all these functions. In what? In a car class. Okay, very good. Now suppose whenever I want to use this class, let's say I have my main code, my main function. Whenever I want to use this class, we have to make an object. And we make an object like this. Let's say car. C equal to new car. By doing this, we make an object. And after that, I can use all the functionalities of this car. I can write C dot start. Our car will start. We expect. C dot accelerate. Our car will catch the speed, we expect this. C.brake will be applied to our car. Right? Something like this will happen. Now you think that our main function, this is a different class. It comes in a different class. And our car comes in a different class. And let's say a car class is written by some other developer and is in some other file. You just have to use that car class. Okay? So how do you do it? You make an object of car, carC equal to newCar. And now you can use all its functionalities, c.start, c.accelerate, c.break. And you expect that it will be what it should be. If I start, then my car should start. Suppose you are representing a car in a game. There is a game in which the car keeps running. So if you start it, it will start. If you accelerate it, it will catch the speed, etc. So you would expect that if I write c.accelerate, then my car will accelerate. You don't know how the accelerate function works from inside. You didn't open the car and see it. You don't know. You don't know how the brake works from inside. You didn't open it and see it. You just know that if I apply the brake from the object of this car, then the brake will apply. This is what we discussed in the real world. When you drive a real life car and apply the brake, then you don't know what actually happened from inside. But you know that if the brake is applied, then the car will stop. You don't care about the implementation details. As it is implemented, all you care about is how to use it. This is low-level abstraction. And how does Java implement it? Simple. Through classes. We have already read this. We have read this in encapsulation as well. Encapsulation says that it will basically connect all the data and all the methods together and will also put access modifiers on the data so that the people outside If it is data sensitive, right? That was the second part of encapsulation. But one part of encapsulation was that it keeps data and all the behaviors together, encapsulated. And through that, our low-level abstraction follows. To follow low-level abstraction, we don't have to do anything else. Because for us, where we are in the main, we don't need to know how the start function works to write c.start. We just wrote it. That work should be done. Because as soon as we write c.start, its start will be called and whatever is written in it will be implemented. Right? So here Java handles low-level abstraction like this. Okay. Now let's move towards high-level abstraction. Here Java becomes very interesting. Okay? High-level abstraction. Now Java implements high-level abstraction using two things. One has abstract classes and the other has interfaces. So these are the two ways in which Java implements high-level abstraction. But what are these classes? What are abstract classes? And what are these interfaces? We will read it slowly. First, let's understand what high-level abstraction says. What did we discuss? It says that we have to separate what from how. What to separate from how. What and how is here? What does something do? We have to separate that how does something do something. Okay. What does it mean that we have to separate it? Okay. Let's understand the meaning of this in this upper class which we had made up. First of all, till now all the classes which we were discussing, in which we used to write normal classes, we used to call these classes concrete classes. What do we call these classes? Concrete classes. Okay, now you will understand why we are calling it a concrete class. Okay, now look, we used to call them concrete classes, how we used to make their object, car c equal to new car and we used to be able to do its operations, start, accelerate or break, whatever we had to do. Now look, I tell you, what is this c, what is this, this is a reference, we have discussed, this is a reference variable. And it points to the object of the car in the heap memory. So actually the object of the car will be in the heap memory. And the reference variable in the stack will be c. And c points to it. Now Java says that this c is not tightly coupled with this car. Tightly coupled means that whenever I write c.start, or when I write c.accelerate, then always the same functions will be used. In c.accelerate, this will be called, in c.start, this will be called. Now let's say this car is c, I will not make it a c object, I will make it honda city. I have made a car honda city. Honda city is a car. Now when I call this honda city.start or honda city.accelerate, then what will happen? This brake, this accelerate, and this start will be called. So I am saying that my reference variable C or Honda City is tightly coupled with the car, with this particular new car. All functions will always be called here. So even if I don't know how things are implemented from inside, this means I don't know how they are implemented from inside. Even though I only know what they can do, I know their what. But still their what and how are tightly coupled. Tightly coupled. I hope you understand how. Because whatever is what, it has a specific how. What is what? C dot start. What is its how? Here is the method. Similarly, what is zero-tax-celerate and how is YERA method for the same brake. So, whatever they can do and how they can do it, they are still connected to each other. But suppose, this car code that we had written, it was a normal petrol car code. But now-a-days, electric cars have come. If I want to write a code for an electric car, Now, obviously, you know, electric cars work in a very different way. There is no combustion engine in it. It runs on electricity. So, there won't be a gear system in it. There will be a lot of different things in it, right? So, for that, I will have to write a different class. Like we wrote a class car. Similarly, let's say, what I will do? You assume that the class written here is fuel car. about fuel car. So, I am writing a small class. Okay. In that, I am writing a method. I am just writing drive. So, this will be a different type of drive. Here, a different code will be written. Similarly, I will have to make another new class. Class, let's say, electric car. And what will happen in this? There will be a method called drive in this too. But its drive will be different. There will be a different implementation in this. Right? Obviously, you will get the same result. What is the result? That your car will move forward by driving. Okay? You will be able to drive the car. Etc. Etc. Now consider drive as a big function. It is possible that you want to make multiple functions. Accelerate. Brake. apply gears, apply brakes, etc. But for now, I am assuming a specific common function drive. That there will be a drive in both and the implementation of both will be different. So, I call this as implemented in the first way and this is implemented in the second way. The what of both is the same. What does it do? It moves the car forward. But how is it different? Now, let's go back to our main code. This is our main code. Now, if I want to make a car, which is an electric car, then I will have to make an object of electric car. I will write electric car. I will make a reference of it. Let's say EC equal to new. Electric car etc. etc. which I write here. Similarly, if I want a fuel car, then I have to make a different object of fuel car. Fuel car, let's say, FC equal to new fuel car etc. etc. which I write here. Now, I can call its methods through this EC and FC. Now, when I write EC.drive, So there will be some different things from inside. And if I write fc.drive So there will be some different things from inside. These two methods will call different implementations. What is ec.drive? It will call the electric car method. And fc.drive will call the fuel car drive method. Right? So you are understanding the problem. My what is tightly coupled with how. Because if I want to apply drive on ec, electric car. So its drive method is always called. And if you want to apply the drive method on FC, then it is always called. Right? So I have to make a new class every time. Fuel car, electric car, tomorrow I will make a diesel car. Or suppose tomorrow a car that will run on water will also come. So I will make a different class for that too. Water car, etc. So you are seeing how many classes I have to make, how many objects I have to make, how many references I have to make. Java says, why don't I bring another level of abstraction, a high level of abstraction, that I just make a car. I'll make a car. Its reference variable is C. Equal to. And it can point to anyone. It can point to an electric car. So, I can write new electric car. Or it can point to a fuel car. So, I can write new fuel car. Or it can point to any car. Now, you don't think that we have read somewhere. that we have a reference carC equal to new, here one type of object can also come, let's say electric car, or fuel car, here that can also come, so multiple type of objects, how can one reference be assigned, through inheritance, right, we had studied this, that what if we work, we make a simple class, car, and we inherit it, from two classes, so one will be normal, let's say fuel car, I am writing it as FC, it will extend this car, Something like this. And here we will have a class. Let's say EC, electric car. And this will also extend our car. Something like this. Right? So see what I did. I separated the implementations of both of them. Using one parent class. And parent class became our car. Okay? So, I have made this hierarchy. We have studied this in inheritance. And you know what inheritance says? An object of parent class can refer to an object of child class. Basically, it means that I can write in code car C equal to new. Now, in front of this new, I can write electric car or I can write fuel car. I can write anything. Why? Because electric car and fuel car are both child of car. And this C is the reference of car. So, because this is the reference of parent class, This reference can point to an electric car or a fuel car. Using this principle, We will change a little bit in this. But using this principle, we can go one more level up in abstraction. How? Look, now you assume that our fuel car and our electric car, obviously these two are very different. These two have different engines, their mechanism is different, obviously. But there must be something common in them. For example, I say that if you apply handbrake in both, then it will be the same procedure. Right? You will lock the rear tires with the light. Okay? So I am saying that this common method, I will write it on top only. Apply Handbrake, applyHandbrake. So this is a common method which I have defined above only. So basically, if applyHandbrake exists above, then it doesn't need to be implemented again by itself. So we can skip this. What we can do is, we can implement our drive method. So I will write it exactly. Here it will have a drive method, something like this. And here it will have a drive method. Something like this. Because we know that its drive is different. So I don't represent the drive here. I just represent a common method, applyHandbrake. And the rest of the applyHandbrake will be found automatically using inheritance. Now see what will happen. This car's reference C, whether it points to an electric car or a fuel car, it can also call the applyHandbrake method in it. And it can also call their respective drive method. It has both the functionalities. Right? Now, if we understood till here, then we will have to understand why we did this, what has happened till now. What has happened is, till now, between what and how, the tightly coupledness that we were seeing, we removed that. We said, now, these two are not tightly coupled. How not? Now, see. Now, after going and My C reference object can dynamically decide at runtime whether I want to point the electric car or the fuel car. What does dynamically mean at runtime? We will see this in the next topic Polymorphism. But for now, let's assume that C can point this as well as this. So if C is pointing the electric car and you write C.drive So which method will be called? This one. Electric car. And if C points to fuel car and then you write C dot drive. Which method will be called this time? This time it will be this method. Fuel car method. Now you saw what you did. That something does what? Drives. And how does she do it? She can do it like this or like this. You have separated those two. What does she do and how does she do it? Separate between each other. Because what is the same? She drives. But how does she do it? It depends. Are you pointing at the electric car or the fuel car? Right. So basically, this way, you can use inheritance to go to high level abstractions. But brother, we used normal class to apply low level abstraction. use inheritance and apply it to high level abstraction. Then why are you saying that we have abstract classes, interfaces for abstractions and why did all this come? And how do we implement high level abstraction together? Okay, now let's go to that. Now if you have understood so much, then it's time to understand abstract class first. Okay. Now look, because its name starts with abstract, then Java must have thought that it will focus on abstraction in some way. It will try to implement abstraction in some way. High level abstraction, right? Okay. What will it do? First of all, let's understand whether there is an abstract class. Abstract class is a normal class. So if I was writing a class here, then I am making a normal class here. Car. Okay. Here I made a normal class. Now what I do in a normal class is that I declare some methods. So now we skip the variables, we say let's say car, now we don't go to drive, we go to specific methods. So let's say one method of car will be start. It will have some implementation. One method will be let's say accelerate. It will have some implementation. And so on. Let's say let's make one more, break. It will have some implementation. Something like this. We used to make this a normal car. Okay. So if I make it a little smaller like this. So what we did here is, I am telling you the normal classes, we will understand the abstract class after this. So what I did is, I made a normal class like this. Now suppose if another car, which was our fuel car, we used to inherit it, then how did we write its structure? We make it once. So see, we used to write its structure like this. We have a class car, we extend it as fuel car. And there can be some methods of its own. Now I had told you that What if this car defines everything by itself? How the car starts, so something generic would be written. How the car starts, how it accelerates, so a generic implementation would be made. Let's say there would be a print here. Car accelerated at 10 km per hour, something, something. And the car breaks with the brake. But fuel car, because it's a different type of car. And if we make another new car here, if you assume that another electric car is also made here. You just assume that we will not make it complete electric car. Extends car. Okay. This will also be the same. So, here a different type of car will be made. So, you also know that this will accelerate in a different way. Right? So, because fuel car accelerates in a different way. Don't we want that it should have its own accelerate function? It should not use this accelerate function which is already made. So, what will it do? It will define its own accelerate function. Something like this. Why did we do it? Because the way it accelerates is very different from our generic class. And there is no sense in accelerating the generic class. We have done a useless implementation here. Because I am telling you a car. You will ask which car. But I am telling you to accelerate it. Write some implementation of it. Write some logic. What logic will you write? You will write a generic logic. Let's say a car is accelerating from 10 km per hour. How is it happening? Is it an electric car or a fuel car? What is happening from inside? You don't know, so you wrote a generic implementation. So we must have done the same here. But since the fuel car is a specific car, it knows that when I accelerate, what will I have to do from inside? Now to accelerate, let's see how the combustion engine will work from inside. It needs to know all this. So its implementation will look different. So if I say that here, the implementation code will be different. In the same way, the implementation of the accelerator will be different in the electric car as well. Something different will be written here as well. So if you think about it, it has its own implementation here and here as well. Because the car doesn't tell us anything specific about what type of car it is. It is giving us a generic idea of the car. We are making concrete classes. Now you must be understanding what a concrete term is. An electric car, a fuel car, tomorrow let's say we make a diesel car. Okay? These are concrete classes which will actually tell you how to separate accelerate from sub. So why don't I cut its definition and terminate it here? It means that you don't need to define it. You just declare it. Now, as we used to declare and define variables, if we used to write int x and then in the next line we used to write x equal to 4, then we used to say this is declaration and this is definition. Actually, to provide a value to it. Similarly, what I did, I just declared a method here. That there will be a method called accelerate. Now it has given its own definition. It has given its own definition. Now whenever we will create its object. How will we create its object? Whenever we will write car C equal to new. And what will come here? Electric car can also come here. Fuel car can also come here. So if we write electric car here. And accelerate C dot. Then this will be the accelerate call. And if we write fuel car here. and then we do C dot accelerate, then this will be the accelerate call. We have discussed this already. So, you understood that there is no need to define this accelerate here. Same goes for brake. You know that if there is an electric car, then it has regenerative braking. You can charge the battery through the brake. So it will have a different break implementation. And let's say it will have a different break implementation. So why should we define the break? We should just declare it. Is it possible that the compiler will not throw an error? Because obviously, there is a syntax of a method. You put parentheses and then open curly brackets and write some code in it. But you have terminated it. Yes, in this case, our Java compiler will throw an error. What is this? This is a concrete class, in this you have to define the method. But here we use abstract class. I just have to put a keyword in front of this accelerate function, that keyword is abstract. Which will tell the Java compiler that this method is an abstract method. Abstract means I am just declaring it here, not defining it. The responsibility of all classes will be to define it. Whichever classes will inherit me. Same because here I have just declared break. So here also I will put let's say abstract. Now again Java compiler will say that I am just declaring. The responsibility of defining all classes. But if you will see here. So I have still defined start. Not declared. No problem you can do this. Define some methods. Define some methods here. Declare some methods here. Because I am assuming that. Any car, be it an electric car, a fuel car or a diesel car, starts in the same way. Obviously, you can also say that it's different in this too because the start and engine are different. So, the start will be different in both. Now, let's just theoretically assume that the start will be the same in both. The start will be the same in both. Since both are happening in the same way, then we don't need our own start function. Here common function start is written and it comes automatically through inheritance. So if I can call the object of the fuel car as c.start and the object of the electric car as c.start, then you know that through inheritance. So we left the start here. So we won't write abstract after this because it is fully defined. Now the rule of java is that if any method is abstract in your normal class, then you will have to make that class abstract. That is, the keyword will be abstract in front of this class. This is the rule of Java. You cannot write a class in which one method is abstract and the class is normal. The abstract keyword will be applied in front of that class. This is the rule of Java. Because let's understand this once. Now think, if you write an abstract before any class, will you be able to make an object of that class again? No, you won't be able to make it. Now I can't make an object of this class car. Put a basic first thought principle. Because if I made its object, if I could write like this, cars equal to new car. If I wrote like this, not electric car, not fuel car, just new car. So if someone calls c.accelerate there, then what will happen? Accelerator is not defined, how can you call it? So, because there is no confusion, Java says that you can't make an abstract object, you can't instantiate it, you can't make an instance of it. Similarly, this class car, because it is abstract, you can't create its objects. If you want to use these glasses, you can't make an object, then what will you do? You will extend them using child glasses. So we made a fuel car and an electric car. Both extend this car. And what happens when you extend it? These three functionalities come in it too. So start came in it too, accelerate came in it too, brake came in it too and so on. But, because accelerate and brake were only declared here, not defined, now it is the responsibility of the child class to define these two. Suppose you made an abstract class and only start is defined here and accelerate and brake are declared. your child classes, let's say you didn't define accelerate, you didn't define anything, then your compiler will throw an error, that the electric car and the fuel car, because they are inheriting an abstract class car, it is your responsibility, it means the responsibility of fuel car and electric car, To give the definitions of the accelerate method and the break method. So what did you do? You forced these two classes. That I am forcing you. Give the definition of the abstract methods in your parent class. Because if your objects are created. Then I would like the accelerate and break to be called. If these two also don't want to give it implementation. No problem. You make these two abstract too. You can also write this as abstract. This can also be done. In Java, multiple abstract classes can inherit one abstract class. Now, after this, put another inheritance. Let's say, electric car also inherits a different car. Similarly, a different car inherits a fuel car. You can go deep into inheritance. But we don't want to study there. We just think that we didn't put any abstraction here. We have accelerated. And in the same way, we have to define break here. So, I make it small. Break is defined here. Break. Here also it is defined. Once you have done this, now your code is ready to run. Now you have to make an object. How will you make an object? We know that we will write cars equal to new. Here we can also write electric car, fuel car. And when we call c.accelerate or break, then depending upon what is written here in new, its accelerate and break will be called. We have separated what from how. So this is the story of Puri Ki Puri abstract class. Let's go fast and see it in code. So, see here we have made our own project demo.java, our own class. So, first of all, let's make our own class. Let's say, we made a class car. And what we do is, we make some methods in it. I am ignoring all the data types and variables. So, let's say, first of all, my method is void start. How will car start? Here I am not doing anything. Let's do system.out.println. A generic implementation. Car started. Okay. Similarly, let's make void accelerate. And it will do this. Let's say again, system.out.println is a generic implementation. Because obviously car is an idea. Car is accelerating at 10 km per hour. Right? Something like this. So, similarly, you can also make break. So, let's make it void break. Okay. And let's say car is stopping. Something like this. And I had told you, let's say I make two child classes. I make a car, a fuel car. Okay, which extends this car. Okay, something like this. And I make another class, our electric car. Electric car, which extends our car. Something like this. Now look, since it's a normal class, a concrete class, all the methods are defined in it, so we don't get any error. But both the fuel car and electric car, both get these three functions. But as I told you, since the fuel car accelerates and brakes in a different way, and the electric car in a different way, what I do is, I remove the implementation of these two from here and just declare it like this. I'll just declare it. As soon as I did it, an error came. This method requires a body instead of a semicolon. Why? Because according to this, this method is not abstract. So, I'll have to write abstract first. Here as well. And here as well. Now, as soon as I wrote abstract here, one more error came up. Now, let's see if the error has changed again. Yes. The abstract method accelerate in type car can only be defined by an abstract class. Means here we have to make it abstract. See here it is also saying that the type car must be an abstract class. So now we also make it abstract. Error went away. As soon as error went from here, error came here. So we tell you everything about Java's compile time error. Now here it says that I, the fuel car, must implement the inherited method. Now because I told you that it will be its responsibility, then the fuel car and the electric car will have to implement these two methods. So something like this. We will write void accelerate and here we will give a body and similarly we will write void break and give some body. Similarly, we have to do the same here. Something like this. See, as we did, all the errors are gone. Right? Although we have given an empty body. But according to Java, we have implemented them. In fact, what we do in Java, we add an annotation. At the rate override. What are annotations? We will discuss this in a different class. But if you don't add this override, then it will work. But this is just telling the compiler of Java that we have implemented accelerate and break separately. So, I will add this here also. This is a good practice. This is not mandatory, you can ignore it too. But it is a good practice. Override means that we are not using the accelerator and brake of the car in the upper class. We have made our own accelerator and brake. Okay. Now I will fill some random values in it. Now I have filled random things here, just printed it. Accelerate, fuel car is accelerating, fuel car is stopping at the brake. Here electric car is accelerating and electric car is stopping. Obviously, it will depend on the logic of how we are implementing it. But to tell you the best difference, I have left it simply. By printing one by one. And here, accelerate is our break, our abstract. Now, let's go to the main method. And let's see what we can do. We can make an object of this car. Let's say small car. This car is in small. Equal to new. Now, obviously, I can't write new car. Error will come. What will the error do? Cannot instantiate the type car. Because car is an abstract class. I can't make it in object. So, here I have to write electric car. Or we have to write our fuel car. For now, let's write electric car. Now, if I call car.accelerate or car.brake. Let's do one thing, let's do one thing at a time. First, I'll call car.start. Start. First, we started the car, then we accelerated the car. And finally, let's apply brake in the car. Now, I could have made another method of car stop, but for now, let it be like this. So, I just did this, and if I run this code, then what will happen? My output will come, car started, electric car is accelerating, electric car is stopping. But if I replace electric car with fuel car, Now my method is completely changed. Now car started. Fuel car is accelerating. Fuel car is stopping. In both cases, car started was common. It was called from here. From our parent class. But accelerate and brake was called from that respective class. Which we had made an object here. If we had made it from fuel car, then fuel car's methods were called. If we had made it from electric car, then electric car's methods were called. Okay, I hope you have understood what abstraction is and what abstract classes are. If you have understood abstract classes, then there is one more thing in Java to implement high-level abstraction, which we call interfaces. Interfaces. This is a very interesting concept and a lot of freshers who are reading Java are very confused about interfaces. So we will understand it in depth. First of all, when I make an interface, you will find its syntax very similar to abstract class. Not just class, it will look similar to abstract class. But you have to remember that when I say class, then what is class? It is a blueprint of an object. You have said this, blueprint of an object. But when I say interface, then remember that interface is different from object. kuch lena dena nahi hai, interface ka object se koi lena dena nahi hai, interface koi blueprint nahi hai, kisi object ka, wo object ko na to represent karta hai, na koi bhi uska object se kuch lena dena hai, interface bahut hi ek alag cheez kaita hai, abhi hum discuss karenge kya, interface apne aapko ek contract bolta hai, thik hai, isko ek bar dekhenge, pehle example ke siru, pehle samajhta hum interface banate kaise hai, thik hai, toh bilkul same tarika, humara is bar keyword badal jayega, humara keyword ho jayega interface, thik hai, So, if we take the car example and make an interface of it, then how do we make it? We make an interface car, something like this. Here, first we wrote abstract class, now we wrote interface. And here, we don't define methods, we just declare them. So, let's say, one method is void start. Let's say, I just declared it. One method was void start. Accelerate was also declared and not defined. And one method was void break, which was also declared and not defined. Something like this. Now the syntax is different in such a way that I don't need to write an abstract keyword here. Because we have written the interface, so Java already knows this from the inside. The meaning of interface is that all its methods are abstract by default. Now we can inherit the interface too. So like this is our Cars were made, fuel car and electric car. Now we can make the same cars again. This time we will write class. Fuel car. Like we made earlier. When we inherit any class, we write the keyword extends. But when we inherit any interface, then the place of extends will come. Implements. This is another keyword that changes in Java. Implements. And here the interface name is car. The last time we wrote car as an abstract class and this time it is an interface so we wrote implements here. You implement the interface and extend the class. Now we will make a class here. Let's say electric car and it will do implements. Our car will be implemented in this way, right? Now both of them are going to be the same, that we will have to define the start here, we will have to define the accelerate here, and we will have to define the brake here. I am writing it completely, but you understood. And it will be the same here. Accelerate, start and brake, we will have to define all three of them here. But now what is the difference? Obviously, both of them look exactly the same, right? So you can make it in the same way as we did in our abstract class. We will write, let's say, I will first separate this here. This is our class and this is our class. If we want to make an object, then we will write carC equal to new. Now what will come here? Here our electric car can come or here our fuel car can come. So whatever will come here, for that when we will call c.start, it will be a start call. If we will call c.accelerate, it will be an accelerate call. And if we will call c.break, it will be a break call. Okay, it's exactly the same. We just changed the keyword, did the interface, removed the abstract method, and wrote implements instead of extend. What's the difference? You must have understood one difference. In abstract class, we at least declared and defined the start. What did we do? We defined the start. Here we didn't define any method, we just declared everything and left it. So, Java says that you have to declare all the methods in the interface, not just define one. The responsibility of defining will be of the child class. This is what old Java says. In new Java, you can give default methods in interfaces. But we will talk about this later in our videos. Right now, we are going to talk about how old Java is implemented. Right? Even now, if you don't define any method in the interface, it will be absolutely correct. Right? In fact, this is mostly done. In fact, in interfaces, we can't define any method. We declare all of them and leave them. Okay. So, but why did we make this? Why do we need interface? When the same functionality, everything is going on the same way. So, why is there an interface? Right? In fact, there are no interfaces in C + +. In C + +, everything is done through abstract classes. Right? We don't need an interface. But Java still made an interface. Why did Java make an interface? There are 2-3 reasons for making an interface of Java. Let's go over them. First of all, when we made the interface, Java said that it will tell us the pure what. Pure what means that we will not define any method in the interface. We will declare all the methods. This means that the interface tells us what anything can do. For example, a car can start, accelerate, or brake. But it won't tell us how it can do it. It will leave it. For whom will it leave it? The one who will implement that thing, like electric car or fuel car, will decide how to start, accelerate, or brake. Interface won't tell you. So, interface tells you about pure what. which we call as Pure Abstraction. Pure Abstraction. Now, obviously, it has changed a bit. You can go to the interface and give default methods. It means that you can define the start here just like the abstract class. So, there was a difference between them. The interface tells you pure what, but your abstract classes tell you partial what. Some things they also define. So, some things they also tell you how. So if I compare it line by line and write my abstract class here, abstract class, then it tells a little bit of how and what, but the interface today also tells how and what. But now we will talk about the old Java, so it used to tell pure what, it used to tell partial what and how. I hope you understood. He used to implement some things, he didn't implement some things. Okay, very good. So, there was a difference in the interface. What else is there to bring the interface? See, the biggest difference that the interface brings in Java and separates Java from all the other languages, C, C + +, where there are no interfaces, is the intention. There is a big difference in intention. What is intention? Intention tells us that Ki abstract classes kya karne ke liye intended hai? Aur interface kya karne ke liye intended hai? Ab mai bolta hu, ye jo maare interfaces hai na, inka main kaam If you want to take a loan from the bank, then there is a contract signed between you and the bank. It has all the terms and conditions written in it. If you can't repay the loan, then what will happen? What will be the interest rate? How many months will you have to pay? The interface also tells you like a contract. Let's say I made an interface of a car. So if you want to implement this car's interface, then you have to give definitions of all the methods. Now, like electric car and fuel car, both of them implemented the car. So it was the responsibility of both of them to implement the start, accelerate and brake. So, it was a contract between these two. I hope you understood contract. It works like a contract. So, it tells compulsorily that whoever will implement it, he will have to define these methods. Now, intention explains you a few more things. We will see that. After this, all your problems will be solved. That you should use abstract class or interface. Look, the interfaces that I have told you, you don't have to take them from the object, they tell you what the responsibilities of any class can be. Responsibilities or you can call it capabilities. Or you can call them roles. These are all the same. Okay. So interfaces just tell you what responsibilities, capabilities or roles of any class should be. Okay. What does it mean? Look. It means, as I just told you above, that if someone wants to call himself a car. What did I say? Car is an interface. Which I had made above. If someone wants to call himself a car. Car has nothing in itself. Car is a generic idea. Okay. Like electric car. If someone wants to call himself a car. Right? Take another example, Fuel car. This also wants to call itself a car. If these two want to call themselves a car, then the interface says that I am telling you some methods. You will have to define those methods. There are rules. So if someone wants to call himself a car, he will have to implement all that. What is this? It is telling a role. What are the roles of this car? So electric car will also have to follow those roles. Fuel car will also have to follow those roles. So if every car can start, Accelerate can be done. Brake can be applied on it. So, this should be true for every car. Whether it is an electric car or a fuel car. Now, this electric car also has to implement start, accelerate, brake. And fuel car also has to do it. Okay. So, it tells them its responsibilities. That's why you will see. Now, we will discuss about this in a proper lecture. Interfaces deep dive. In which we will see how many are the interfaces of Java. How will you see the name of all of them? You will see the name of all of them like this. Let's say one is comparable interface. One is serializable. So behind everyone, there will be a word like able. What can it do? Now, here we have given an interface car name. This is not a good name to give an interface. If we give an interface a name, which should be a good name, then we will have to put a name like this behind it. Like flyable. Walkable. Or you can say, let's say runable. Something. Okay? Whatever it can do. So, what will flyable say? If there is an interface called flyable, then it will say, if anything has to fly, then it will have to implement this flyable interface and it will tell the rules of flying. If anything has to walk, then it will tell the rules. If anything has to run, then it will tell the rules. If you see the example of flyable, let's say I made an interface flyable. This is an interface. I will write this. Interface. And let's say, there are two things in my application that can fly. One is my birds, which is also an object. And one is my, let's say, aeroplane. This is also an object. Both of them can fly, obviously. So let's say, what did I do? I made a bird class and an aeroplane class. This is a class and this is also a class. And what will both of them do? They will inherit our flyable and implement it in a way. They will write, what will be the syntax? Aeroplane implements flyable, birds implements flyable. And Flyable has already written some rules. For example, if you want to fly, you have to define the fly method. Because both fly in different ways. Bird's fly method will be different. The fly method of an aeroplane will be different. Because birds will flap their wings and fly, so their fly will look different. Aeroplanes will use aerodynamics and engines to fly, so their fly will look different. So the fly will look different from the inside. But both of them will have to implement this fly method. This is what a flyable interface is. This is the rule. It has defined that you have to do this. So I hope you have understood that what interface tells you? It tells you your responsibilities, capabilities or roles. That what you should do. So here if car is a little wrong name. Although you can write, in fact you write like this in production too. But generally we prefer a verb form. That what can it do? Flyable, walkable, runnable, talkable. That what can it do, right? In fact, if you see in C sharp language. So, a rule was made that if you want to start a name of an interface, then you have to start with I. So, if you want to make an interface of car, then you will make I car. And this is also followed in Java many times. Some companies follow it, some companies don't follow it. This is again a nomenclature. There is no rule of Java. But what you do with this is that you can make a gap between interface and class. Okay. Okay, if all this is interface, then where should we use abstract classes? Abstract classes. Brother, you said that interface tells your roles and responsibilities. So, what do abstract classes tell you? Look, abstract classes tell you families of similar objects. Since it is not an interface, it has to be connected to an object. So, I can group similar objects. For example, when I made an abstract class of a car, the car was of that type because an electric car is also a car and a fuel car is also a car. And these two cars belong to a similar family. So, I made a common car and I said that we three together represent a family of which I am the parent. And these two are my child. Okay. And what did the parent do? Some methods were implemented by itself. As we saw in the starting, it defined the start by itself. and accelerate and leave it on break-in that you define it and I will declare it and leave it. So abstract class will tell you families of similar object and interface will tell you roles or responsibilities. Now how to decide? You have to decide. If in your application, if you write any code and you think that my code is representing mostly families, then you will make abstract class. Otherwise if you think that your parent class like here was car, is working like a contract. So, you will make an interface in that case. So, there are some design decisions which are subjective. But there is one more big difference which separates interface from abstract class and we will learn about it in depth when we will study interfaces properly. That is, let me just tell you, multiple inheritance. Multiple inheritance. Do you remember in the inheritance lecture we said that multiple inheritance is not supported in Java? No, but it is not supported through the class. is supported through interface. So if you want multiple inheritance features, then you will use interface. And why is it supported, how is it, we will see that again in the interface lecture. But for now, that's it. Now let's quickly look at it in code. You see here I have again made my demo 2.java empty file. Now what I will do here, I will define my interface. So I will write my interface and our car. Something like this. Spelling is wrong here. Car. Okay. Now what I had said. Same thing can happen in interface too. So as I had written here. I will pick up the same method. Our start. Accelerate. And break. I have picked up the same method. Obviously. Now what I have to do. First of all. I have to remove this abstract keyword. From here. This. Break. I have to remove this. And here is an error, it says abstract method do not specify your body Now your body should not come here because according to it everything is abstract Start, accelerate, brake Okay, I have defined three methods here Now what can I do? I can make a simple car of my own So I pick it up from here Fuel car and my normal car Something like this So I copied the fuel car and electric car from there First of all, I have given the wrong name of the car here It must be a capital car Now I have to write implements here and implements here as well because you know you implement an interface and as I wrote an error came up that I must implement the inherited method because you haven't implemented start yet so you will write void start something like this and here you will implement something so fuel car has started let's say Okay, so here I have again implemented start, fuel car has started. Now you will see there is an error in all three methods. And what is that error? Cannot reduce the visibility of the inherited method. What does this error mean? This error means we will understand when we will understand interfaces in deep. Now you just assume I have to put public in front of them and that error will go away. Okay, so public, public, public. And public. Okay. Again. We will understand. If you want to understand in short. Then I can tell you. That interface. All its methods. By default. Are public. So you. Fuel car. Which is its inherited class. You can't change it from public. You can't narrow down its visibility. You can definitely extend it. Okay. So it will be necessary to put public here. So here also. I put public. I will write overwrite at the rate of overwrite. Now this code will work. But since I want to implement start, I will copy this start. And paste it here. And write here, Electric car has started. And public public. And here it will be public. So all my errors are gone. Rest of the things will remain the same. Again I can make the object of car. Let's say small car equal to new. Now I can't write car here. Because this is an interface. Error will come. Cannot instantiate the type car. So you can write either electric car or fuel car. Again if we do all that. We can write car.start. We can write car.start. Accelerate And we can write car.break Something like this Now when we run this Then our electric car functions will run See this And if we did fuel car here Then you know that our fuel car methods will run So let's see that too See, right? Let's go Yes, so if you understood this well Interfaces and all Let's talk about a topic in which many kids get confused That in abstraction And what is the difference between encapsulation and encapsulation? Now, if you have watched this video properly, then I don't think you will have this doubt. But sometimes we ask in interviews and try to confuse, so let's clear it today. It's a simple thing. First, let's understand what encapsulation tells you. We had seen it in the last class. Encapsulation tells you two things. First, that your data and your methods should be encapsulated together. So for that, we make a class to keep them together. The second and most important thing is data security. We had discussed that some data is sensitive and it should be secure. You don't want to give access to that data to everyone. So you provide data security using access modifiers. Through access modifiers. And what do you do that? In encapsulation. So encapsulation also talks about data security. But what abstraction on the other hand says is that the world is very complex and it is very difficult to understand. So we hide the data or hide its implementation. So what it means to say is that it talks about data or the hiding of its implementation. Implementation hiding. So it's not providing you any security, it's just hiding that data. For example, car. You know how to drive a car, it doesn't mean that you know its internal. So all those implementation details are hidden from you. If you want, you can see it. If you open the bonnet, you will see the engine. You can understand the functionality of the engine by going to YouTube. You can study. But this doesn't mean that it provided you with security. It just hid the data for you. In fact, what will happen if you do all this? You won't learn anything. But even if you don't learn all this, there is no problem. Right? So abstraction talks about data hiding. Encapsulation talks about data security. These are the major differences between these two. Abstraction never talks about security, encapsulation only talks about hiding. Okay, I hope you understood this. If you understood this, then we move towards our last topic, which is going to be very interesting. In fact, it will be so simple for you that if you understood abstraction, inheritance and encapsulation, So this is a piece of cake for you. I'm literally telling you. You just have to understand its concept because as soon as Java implements it, you have already read it. So let's understand polymorphism in such a way that what is the meaning of polymorphism? So first of all, it is made up of two words, poly and morphism. Poly means many. And morphism means forms. And if we read it together, it means many forms. What does it say? What does it say about real life objects? It says that our real life objects, they have many forms. Let's understand this carefully. Okay. So, polymorphism will talk about two things. So, let me say one thing first. I will explain it in real life, then how it is represented programmatically, and then second thing, how it is represented in real life and then programmatically. First of all, what polymorphism says, first we will understand it and then we will define it. Let's say there is a human, now it can be a man, it can be a woman, it can be anyone. Okay, I am saying, I will give it a command, run. A stimulus do run. Okay. So he will run in a particular way. So let's say he will run at 2 km per hour speed. For example. But let's say I changed the situation a little bit. I said there is a dog behind him. Dog. Okay. He is behind him. Now he will be scared and he will run faster. So now if I call him run. So what will he do? He can increase his running speed by 5 km per hour. So, if I try to define it, what happened is that there was only one object, human. I gave it one command or stimulus, which I called run. But it reacted in different ways. Why? Because one condition changed that a dog ran after it. So, polymorphism says that same object behaves or responds differently. Same object behave or response differently on same command. Here command or stimulus was also same that it has to run. If there is any difference in parameter then we can also write it. If one parameter is changed. Okay. So I hope you are making sense of what I am trying to say. That there is only one object that reacts to the same stimulus in different ways. If we separate a parameter. This happens in real life, you know it happens. Java thought how can I represent it programmatically. It is very simple. We have already done this. We have method overloading technique. Overloading technique. That too does the same thing. Now see, why did I make run with parentheses? Because if I made a class of it, then there would be a method named run in it. Right? You know this. So if I represent this human here, then what will I do? I will make a class of it. So let's say I will write class human. Something like this. And here I will make a method, let's say void run. Now on this method, let's say I don't take any argument, so here parentheses are empty. And let's say he will implement this, on which he runs from 2 km per hour. So 2 km per hour, right? Rest I can make another method, void run. Name is same, see. What I did here too, I said on the same command, once ran from 2, once ran from 5. So here I made another run again. The property was getting separated. Let's say I took a boolean property, isDogBehind. So I wrote here, if this property is true, let's assume it is in some condition, then I said, implement 5 km per hour. So we were doing this earlier too. See, same command, run, run. Okay? It's the same object, human. But one property has changed. Is dog behind or not? If it's a property, then this function is implemented. And if you don't give this property, then this function is implemented. That means it can run 2 km per hour. It can run 5 km per hour. We have already studied this. We were talking about method overloading. So, it's exactly the same. The first part of polymorphism, we express it programmatically through method overloading. This is the first type of polymorphism. Another name for it is compile time polymorphism. What do we call it? Compile time polymorphism. Polymorphism. Why do we call it compile time? See, it's a simple thing. Which method will call here? This method will call or this method will call? You will know this only at compile time. Now see how. When you call the main function, what will it do? If you call run like this, then this method will call. Because you have left parenthesis empty. But if you have given a boolean variable here, let's say true. or false, whatever boolean value you have given here, then this method will be called. So, Java will get to know what code you have written here at compile time. According to that, I will either call this or that. That's why we call it compile time polymorphism. And how to implement it? Using method overloading. Okay, very good. Now let's move on to the second thing that polymorphism says. Again, we will first understand it from the example and then we will understand its definition. Example says that this time let's say I take three different types of objects. One I take, let's say I take dog. One I take, let's say I take duck. And one again I take human. All three are animals. Obviously, right? Human is also an animal. So I say, let's say, I give all three the same command. I call all three run. I call this also run. And I will also call this run. I will give the same command to all three. And this time the situation is also the same. That means no property is getting separated. I have given the run command to all three in the same situation. So see, your dog will run in a different way. Duck will run in a different way and human will run in a different way. All three will react in different ways on the same stimulus, in the same command, in the same situation. Although all three of them are animals. So let's say if I make their parent object animal, then I will say this is also an animal, this is also an animal and this is also an animal. So this is also a type of polymorphism, second type of polymorphism. And Java says that yes, this happens in real life. How will we represent it programmatically? We have also read that programmatically we represent it using method overriding. What do you do? What we just read in abstract classes. We overwrite the method. So how will you implement it? Let's say you will say that you will have a class. Let's say I make an abstract class animal. Which we just read. Animal. And what will happen in that? There will be an abstract method. Sorry. Abstract void run. And I will just declare it. Because animal is a generic thing. It will run by itself. Now what will I do? I will make different classes. I will make one class dog extends animal. Now it will implement its own run method. Similarly, I will make the class DuckExtendsAnimals and the class HumanExtendsAnimals. And let's say if our class is Duck, then we have Duck and if we have Human, then we have Human. Everyone has their own run method. I have made my own run methods and I will decide which run method will be called at the runtime. Now think carefully, there are different objects, one is your dog, here let's say I have represented a duck, so I will write here, here let's say a duck came, here let's say a human came, I did not make their classes but you understood that here I represented a duck and here I represented a human. There are different objects, the method is the same, run. Run. Run. Method is same. But all three will implement in different ways. Dog will run in different ways. Duck will run in different ways. And human will implement run in different ways. Right? So what did I do? By combining, I declared run in their common class animal. And left the other three to define. Now why did I call it runtime? I will make its object like this. I will make it Animal, let's say a equal to new. Now I can write anything in front of new. Let's say I can write duck, human or dog. So let's say I wrote dog here. So now if I call a.run, then whose run method will be called? This dog's run method will be called. If I had written human instead of dog, And now when I call a.run, then the human's run method would be called. And if you know that this is a duck, then the duck's run method would be called. We call this runtime polymorphism. Runtime. Polymorphism. Run time polymorphism. Why do we call it run time? Because it will be decided on run time. On run time. Which run method will be called? This one? Duck's? Or human's? Why will it be decided on run time? Because I told you that these objects are created on run time. Whatever you write in front of new is located on memory on run time. Now you think that suppose my code is written like this that I will first declare the object of animal as animal a. Now what will be equal to new? It will depend on some condition. So I will write here let's say if type equal equal duck. So I will create duck animal. I will write a equal to new duck. Right. Similarly, else if type is equal to human, then a is equal to new human. Right? So, basically, in a dynamic condition, what value is coming in type? Duck is coming, or what is coming? Human is coming. On that, I can decide whether I want to create an object of duck or human. And similarly, I can do else or create an object of dog. New dog. Right? The first thing you have to remember is that objects are made dynamically You have written the condition here that if I pass duck in type then make this object If I pass human then make this object So when will you pass this? You will do this dynamically Let's say when your application is running then someone will give an input That duck is type so a different object will be made If someone will give an input something else then it will be a human object So how did this happen? It was decided on runtime That whose object will be created in memory. Duck. Sorry. Duck's. Dog's. Or human's. And whose object will be created. When you call dot run method on it. Its respective run method will be called. Okay. So you understood. Why we call this dynamic polymorphism. So if I tell you broadly. That polymorphism. If we draw a table, then it will look something like this, right? There are two parts of polymorphism, it can be compile time or run time. In compile time, we will call it method overloading and in run time, we will call it method overriding. Absolutely right. So, till now, the code we were writing was a complete polymorphic code. So, if you write a code in which you make multiple methods with the same name, just separate its parameters, basically overload the method, then that is also a polymorphism. Or if you write something like this, which we were writing till now, that we make a class, appset class animal, like we made car, electric car, fuel car, same here we made dog, duck, and human, all of them have run run run method, now which run call will happen depends on what comes after new, animal equal to new what, so we decide this at run time, so this is its complete table, we have already coded this so many times, right, so polymorphism was nothing new, now I want to tell you about some keywords that we read earlier, one was our static keyword, One was our final keyword and the other was our private keywords. When you overwrite the methods, i.e. when you show dynamic polymorphism, then they react in a different way. We have to understand this first. First of all, let's talk about static. Look, I am saying that all your static methods in any of your classes are not overriding. They don't get override. Why? By first thought principle, understand it. That our class is, let's say, A. Okay. In that, there is a static method. Let's say, static void fun. There is a method. Okay. Now, again, I made a class B. And I extended it through A. Okay. So, now, if I write the same method here. Let's say, static void fun. Okay. If I write here, I will make an object of a, small a, means I will make a reference, equal to new. Now what can I write on this new? Let's say I wrote new b, right? You know this is a parent class reference, it can point to a child class object. So now if I write a.fun, then my expectation was that the function of b will be called, right? If we go by the method overriding rule, then a.fun means the function of b should be called because whose object is being made here? B's. But actually whose will be made? A function will be called Because A is a static Because our fun is a static method Okay Let's see it in code quickly And let's finish it by looking at the rest of the parts in code So see here again I have made a demo and made an empty project So what I was telling you First of all, let's talk about static keyword Static keyword So let's say what I did first I made a class A Okay, let's keep a simple name And let's say there is a static method in it Static void Sorry Static void And I give the name of this fun Let's just print hello on the screen So system.out.println I said hello to it Now I will make another class classB which extends our A class And it also has static void fun method which means it is overwriting the above method And again I overwrite hello and write here Bye. Something. our main function, if I make an object like this, make a reference of a equal to new, but make an object of b. Okay? So, our common sense tells us that if I call a.fun call, then call should be by. But look, call will be your hello. Because on static methods, overriding doesn't apply. But let's say if I don't write static here, then what would happen? You can expect it. Then our normal call would run and our hello would be implemented. Sorry, bye would be implemented. Let's run it once and see. Bye, right? So, our static methods are not override. So, let me write static here first. So that we get the correct code. Static word fun. Now, use common sense that why static methods are not override. It's a simple reason. To whom static methods belong? They belong to class. They belong to class. And not to objects. So they don't belong to any object. So if they belong to a class and not to an object. It means that they don't belong to an object. So when we write a equal to new. So their binding is not because of this. That they are referring to this object. But because of this. That they are the reference of this. So what is A here? A is the reference of our big A, capital A class. So if small a is the reference of capital A, then when you call a.fun call, then it will always be a fun call of capital A because it is its static method, it is bind with it. Right? Your class is A and this small a is bind to whom? To the class. So it will always call the method of the same class and not the object it is referring to. Okay? Let's go. The next thing I wanted to tell you is that let's say I have written a private function there. Let's say private void fun2 something. Okay? It is a private function. And let's say it also prints your hello for example. Okay? Something like this. So if you have marked it as private, then you can't override it here. If you make a method here, private fun2, then it will be a different method. This method and this method are completely different. This method is not being overrided here. So we can say that your private methods can't be overridden. They can't be overridden. Why can't they be overridden? Because you know what the private method means. It can't be accessed outside the class. It can't be accessed in the child class. So if the child class can't access this method, then how can it be overridden? Right? So private methods cannot be overridden. So you have to remember this. Okay? Third, we remember we read about the final keyword. We said that if we put a final in front of a variable, then it becomes a constant. You can't change it. But I said, final is not just a variable. You can put it in front of functions and classes as well. Today we will see, if I put a final in front of a function, then what will happen? Let's say I write final void fun3. Fun3. I write something like this. What else do I do? Let's say I print hello again. Something like this. See, I can write this. But if I try to override this, let's say I write void fun3. So, See, as soon as I overwrite, an error appears. So let's say I was going to write by here. If I write by here, what does error say? Cannot overwrite the final method from A. Final method. If you add final to method, then that method cannot be overwritten. Final methods cannot be overridden. In fact, we put final in front of those methods which we want no one to overwrite. There may be a use case where we want to use this method for the parent class. I am saying fun3 for the parent class. The child can't make his own fun3. So, the one who extends a will have to use fun3 for the parent. So, you put final in front of that method. Now, there is another critical concept which many people miss. The fields or variables, sorry. or also called as variables, they cannot be polymorphic. What does it mean? It means that our fields and variables are not overrides. They don't act like polymorphism. How? So, what do I do for that? First, I comment these two classes for now. Let's assume that I make a new class here. Again, class A. And I make another class. class B and I will extend it from A. Now suppose in this class there is an A variable int x equal to 10. Now if you are thinking that I will do int x equal to 20 in B as well then it has overrided this x so it is not like that. Now if I go and make an object of A, A equal to new B and appoint it as B and I will comment B dot, now I will comment it because it does not exist. And if I Let's say I try to print system.out.println a.x So what will be printed according to you? Look, 10 will be printed which is of a or 20 will be printed which is of b. This small a is the reference type of big a. But it is pointing to the object of b. Let's see what is printed first. 10 is printed. Which variable is printed? Of a. which means that our variables also don't participate in polymorphism. We will just see which is our reference object, A or B. If reference object is big A, then A.x will always print the value of A. If you want to print this class B.x, then you have to write something like this. Let's say B equal to new B. You would have to write something like this. Now if you would have printed b.x, it would have been 20. Okay? So I hope you would have understood that the fields or variables that you have, they don't participate in polymorphism. Because if they would have participated, then a.x should have been printed as 20. Right? Because we expect from methods that The reference a, how is it pointing to the object of b? So it should call the things of b, right? So it will call the methods of b. But it will call the variables of a only. Why? Because it is a reference of a. Okay. Now if you want to do this a.x, then if you get the value of b, then you will have to convert it to methods. So let's say instead of int x, I would write something like this. void get x. And what would this do to you? Let's say return 10. Or not void, here int comes. int getX returns you 10. It's exactly the same. Here also, what would I do? I would make a method named int getX. And this time, I would return 20. So, now it will override here. Now, this is class A, this is class B. It is extending A. So, what will this getX do? It will override this getX. So, now if you do something like this. And instead of A.X, you will call, let's say, A dot getX. Kuch aise. Now, your 20 will be printed. That means, the value of b will be printed. Look at this. Right? So, you know, these methods participate in overriding. But your variables don't participate in overriding. Okay? I hope you understood this. So, we have written all these points here. That static belongs to class. So, it doesn't override. Private methods are hidden. So, they don't override. And final keyword is made because if you put it in front of method, then it doesn't override. Okay, tell me one more thing. If I put this final keyword If I put it in front of a class, then what will happen? We haven't studied this yet. So think simple. If this final keyword is put in front of a class, then what will happen? Then what will happen is that you cannot create child of that class. So you won't be able to inherit that class. This means if I show it like this, So, if I put here final class A, then the error will come immediately and it will say that the type B cannot subclass the final class A. Because if your class is final, then you cannot inherit it. Now, why will you do this? Again, it depends on the use case. Sometimes, there can be such a use case that you want that no one can inherit my class. In that case, this logic of inheritance works very well. So, I hope you understood everything crystal clear. We understood abstraction and polymorphism. People find polymorphism very hard. But if you do abstraction, encapsulation and inheritance properly, then polymorphism is nothing. Because polymorphism is all about method overloading and method overriding. And we have already studied that. So I hope you enjoyed it. If you enjoyed it, then please like the video, hype it, comment it. And if you have any doubt, then obviously come to the comment section and ask. Okay. I'll meet you in the next video. Till then, thank you so much.
