Podcast Lookup

Java Generics Wildcards Explained | Unbounded, Extends & Super Wildcards

The Java Developers Hub

  • December 3, 2026
  • 1 h 22 min
  • Episode 28
Episode page
About this episodeJava Generics provide type safety and flexibility when working with classes, interfaces, and methods that operate on different data types…
Java Generics provide type safety and flexibility when working with classes, interfaces, and methods that operate on different data types. However, understanding wildcards is essential when building reusable methods and working with generic types that have inheritance relationships.In this episode, we take a deep dive into Java Generics Wildcards, exploring unbounded wildcards, upper-bounded wildcards using extends, and lower-bounded wildcards using super.You'll learn how wildcards make generic methods more flexible, how Java handles type compatibility, and when to use each wildcard in practical programming scenarios.Whether you're learning advanced Java or preparing for technical interviews, this tutorial will help you understand one of the most important concepts in Java Generics.What You'll Learn
  • Understanding Java Generics and why they are important.

  • What wildcards are in Java.

  • Understanding unbounded wildcards using ?.

  • Working with upper-bounded wildcards using ? extends.

  • Understanding lower-bounded wildcards using ? super.

  • Differences between extends and super wildcards.

  • Understanding inheritance relationships in generic types.

  • How wildcards improve method flexibility.

  • Understanding the PECS principle: Producer Extends, Consumer Super.

  • Working with generic collections and wildcard parameters.

  • Practical examples of wildcard usage.

  • Common mistakes when using Java Generics.

  • Best practices for writing flexible and type-safe generic code.

Who Should Watch?
  • Java developers learning advanced Generics.

  • Computer science and software engineering students.

  • Backend developers working with reusable Java components.

  • Programmers preparing for Java technical interviews.

  • Developers interested in type safety and generic programming.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Generics Wildcards Explained | Unbounded, Extends & Super Wildcards

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today again we are going to read about Java's generics in more depth. In fact, we will read about wildcards, which is a soul of Java generics. You will have a lot of fun, you will get to learn a lot of new things. We will understand such good generics that you will never have any doubt. Those people who have always had doubts in generics. So let's go to the screen and start. So till now we were talking about generics that we have to make a general purpose class. A class that can accept any data type and we started with our object class and we thought what if we keep object as a general purpose class but it had many disadvantages and then we shifted to generics. And how do we make a class in generics? Let's say if we want to make a general purpose class, we take a datatype of type T. Now what is T? Again I said it is a placeholder, we can keep any name of it. Okay, so it takes a datatype of type T. Let's say we open our class. And we can store any value of type T in it. We talked in this way. And all our work was going well. Now comes a very big question on generics. That is, our generics break the parent-child relationship. Break the parent-child relationship. What does it mean? Till now, we saw in inheritance that in Java, there is a parent-child relation. For example, the object class we have is parent of every class. Now, it extends it. Let it be any class, let's say string, integer class, or any class defined by you, let's say student. Every object is inherited. So, when I say that generics break parent-child relationship, what does it mean? Okay, let's understand this well. Okay? Aap imagine karo ki mere paas hai ek animal class. Animal class. Theek hai? And I say that I make a new child class and I name it dog because I know dog is an animal. So I have a dog child class and animal is my parent class. So there is a parent-child relationship between them. And that's why I can write in the code that let's say I defined an animal. A reference variable and here I can write new dog. This is possible because we have inheritance and polymorphism in Java. We know what it means. It means that the heap memory in Java will have a dog type object. And the stack memory in Java will have the reference variable of the dog. This is not the reference variable of the dog, it is the reference variable of the animal. But the reference variable of the animal will point to the object of the dog. And we also discussed which methods we can call on this reference variable a. All those methods which exist in animal. So let's say I have one method in animal which is eat and another method is walk. I have defined it in animal like this. So on this a reference variable either I can call a.eat or a.walk. Suppose I have a method in dog which is called bark. But can I call that bark from a? Can I do that? The answer is no, not at all. Because in a reference variable, we can only call the same method which will be declared in animal and dog. If we want to call that, we have to make a dog specific object. We have to make something like dogD equal to newDog. We have to do something like this. In that case, our D reference variable will point to this doc. Now on this D, I can call eat, I can call walk, and I can also call my bark. Right? We know all this story. Okay. Now let's move one step ahead and see our question that how parent-child relationship breaks due to generics. So I'll remove this, we already know this. Now to show you that, I am going to use Java's List Framework. Now we will study List in Collection Frameworks separately. But let me tell you that in Java we have an interface which we call List. Now which list is this? You can consider it as an array in which you can put any element, remove that element, etc. So this is our interface in Java. And we have a concrete class in Java which implements this list. Which is called ArrayList. This is exactly the same as the normal array that we used to make. The only difference is that it uses generics in general. Now, what is the method to make it? As I said, our list is our parent class interface and array list is a child of it. We can say that we can implement it. And again, our list uses generics very well. So when we make an object of ArrayList, we can make it like this. I can write list of type, let's say integer. So I have already passed it in generics. It is an integer list. And I name it smallList equal to new. And here I can write ArrayList. Something like this. Now if you don't know what an ArrayList is, we will study it in depth in Collection Frameworks. Now you just understand this syntax that what I did here, I declared an ArrayList. So if we understand it again in the same way as what happened in our heap memory, then you can understand that in our heap memory, an object of ArrayList will be created and in our stack memory, a reference variable will be there whose name will be list. Something like this and it will point this object. Now in this object, you can understand that it is almost like an array. As soon as you do this, you call the list.add method. And because it is integer, you can add any number of integers. You write list.add, then you can do list.add, And in this way you can keep adding elements. So you can assume that you have added 2 or 3 to the array and so on. Now again, this is a dynamic list, you can add as many elements as you want. You don't have to worry that your list will be full. For now, you just need to understand that we can make a list of any type. Whether it is of integer, string, object or any class of your own like animal. Because we use generics. If you understood this, So let's go back to our main question. What hierarchy did we have? We had an animal class. And that animal class was extended by our dog class. So let's say I made our dog class here. So this hierarchy was obviously there. And I said that we can always write this. Animal A equal to new dog. Because our animal is a parent class. It can refer to our child class. But suppose I have a list of dogs present. So I have a list of dogs. I can write it like this. And I call it dogs. Dogs equal to new array list. Something like this. Now because I can write like this. That I can put a dog in an animal's reference variable. So can I put a list of dogs in an animal's list? If I write here. List of animal. And let's name it animals. And here I assign it to dogs. Okay. So it means that I have a reference variable of type list called animals. And who is it pointing to? It is pointing to this list of dogs. Can this be possible? So basically it means that we will have a stack memory. In that we will have a list of dogs. And in our heap memory we will have a reference variable called animals. Animals. Okay. And it will be pointing to this. And what is this animals? It is a list of animal type. Good to study, Casey. Can this happen in Java? The answer is no. This is not allowed. And this is very interesting. So basically, we can put a dog in an animal reference variable. So logically, this will come in our mind too. So the list of dogs can also be referred to as an animal list. But the answer is no. And this is the reason that the generics in Java break the parent-child relationship. How can we read this? We can read it like this. I can say that dog... Dog is a animal because we know that our inheritance is a relationship. I can say this. But I can't say that list of dogs is a list of animals. I can't say this. This is wrong. This is completely wrong. In fact, instead of is a, the correct word would be not any relation. In dono ke beech mein koi relationship hi nahi bachta. Okay? To Java ne kya kiya? List of dogs aur list of animals ke rishte ko poori ki poori tara toad diya. Jo pehle hamara is a relation aa raha tha, wo apuri ki poori tara break ho chuka hai. Okay? To isko sahi tarika hai likhne ka ki generic A is not subtype of generic B. So basically, if you are using a generic in any of them, if there was a parent-child relationship in both of them, then after generic, there is no parent-child relationship in both of them. And now we will first understand why this happens and then we will see what is its solution. Okay, so let's assume that Java has not made any such rule and it is allowed. So if generics don't break the parent-child relationship, then what would be allowed? Let me write my code first. Yes, so this was our code. Let's assume this is allowed in Java. If I write it now, then there will be a syntax error, but let's assume that I make a list of my dogs, okay, a new array list, and in the reference variable named animals, which is of type list animal, I could point that list of dogs. If this were to happen, then what would happen? So Java says that if this were to happen, then we would break type safety completely. Type safety will break. Okay, how? How does type safety break? We had understood what type safety is in the previous video. If you haven't seen it, you can click on the i button and watch it. I will attach it. Now, type safety would have been broken because if I could do this, if it was correct, then I would have put the list of dogs in the animals reference variable. Now imagine that if it was possible, then what I would have done is I could have read the list of dogs from the animals reference variable. So to read, I could have written like this that animals.get and let's say I wrote 0 here, then it means that bring me any animal on 0th index. Now I know if this is a list of dogs, so if I make this list and show it, suppose this is a list of dogs and for now, There are 5 elements, so 0, 1, 2, 3, 4. So there must be a dog on the first index, on this, on this, and on this. So if I do animal.get, then I will get a dog. No problem because dog is an animal. So there is no problem in reading. But, but there is an issue in writing. What does it mean? Here I can also do this. Animals.add I am adding a new animal. And here I add a new animal. Something like this. And I can do it. Because I have reference variable of animal. And list of animal is animal. So, I can do animal.add new animal. Now, there is a problem here. The problem is that, let's say, there were dogs in my let's say, till now. Here and here and here. There were dogs in all of them. But as soon as I added a new element, I added an animal in this dog's list. Okay. Now, this is wrong because, let's say, in my dog class, there was a special method, bark. Bark. which was only in the list of dogs. In fact, there were only two methods on animals. One was our eat and the other was our walk. But in this animal class, there was no method named bark. And why did it happen? Because every animal cannot bark, right? Only dogs can bark. So dogs had that specific method with them and animals didn't have that specific method. But look what I did here. I pointed the list of dogs from a reference variable named animal. Which is of type list of animals. So if I did this, then I was able to add this as well, so I added a new animal here. Now you think, if I wrote this code and it would have been allowed, now another developer is working on this code. So he will keep the expectation that there will be only dogs in this list of dogs. That is obvious, right? There will be only dogs in this list of dogs. But now what he should do, he should go through this list of dogs and try to call the. bark method of every dog. So we can write something like this, for every dog d in animals, And we will call it d.bark call. Now our code is wrong. So I am looping through this list. And I am asking every dog in this list to call its bark method. Now its bark will be called. This, this, this, this, this. Everything will be fine till here. But as soon as we come here, first of all, this dog name reference variable will not be able to point it. Because dog is our child class, it cannot point the animal. Right? And neither can it call its bark. Right? So this loop will be completely wrong and dynamically we will get errors. Right? So doing this is very wrong. Now, because doing this, our type safety is lost. We don't understand that the animal we have added here, we have added it in the dog list. But now we can't even point the dog name reference variable. and neither can we call any method on it. So this breaks our type safety, that's why Java doesn't allow it. Now we call this property as invariance. So basically we can write generics are invariant in Java. Invariant. What does invariant mean? Invariant means exactly the same thing that let's say even if A is child of B then still generic of A is not child of b child of sorry generic of b So this is the basic invariant that our parent-child relationship is broken. First, it is possible that B is the parent class and A is the child class. This is allowed. As we saw that animal was the parent class and dog was the child class. But this does not mean that the list of animals is also the parent of dog. This assumption is wrong. Now, I said that let's break the Generic Parent Child Relationship If we go back to our Java Arrays and don't use the list then you will get this Parent Child Relationship again What does it mean? You can think of it like this If we had an array of dogs instead of a list then let's say we have an array of dogs which I call dogs equal to let's say new dog and let's say I have an array of a dog of 10 sizes Now, what I would do is I would make an array of animals animals And if I had pointed it to dogs, then this code would have been correct. Here invariant property is not applied. Java would have allowed it. Why would it have allowed it? Because here we are not using generics. Right? But because it is allowed in Java, in the case of our array, the same problem arises, which we had just discussed, that if it would have been allowed in generics, then this problem would have come. Right? Which problem? Same problem, that we can still write animals, let's say this docs.carry is filled to 2 so in third index I have put new animal so I am able to write this in my java and as soon as I write this I will get run time error And you know why it will come, we discussed it earlier. If we look at it from the heap's perspective, we have arrays in our heap memory, right? So let's say there will be a list of dogs in our heap memory, and let's say D1 and D2 dogs will be filled in it, and the rest of the list will be empty, in which a total of 10 elements can come. And here will be our stack memory, and what I would have done, first a list named dogs will point it, like this sentence, and then I made a new reference variable animals and this will also point to the list of this doc now as soon as I wrote this line animal at third index equal to new animal so I am saying on this third index ok this is second index so let's make it second so I am saying 0, 1, 2 on this second index add a new animal type reference variable So here I am trying to add a new animal. Right? And this is the problem. The problem is because we know what happens in arrays. If we have a non-primitive type array, then if we have an array of dogs, then every index of this dog's array will be pointing at a dog. So basically what we mean to say is that we don't actually have D1 and D2 inside it. Right? Every index will basically point at a new object of the dog. So basically here we will point at a new dog. Here also we will point at a new dog. And in this case too, we would be expecting a new dog to be pointed. But what did I write here? Animal at index 2 equal to new animal. So what did I do? I directly said that this index points to an animal type object. But how can this be? Every block of this array is a dog type reference variable. And a dog type reference variable can never point to an animal. Can you write like this from inside? Dog D equal to new animal. You can't write like this. This can't be allowed in the code. Because you are saying that the reference variable of the child class is pointing to the object of the parent class. This is the opposite of polymorphism or inheritance. The reference variable of parent class can always point to the child, not the other way around. In this case, the same is happening. So in this array, this block, which is a reference variable of dog and it is expecting that it will point to an object named dog, but actually it is getting an animal type object. That's why our runtime error will come there. So obviously, even though it is allowed in the arrays, but as soon as we try to do it in the array, our runtime error comes. So let's quickly go to the code and see both these things. So again I have taken my demo.java as an empty file. So let's do what we discussed. First of all, let's make a class animal. And for now, let's leave it empty. And again, let's make a class dog. Which extends, let's say, animal. Something like this. Now let's do one thing, let's put two methods in it. Let's say void eat, I said it can be done in this. And let's put one void walk. And we define these two methods basically So we put two methods in the animal class, eat and walk and defined them Let's do the same thing, let's put another new method in the dog, let's say void bark And we simply print this as barking Something like this Okay, so this is our hierarchy Now let's go to our main part First we see invariant in generics Okay So first I'll show you a normal case that we can do this. If I write without generics, an object is animal, animal equal to new dog. So I can easily do this, there's no problem. Now in this animal name reference variable, I can either call eat or I can call walk. But can I call bark on an animal name object? The answer is no, I can't. because it doesn't exist, the method is undefined. So if I can't do this, but I can do eatwalk and run it, then I will get my output as well, eating and walking. So let's comment this for now and see if I can do this, let's say I have a list and what is the list of? So I have a list of dogs and I call this list as dogs equal to new array list. Now here error is coming because we have to import its list class. So we import this list. And we also import this array list. So these are their imports. List array list exists in the util package. We will see this well in our collection framework lecture. But for now you ignore it. But yes, just assume that we have a list of docs. Now what I am doing is making a list of animals. And I call this animals. So here I was saying that I can't write by doing such docs. I will get an error. Compile time. And what is this? Type mismatch. Cannot convert from list of dogs to list of animals. See, there is no relation. You can't convert a list of dogs to a list of animals, but you can do this. This is allowed. One dog can be converted into an animal, but many dogs, that is, a list of dogs cannot be converted into a list of animals. Since it is not allowed, we can't do anything about it. But in the case of arrays, it is allowed. So let's make an array of dogs. I need an array of dog type. I will call it dogs equal to new dog of size 10. So I made 10 size garras. Now I'll do the same. I'll make an object of animal type. Animals. And I'll try to point it to the dogs. See, I didn't get any error. There's just a warning. It's saying that this local variable is not going to be used. But I didn't get any error. It means Java is allowing it. But it's very risky. This is risky because by default in this whole list, there must be nulls. This is the list of dogs. For now, there must be nulls in this list. Now you imagine, what if I do like this, that this list of animals, put new animal on its 0th index. See, I did like this. My code is saying no problem. I will still go. Now let's say I do loop through in this list of animals. So for every, let's say, animal, in small, in animals. I looped through this list of animals and I said that this animal's Now will this work? See, again I had a docs list array and I put this docs array in the animals reference variable. Now what did I do? I put a new animal at 0th index. And I am doing this normally. I don't have any compiler error. Now because I have a reference variable of animals and the list is of dogs. So I can put a new animal at the 0th index of this animals. Because it is making a logical sound. And I am calling the eat method of every animal. Now whether it is a dog or an animal, both have the eat method. Due to inheritance. Now as soon as I run this, I will get an exception which is array store exception. Array store exception is a new type of exception. Which is saying that the element you have stored is not of the correct type. Now let's do one thing first. Because I have put an animal on 0. Before that, let's put 2-4 dogs. And then let's see if anything else was being printed or we got a runtime error. So first I put a dog on animal 0. New dog. Something. like this and copy paste it ok so I have put it in 4 I have put a dog in animal1, animal2 and animal3 so let's say I have put a dog in animal1, animal2 and in our third index so in the starting 4, the 10 size area, in the starting 4 there are only dogs but now what I do is that I put a new animal in the fourth index of the animal like this, I put this Now I'll call this loop for every animal in the list of animals, we have to call it animal.eat Now one more thing, in 5 elements, my. eat can be easily called But after that, the remaining 5 elements will be null stored And if we try to do null.eat, we can also get a null pointer exception So for that, I'll put one more condition here that condition will say that if let's say animal equal to null if there is nothing there then we will directly continue something like this so that it comes out of the loop ok so now let's run it once and see what output we get so see what I got directly array store exception ok so basically it is the same error that in your dogs list you have stored an animal which is not allowed child can't point to parent but if I remove this or if I make it a new dog then my code will work easily Eating, eating, eating, eating, eating. And for the rest, I won't get any print because they won't take it. So it will continue and they will go out. So this code is correct. But because I am able to do this new animal and I am not getting any error at the compiler time. And whatever error I am getting, I am getting it at my run time. So this is a matter of issue. So the errors in Java are not invariant. We call them covariant. This is covariant. Because this property allows. But this is unsafe. Java's generics are invariant, they don't allow this property. If you have seen, it won't allow it. The compiler will give us the error here. Right? That's why it's invariant but it's safe. But now you think, if this problem exists with generics, then I can never keep one generics in place of another. What does it mean? It means, suppose I have a function, if void printAnimals Which prints the animals. And it expects that you give me a list of animals. List of animals. Let's say A. And I will print that list. So what will it do from inside? It will basically loop through from inside in this A. And it will print all the animals one by one. So I have made this method by myself. But I have to use this method to print all the dogs. Because all the dogs are also animals. But I cannot use this. This means that if I am calling print animals in my code. Let's see a dog's list. Let's say I give here dogs. Now what is dog here? Let's say dog is a list. List of dogs. Dogs. Dogs. Something like this equal to new ArrayList. Suppose this code is written here. So this is a list of dogs. Something like this. So I have a list of dogs. But I can't pass this list here. There will be a compiler error. Because again the problem is the same. Here I am expecting the list of animals. Here I am giving the list of dogs. Logically it seems possible. Because every dog is an animal. So where I am expecting the animal, I can also give the dog. But this will not be allowed. If you assume that instead of this list, I am expecting an animal. Animal A. And if it gives a dog d instead of a list, then it would be allowed because generic is not being used here. But as soon as generic is used, it will not be allowed. So I will have a lot of problem in writing code with generics. Now, if this problem does not happen, that's why Java has introduced a very amazing concept, which is called wildcards. Wildcards. How do we represent this? We represent it with a question mark. Now, what is this wildcard? Let's see. Imagine that I have a list which can store anything. Which means a list of any type. So I wrote here wild guard list. So this is a list which can store anything. Now what does it mean to store anything? See, storing anything is simple. I can write here equal to new ArrayList of type anything. Now I can also pass animal here. Right? Let's say I can pass dogs here. If there is a cat, I can pass a cat too. I can pass anything. I can pass any list here. And what will this list do? It will take that. It won't have any problem. So basically it's a list of any specific type. We don't know which specific type it is. So I can write this as list of specific type. But we don't know what it is. We don't know what. We don't know now. When will we know? When we actually pass a list in it. Then we will know. Now imagine again in this problem where I had made a method named printAnimals. And I had said that if I delete it again. And I had said that what does it expect here? It expects a list of animals. Animals. So, I was saying that wherever this list of animals is expecting, I am not able to give dogs there. Because in Java, in generics, your parent-child relationship breaks. So, what if I write list of wild guard instead of list of animals? So, basically, I have written here list of wild guards. And the name of the list is still the same. So, now if I have made a list of wild guards, in this list, I can pass anything. So, I can pass any method call, any argument, whether I pass a list of dogs, whether I pass a list of cats, whether I pass a list of animals, whether I pass a list of integers, whether I pass a list of strings, whatever I want to pass, I can pass here. And this was the amazing thing about wild guards. Now, imagine a very interesting thing. If I have a method, let's say void Fun is a simple method that expects a list of wild guard. Its name is list, small list. I have made this method. So I am assuming that because Java has given wild guard, I can pass any list in this method which I can do. Instead of this list, in argument, I can pass dogs, animals, string, object, anything. But you think, can I do this? List.add Let's say I add a string hello here. Can I do this? The answer is no, not at all. Because if you apply the first thought principle, now I can't do this because Java doesn't know what's going to be passed in the list. Right, someone will call this method. And when is the method called substitute? At runtime. Suppose someone calls this fun and passes a string list in it. Then I can add hello. But suppose someone calls fun and adds a docs list. Then adding hello will not make sense. So our compiler doesn't know which type this list is storing. So how will it know which type I want to add? If you add list.add If you do new dog, it will not allow it. Same reason is that our compiler does not know what values are going to come in this list at compile time. We will know all this at runtime. We will not have any knowledge of this at runtime. So Java says that you can do this, but you can't add anything to this list. You can only read this list. And how can you read it? By using object class. Because you think, you can't do this, so this is not allowed. But you think, If you are reading something from this list, then you are asking to bring the list.get on the 0th index. But in which data type will you collect this? Again, same problem. We don't know what's going to come in this list. It's possible that we as programmers know because we are calling fun and passing string here. So we know that we can receive in string. But our compiler doesn't know. Now again, the question can be the same. How does the compiler not know? It can also be seen that we called fun by passing a string. That's why it doesn't know. Because in this case, it's obvious. But in some cases, it can't be obvious. It can be that a call is coming from some other file. Some API call comes through this method. We don't know, right? It can come in any way. I just know that I am expecting a list here. And it can be of any type. Compiler doesn't even know about this type of list. That means we can't receive this value in any particular datatype. Except only one datatype which is our object class. And that too because object is the parent of all. So we can write object obj equal to list dot get. So we can do this. This is allowed. But we can't read it in any particular datatype. Okay. I hope you understood. Now let's go to code and implement this. Yes. So again I have made my empty file by doing demo2. java. Let's write our main function in it. And I have already imported the list and array list. So let's go back to our problem. So I told you that let's say I have a method. I have a method void fun. Which expects a list of mine. And what should that list be? It should be an animal list. So because I haven't made a class. So let's pick this class from demo.java. First class animal and class dog. I copied this. My hierarchy is here so let's say I expect a list of animals And I will just loop through the list of animals in this list I will call the eat method of that animal. Simple. I will call the eat method of that animal which I can do. Because the eat method is defined in the animal and my variable is also of the animal. Okay. Now imagine that I have a list of dogs. So list of dogs. Dogs equal to new ArrayList. Okay. Something like this. And let's do one thing. Let's add 2-3 dogs in this dogs. So dogs.add new dog. So I added one dog. Now let's add another one. So I added another dog. So I have added two dogs. So basically my eating should be called twice. But can I actually call this method? So first I have to make this method static. So static void fun. So can I call this fun by giving my dog's list? I can't do it. I have an error. And what's the error? The method fun in the type demo is not applicable for the argument list of dog. I can't pass the dog's list in the animal's list. So this is not allowed. But suppose I have a list of animals. So list of animals equals to let's say I have written new array list. Something like this. Let's comment this because this is not allowed. We made a list of animals and let's say again I add two animals. Animals.add new animal. I added this one. And let's add one more animal, animal.add new animal. Okay, so I have added two elements to this list. So can I call this fun method now? Something like this, fun, and I have passed animals here. Animals. Something like this. I can do it, I don't have any error. If I run it, then I have two times, because I have two animals in this list, then the eat method will be called twice and I will come out of this loop. Let's run it once and see. Eating eating, I got an eat call twice and then I came out. Okay, we knew that it is not allowed to pass the list of dogs in the animal list. Okay, what about if I make this method generic? Again, I will copy this. And let's comment on this too. Okay, I am making a new method in the same way. Static void fun, which takes the list of wild guard, animals. Okay? Now, as soon as it takes the wild guard, and I can't give its name animals, okay? So, I give it values, because I don't know what's going to come in the list. So, animal name is not correct. Okay? When I'm looping through this, then I can't write animal, animal here too, right? Because I don't know if the object of animal class is going to come or not. So, what can I write here first? I can write object, because I know it's safe to write object. So, for every object, obj, in the list of values, whatever is coming, Whatever is coming, let's say, what can I do in it? Obviously, because it is obj, I can either call. toString method in it, or I can call. equals, I can call hash code, etc. I can only call object-specific method, because I don't know what is coming. So, let's do one thing, let's print something, system.out.println. I have a method in object class, obj.getClass. And in that get class, let's name it. get, so that I get the name of the class, what is coming. And I keep printing it again and again. So I have taken the list of wild guards. So let's first run it in animals. Let's see what it will do. It will take the list of animals. And because it has two animals added. And I am calling fun animals. So as soon as two elements are added. It will give me their class name twice. So let's run it once and see. So I got animal animal twice. Now if I comment this and go back to my main question that can I pass the list of dogs? The answer is yes, I can do it. I didn't get any error. Because I am passing the list of dogs here. Where am I collecting this? In the list of wild guards. So I am collecting it in the values. And I am saying that I should print the class of every object in this value. Now when I run this, I will get dog dog. I hope you understood what we are doing. How we have used wildcard in generics where parent-child relationship was maintained so that we can represent one list with another list. Or in general, we can represent one generic with another generic because we need it. We can say that we will expect anything, you give us anything and we will print it. But now the main point that I talked about, you comment it for now. Assume there is nothing in this fun method. Can I do this? I have a list of values and if I add let's say 10 in it. Something like this. I can't do it. I will get an error and what will be the error? The method add in the type listof is not applicable for the argument int. It is not applicable for integer argument. Can I add a new dog in it? Because I know that a dog is coming from above. I can't answer it. Again, I will get the same error that the method add in the type list is not applicable for argument dog. Because our values don't know till compile time that who this list is going to represent. So how can I add in this list? Java won't let me do this. This is wrong. I can't do this, I can only read the values. And how can I read that? In the object class. If I write again, I can do this. object obj equals to values.get at index 0. I can only do this. Bring whatever is on the 0th index in the list of values. And if I want, system.out.println And what I do is, whatever I get, obj. I get class.getName of it, then I get the name of its class. I can only do this. I can't do anything else. Here I just did the previous index. And in this loop, I was printing the class name of every element of this value. There was only this much difference. Okay. I hope you understood this. Wildcard means that I can expect any list but I am not able to do much. It is very flexible because it is allowing any list to come to me. I am not able to add any value from that list and if I want to print then I have to print through object class and object class has limited methods. I am not able to call the methods of this particular class like animal, dog, eat, walk, bark etc. So these wildguards provide us with very limited scope. So what we did is we used these wildguards with bounds. Remember we did an upbound by doing extends in the last class. If we use generics with that then it will be so much fun. So I made a file called demo3. java here to show you the same example of bounds. Again, I'll copy my animal class and dog class and create a static method. I was expecting a list of a generic type. Let's say values. So, my demo structure is ready. Now, I was saying that I have a list of animals. So, let's copy that first. I have a list of dogs. So, I copied the list of dogs. So, I made a list of dogs and added two dogs. Very good. Now, I am saying that this is problematic because it is allowing everyone to come. And because of that, I am not able to add any element. Nor am I able to read properly. So, what if I use wildcard wild card with upper bound, upper bound we know what it is, it is extents and any class name, we use this keyword, so what if I write here list question mark extents animal, what does it mean, it means that there is a wild card, it can point any list, but the same list which will be either animal or animal's sub-type. In this world, I will not point to every list. Means if you send me a list of strings, then I will not collect it. If you send me a list of integers, then I will not collect it. If you send me a list of animal or subtypes, then only I will collect it. If I write it like this, then see how easy my work is. Now if I write like this, then I can call this fun method. Obviously, I was able to do that earlier too. Now I can collect it too. Now you think, is it necessary that I collect it in an object class? Should I write like this, object obj equal to values.get at index 0. Is it important for me to write this? Which was important for me to write earlier, obviously. So where did I write? Here. Object obj equal to values dot get and index 0. I wrote this. Why is it not important? Think about it. When I wrote this, now the compiler already knows that the list that is going to come in this values reference variable, it will be at least animal. It may be animal or it may be its subtype. Because what I am allowing is either animal or its subtype. So it will be animal. So I can collect it in animal also. Let's say I wrote animal A equal to values.get and index 0. Now this is absolutely allowed. Compiler didn't give me any error. So compiler already knows that whatever will come in this list will mostly be animal. It can be dog, cat, anything below animal. But nothing above animal. Because I don't allow that. So, if this is the case, then I can read these values for every animal A. Because I know that the values that are going to come in this can be maximum animals. So, I have read the animal variable. And I can call the method of the animal. So, I called the eat of the animal. So, can I not do this? Yes, I can. Now as soon as I do this, and I pass dogs here, there is no problem, it will take the values of dogs. Because every dog is an animal, right? And it will go to that list, in the list of dogs, and it will keep calling a.eat. So twice a.eat, means this method will be called, eating, eating. And our output will come on the screen. So if I run it once, then I got eating, eating. Interestingly, instead of this, I can also pass the list of animals here. So I can also write a list of, let's say, animal. Animal. And here I have written animals And I have added two animals here Two animals have been added Ok Here the list of dogs is written Here we write animals, animals And I will comment this for now So I can do this too And here I will pass the list of animals Even now my eating eating should be called twice Now obviously there is no difference between the two So I can make an eat method in our dog too Which will override it So it will override this eat And here we write dog eating And here we write let's say animal eating Something like this So in this case my print should be animal eating And in our upper case Basically, in this case, our print should be dogs eating. So, if I pass dogs here, something like this, then my print should be dogs eating. Absolutely, right? So, I hope you understood. But now, what is in this list? Can I add anything other than these two? Can I do something like this? If I write, list of integer... L is equal to new ArrayList. So I wrote this. Now can I pass L in this list? The answer is no. There will be an error that the method fun in the type Demo3 is not applicable for type ListOfInteger. It is not applicable for this. So I can't do this. I have to comment this. I can only pass dogs or animals here. Or I can make another subtype. Maybe I can make a cat that extends animals. Okay? Now I can read easily. I don't have to read in object class like this. Now the issue was that I could read in object class. But I could call limited method in object class. I could manually typecast this obj in animal A. For that I have to put Sorry, I have to put animal. I could have done this. This is allowed. But again, you know the risk in this case. In this case, the risk is that I may get class cast exception. Suppose, if the list of values does not come in the animal list, if the list of integers comes, in that case, class cast exception will come. But if I have a list of animals or a list of dogs, there is no problem in that case, this code will work. So this is a risky code. So basically, what I mean to say is that I can do this using normal wildcard, if I don't do any extents. But again, what problem will I have? I will have runtime problems. That I will have runtime exception. But what I always prefer is that give me an issue at compile time, don't pull it till runtime. So here it will not give an issue at compile time, but it will give at runtime. But in this case, this is not the case. In this case, I cannot pass the list of integers. I can do it here because it has simple wildcard, it can expect anything. But in this case, I can't pass any other thing other than animal or its subtype. So, I will never get run-time exception. But I can safely refer to animal class whatever I get in this list of values. Right? Very good. Okay. So, I don't need to collect in object class during reading because I won't get class cast exception like it comes in this. Okay? So, here I have it safe. But what about writing? Can I still add some elements? Can I add like this? values.add. Can I add a new animal in this list? The answer is no. I still can't do it. Although I am sending a list of dogs and I am collecting in this values which is animal or its subtypes, still I am not able to add. And if I go to the error, it will still be the same error that the method add in the type list extends animal is not applicable for argument animal. Okay. Why won't it come? Now think about this from the first thought principle that why Java is not allowing me to add a new animal here. Whether I add a new animal or a new dog, it won't allow me to add anything. Even though I am expecting animal or its sub-type. I am expecting animal or its sub-type. So I am asking Java to send me either animal's list or its sub-type list. I don't know but send something from them. Suppose I have a class like this dog. of cat class cat and that also extends to our animal because cat is also an animal something like this so suppose if someone sends me a cat list from above so in this list, cat's list will come If I add dog in that list, then it is a very difficult thing. Because it is a list of cat. How can I add dog in a cat's list? Because they are siblings. Sibling relationship is not possible. If someone sends me an animal, then it is okay. Then I can add animal and dog. Or if someone sends me a dog, then I can add dog. But if someone sends me a cat, and I am adding a dog. This will be a problem. Again, this is our sibling relationship. And if someone sends me a dog and I am adding an animal, even then it will be the opposite. What am I doing? I am trying to add a parent to a child's list, which is not possible. That's why Java doesn't allow this. Java says that I won't let you do this. Because it's possible that we write a nonsense code in which there is a sibling relation or a child to parent which is not possible in Java. It's not possible through inheritance or polymorphism. Right? So if I explain it here, then you understand that my fun method expects me a list of anything that extends any value. It was written like this. Let's assume it's L. This was our list. Let's assume we have the hierarchy that we have animal at the top. And below that, we have dog which is an animal. And cat which is also an animal. And I'm asking you to send me anything in this list whether it's an animal or a child of an animal. And if someone calls this function from below and sends a list of cats. And here I did it by mistake. The name of the list here is L.add. And I added a new dog. New dog. This doesn't mean that I had a list of cats. So let's say there is a cat here and there is a cat here. But here I added a dog. Which is not possible. How can you add a dog in a cat's list? Because cat and dog are siblings. There is no relationship between them. On the other hand, someone sent me a fun of. Let's say I write it correctly. Someone sent me a fun of. Dog. I was sent a list of dogs. Okay. So if I got a list of dogs and by mistake I made L dot add new animal. New animal. So this is also wrong. Why is it wrong? Because I added an animal in the dog's list. I can't add a parent in a child's list. That's why Java won't allow me to do this. So I can do reading. Reading is possible. Because obviously, what did I say? What can you send in this list? You can't send anything above animal. And you can send anything below that which is animal's subtype. So basically, you can either send a cat, a dog or an animal. You can send anything from these three. I can collect it in animal. Right? So I can write in code that animal a equal to whatever is in this list, l dot get at any index 0. So, I can write this. Or I can also do loop through. So, I can write in such a loop, for every animal a in the list l. I can also do this. Because I know in this list, either animal will come, or cat will come, or dog will come. But whatever comes, I can represent it through animal. That's why reading is allowed in this case. But writing is not allowed. So, we can say this, when we write wildcard using extends, we get covariant, which we talked about above, which we used to get in the case of arrays. Right, here we get covariant. But suppose I want to do editing, I want to add something to a list. What can I do in that case? In that case, Java has given us a very interesting keyword which we call super. How does this work? Let's say I'm making a list of wild guard super animal. Now what does this list mean? Think about it. What can this list expect? This list says that you send me anything that is minimum animal. Minimum animal means if we go back to our hierarchy, then we have an animal class, below that comes a dog class, A cat class will be equal to it. Something like this. And above animal, our object class, which is parent of every class. Something like this. So here I am saying, in this list, because I have put a super keyword, I am saying, send me anything that is either animal or its super type. Basically, I am saying the opposite of that. So either send me animal or object. But you can't send cat or dog. At least animal. I have drawn a line here. Send any above class. Below class is not allowed. But in this case, I was saying that send any class here because this is a maximum window. No class above this is allowed. Send anything below, it is not allowed above. Here I am saying that send anything above, it is not allowed below. Okay, exactly the opposite of that. So now think if this had happened, then what could I have sent in this list? Suppose if this function expects this list, if something like this had been written, let's say fun and you send me list of question mark super animal. So basically the function call expects this. So when I call this method, fun, what can I send? You think about it. I can send list of animal. I can send list of object. Right? I can send this. I think I can send these two because in our hierarchy there is either animal or object. There is nothing above that and I can't send anything below that. So I can send these two with fun. If I make my fun class again by broadening it a little bit. So I wrote that this is my fun class and it expects list of question mark super animal. And the name of the list is let's say L. And here I have called my method And let's bring our hierarchy back here So I had hierarchy So this was my hierarchy and this was my method I am saying that someone can call fun from below by giving the list of animals So let's say he can send animals here Or someone can call fun by giving the list of objects So he can send objects Suppose this is a list of objects And suppose this is a list of animals So, can I send something from these? So, can I not add new animal in this list? New animal. I can absolutely do it. Think about it. Put first thought principle. If I know that I will get minimum animal. Maximum can be anything. Maximum can be anything above animal. So, I can get object also. And if something else is above, then I can get that. But above all, because object class is there, then I can assume that maximum... I can get anything, I can get an object, but at least I will get an animal. So if I get a minimum animal, then why can't I do l.add new animal? You think, if I get an animal, then I can add an animal to the list of animals. But suppose if I get an object, if someone passes me an object from here, then I can add an animal to that object's list too. Why? Because animal is a child of object. So if I have a list of objects, in which all the objects are present, then I can also add animal A1 in it. Because animal A1 is an object after all. Every animal is an object. In fact, I can add a specific dog. So if I extend this hierarchy a little more and make a specific class here, Labrador. which is a type of dog and let's say I make another class German Shepherd that is also a type of dog so can't I add new Labrador here? something like this or else add L dot new GSD, German Shepherd, can I add this? The answer is, yes, I can. Whether someone has sent me the list of animals, or the list of objects, in all of them, I can add Labrador and GSD. Because every Labrador and GSD is a dog, and every dog is an animal, and every animal is an object. Now, because all the classes, above Labrador and JSD, then I can add both of them because their reference variables can point to them. A parent class reference variable can point to a child class object, we know that. So I can add these three things to them, there will be no problem. So my writing work is done using the super keyword. Now, because I have unlocked writing here, then again my reading will be cancelled. Now I can't read it the way I was doing it in the way of extents. Why can't I do it? You think. We know that what will I get? Minimum animal will be available. It is possible that I will get anything above that. So, what should I read in this? Right? So, basically, if I make a little space here, then I will write animal here. a equal to l dot get zero. I can't write this because it's possible that if someone sends me the list of animals, then there is no problem. But if someone sends me the list of objects, then there is an object here. Now I can't point that object in a child class. The object is a parent. I can't point the parent in a child class. So this will not be allowed. So again, if I have to do reading, then how will I do it? I can only read in the object class. I can write this object obj equal to l dot get zero. This is allowed. But this is not allowed. This is not allowed. I can't write this. Okay. I hope you understood. If I am writing super, then I can write, not read. And if I am writing the upper extents, then I can read, not write. If I want to read in the case of super, then I will have to read in the object class. Okay. So let's quickly go to the code and see. Yes. So I have made a file of demo4. java. And I have imported my list and read list. Let's do one thing. Let's bring back our animal class and dog class. Let's bring the cat too. So, this is my class. Okay. So, I have brought all the classes back. Now, I was saying that I will make a new function. Let's say public static void fun. And this time what are we understanding here? We are understanding generics with lower bound. We call this lower bound. Which we represent using keyword super. Right. We are giving lower bound. We are saying send any class above this. Don't send it below. So basically I can accept list of any class. List of any class. That is super of animal class. Something like this. And I call it let's say values again. Okay. And let's do one more thing. Let's pick up our list again. So I said. Let's say I have made a docs list. Let's put the list of animals in the list of animals. And in this list of animals. And in this list of animals, we pass a new animal. Something like this. So I made a list of animals and passed two animals in it. So what did I say? You can send me any animal or its supertype. Now I am saying that I can do addition in this. So I can do this. I can do values.add new animal. Because I know that whatever will come here will be minimum animal and maximum can be anything. Now whether it is an animal or anything on it, I can add an animal to that list because an animal is the child of everyone. Either the animal is equal to the animal or the child of God. So see this code should work. Now see two animals were already here, I added the third. Now if I want to print these three, I have done this writing. This is allowed. Writing. This is allowed. Now if I want to print the list of values because there are three elements in it, two are above, I just added one. So can I print directly from the for loop, as I was doing earlier, for every animal a in values. Can I do this? The answer is no, because I said reading is not allowed and I have the same error as type mismatch. Cannot convert from element type to super animal to animal. Basically, reading is not allowed. Simple reading is not allowed. I have only one method. I can read it in object class. Object obj. I can do this. And then I can typecast it in animal. Animal a equal to animal. And obj. I can do this. And I call a.eat. Let's say. Every time. So in eat, I should have animal eating called three times. Okay. So let's print it once. I didn't call the function so let's call the function by sending the animals list Now let's run the code And I got animalEating 3 times which I had expected So there is no problem Here I did simple writing I added new animal to my values To do reading, I had to take help of object class Now again you can see here Let's make a further hierarchy labrador which extends to the dog Even then there is no problem Even then in this list Whether I add animal or values.add new Let's say labrador I can also do this. Why can I do this? Because Labrador is a type of dog and dog is a type of animal. So wherever I am expecting a list which is minimum animal, I can add animal, Labrador, dog. Let's say values.add new dog. I can also add this. In fact, values.add cat. Dot add new cat. Basically, I can add any hierarchy of animal here. So, the issue that was coming to me, before adding, now I won't get an issue. Right. Yes. So, basically, we have read two things. We have read a list of wild guard animals. extends any datatype, let's say T. I will call it generic. And we have read one, list of wild guard that extends super of type T, something like this. In the above example, T means animal. Right? We know this. But basically, we have read two things. A list of wildcard extends T and a list of wildcard super T. Now, which one should we use? We must be able to understand this. We use this for reading. And we use this for writing. Something like this. Right? The one we use for reading, which property is it? It is covariant. And the property we use for writing is called COUNTERVARIANT. And in our normal t, we expect a list of simple t. In that, our parent-child relationship was not maintained. And we used to call it INVARIANT. We understood this. Now, here comes a rule, PECS rule. It's very simple, it's the same as what you have read till now. It's just a little expanded. It says, its full form is Producer Extents Consumer Super Consumer Super What does it mean? It says that if there is a producer, he should use an extend keyword and if there is a consumer, he should use a super keyword. The meaning of saying this is very simple. Look at this carefully. If someone is producing data, then you have to read that data. And when can we do reading? Using extends. We can do reading through extend. So you have to use extend. But if someone is consuming data, then you have to provide data. And to provide data means to write data. And when can we write? Using super. So, producer extend, consumer super. Simple. If someone is producing, you will read it, so you will use extend. If someone is consuming, you will provide it, so you will use super. That's it. It's a very simple concept. So, now let's understand a very important concept where many kids get confused. That we... When should we use t or when should we use simple wildcards? Okay, what does t mean? Any data type Let's say, I had just explained to you that we make our generic class So how do we make a generic class? We make a class, let's say box And it takes a t data type And let's say, what will happen in that? An object named t Let's say, a variable named value Now, t can be anything So basically, when I am making an object, then I will determine So, let's say, I have to make a box of string Then I will say, let's say b1 Equal to new box. Something like this. I can do this. Right? This is allowed. But can I use wild guard instead of t? Can I make a box which is of wild guard type? If I use wild guard here and here as well, I will get an error. And what is the error? syntax error on token this identifier expected so this wild guard is not expected in such places right when is it expected when you are talking about generics in containers right when you want to denote one list through another list or you want to change it then this wild guard is allowed but in general you are not allowed this wild guard ok so you can't write here we understood this ok So here will be t and here will be t But when I used to make a method and make it generic, how did I make it? Let's say I have a public static method and I name it fun I want to make it generic So I want to take two types t type a and comma t type b So it takes a comma b and what I had to do for that Before its return type, its written in written type void fun Before its return type, I have to take t I had to declare it. That's why I used to say, what is my T here? Parameter. Type parameter. Something like this. This method is absolutely correct. I am not getting any error. But can I use question mark instead of T? Or question mark here as well. So I will say I am giving question mark. I can't do it at all. Again the same error will come. This is a syntax error. These tokens don't know what it is. So we don't use wild guards at these places. So you should never get confused. T means any data type that you are expecting. And when do you use wild guards? Which breaks the generic parent-child relationship. In that case, if you want to make a container or a list that you can expect any list, in that case, you use wild guard. In that case, we use wild guard. I hope you understood this. Secondly, we understood this super and our extends keyword, extends, Where should we use that? Did you understand in the case of wild guard? And we talked about it in the previous video in the case of normal data type. Yes, so if you understand all this now and you are enjoying in generics, then there is one last topic left in which you will have a lot of fun, which is type erasers. What is a type eraser? Now this is a little bit of an internal topic in generics, which many people do not know. So I start it by asking you a question. What do you think? Our JVM knows about generics. So I'll write it like this. Does JVM knows generics? What do you think? What will be its answer? So basically, our code goes through phases. We write a source code. Let's say demo.java. It was our file. We compiled it. Our bytecode became demo.class. This is our bytecode. Now this byte code runs on our machine And who runs it? JVM runs it And when JVM runs this code, we say our code is running, right? It is being executed So when our code is being executed, we call this phase as run time This is our run time phase And when our code is in this phase, we call it compile time Now the question is, does JVM have information about generics? If we write that we have a generic class, let's say box, which we were making till now, which is of type T, and it has only one thing, T value. So this code will be compiled into bytecode. So what will happen when it is converted into bytecode? Will JVM see the same T that we see? The answer to this is no. JVM doesn't know about generics. Basically, we erase the T types that come to JVM. What does it mean? If I explain it a little carefully, when this source code is compiled, your compiler will write such a code from the back that class box It will remove this generics and simply open it. Now simply because it removed this generics, it removed this T type. So who can replace it? Only object can replace it. Object value. Because no one else can replace this T. We know that object is the parent of every class. So let's replace object. So it makes this file in the byte code. And JVM runs this bytecode. It doesn't know that we have written a generic class T and we have taken the T value. Even if we have a class, let's say a pair class, and it takes two generics T, U and we have two values, let's say T first and U second So we think that we have used the generics but internally again the compiler will change it to class pair and here it will remove all the generics and replace them with object first and object second Something like this. So basically JVM doesn't have any idea that Generics was ever implemented. So Generics is just a safety layer of compile time. It has nothing to do with run time. Run time doesn't even know that you used Generics. Now why does this happen? Why is it removed? I'll tell you in a while. But first let's see the rules. How does a compiler remove them? Now look, the first rule is simple. If there is no bound, if no bound. There is no bounding. So simply replace with object. Replace with object. What we saw above. We said we have a class box. We have a class box of type T. And what is in it? It is T value. It is just written. So what did we say? You replace it with a class box. And in it, make the T type object type. Object value. Something like this. But what does the second rule say? That if there is a bound Second rule says that if there is a bound If bounded If bounded Then replace with the bound Replace with bound Okay, what does it mean? Assume that it is written like this Class box But which is this box class? And it is written as T extends number Okay Something like this And then again you know what will happen here? T value So, if our class is bounded, it means that we can pass any T which is either a number or a child of a number, which can be an integer, double, float, etc. or a child of number, which can be an integer, a double, a float, etc. So in that case, when the compiler converts it into bytecode, it will replace this T with a number because we don't need to replace it with an object anymore. What can be our maximum class? Number class. In this case, we had to replace it with an object because we didn't have any boundation. So we could have put anything instead of t, any class. So we decided to put the object which is the largest. But in this case, because we already have a boundation, no class above number can be sent here. So the compiler will quickly change this code. Simply, in this, class box and what will it put inside it? Number value. It will change some such code. Okay? Now, the third rule. Third rule says that we have to insert casting automatically Our compiler automatically inserts the cast Because we had the class box which we had written of type T And we had expected the value of type T This was our code which we had written Now what did compiler change this code? Compiler said that I will make class box without generic i.e. I will replace this t with an object and here I will keep the name obj, let's say value. So our compiler said that I will produce this code. But you think, you must have written something like this in your code, if you have made a type of t, then how would you make its object? Some such box of let's say integer b1 Equal to new box And let's say you put 5 value here Something like this And you can get this 5 value anytime In integer You can do like this Integer x equal to b1 dot value This code is absolutely safe, no error will come because the compiler knows that t means integer. So you have added integer, no problem. And if you do b1. value, you will get integer only, no problem, you are expecting it in integer. But when the compiler changed it in byte code, then what did it do? It removed the generics, which means it wrote the object here. This means that this part of the code, the box and the integer written in the bracket will also be removed. So now it will become like this, box b1 equal to new box. Without generic, it will be something like this. And you have given a value here, 5. So 5 value will go in this value. But what is its type? It is an object. If the type is object, then can I directly get it in integer? integer x equal to b1. value Can I do this? The answer is no. So what will the compiler have to do? Here, cast will have to be introduced of type integer. It will introduce it by itself. We will not see that there is a cast here, but it will introduce it by itself. Because we know that when we want to read it, then we have to cast it in integer. Okay, maybe someone has a question in their mind that if we introduced the cast there, sorry, the compiler introduced it by itself, then again, can we never get class cast exception? What do you think? Can it? The answer is no. Because when we wrote our code through generics, then what did generics do? It gave compile time safety. If we have made an object of integer class B1, then we know that we can only add integers. So we can store B1. value in any integer. Can we store strings in B1. value? The answer is no. Our compile time error will come. So if we are sure that the value in this B1 is of integer type, that our compiler will be sure, So even when our compiler changes it in the way of object, we know that when we do b1. value, we will always get integer. Casting is just a formality here because Java expects that if you want to convert a generic object, which is our object class, into a specific object, which is our integer, then you have to cast it. This is our syntax, Java's syntax, that's why it casts it. Now, if you understand that all our generics are removed till the runtime, then you will also understand that we can't do a lot of work with generics. Do you know what we can't do? We can't use instance of operator. We can't write like this, let's say we have a list of type string L, equal to new array list, something like this. You have a list. You can never check like this that you have written here if l instance of list of strings You will think that the answer is true. And our if block should be executed. You can't write like this. You will get an error. Why can't you write? Because you know that when it goes to runtime, it will remove all the generics. So basically, we will not change this line and write like this. List l equal to new array list. Something like this. All the generic information is gone. So in this case, list of string, whether list of Integer ho, kuch bhi ho, wo sab convert ho jaate hain normal list mein. Right? Isle aap ek specific cheez check nahi kar sakte. Doosra, aap method overload nahi kar sakte generics ke basis pe. Let's say aapne ek method likha hua hai void print. Jo ke expect karta hai? Aap say list of string expect karta hai. Let's say L. This is one method. Similarly, you have overloaded another method, print. And you think if I change the argument in this, I will do list of integer, l, then my method will be overloaded. But the answer is that it will not happen at all. Because by changing both the methods, what will happen in the runtime? The same signature will come in both the methods, void print, list l. Something like this. Because our type information, string, integer, all this will get lost. And the next one will also be the same, void print list l, right? So we will have the problem that in method overload, all the signatures are the same. This can't happen. Arguments or number of arguments should have changed. So that's why we can't overload the method. And there is a little more specific, which is compiler generated bridge method. bridge method. Now what is this? Compiler generates one more thing, this is a little more in depth. What happens is, suppose you have a class, let's say parent and this is a generic class, okay? And let's say there is a method in this, let's say get, it is a simple get method, which does nothing, returns null. Simple null return Because we don't know what type t is So we simply did null return Because null can be returned in any type In t, whether it is string or integer There will be some non-primitive Primitive will not be there And why will it not be primitive We haven't seen that in generics Why can't we keep primitives Why can't we keep primitives Have you ever seen that we have sent int instead of integer in our generic list? Not at all, it is not allowed. Why not? We will discuss it in a while. But yes, suppose this is your parent class and after this you have made a child class, let's say class child which extends parent to extend parent and how does it extend? Instead of t, let's say it writes string. So it expands it in the form of string. And let's say it also overwrites our method, so it will overwrite it in the form of string because it is extending it in the form of string so string get and let's say now we return some string so we returned let's say hello so our code is absolutely correct but what do you think when our code will convert jvm then what will it do instead of this it will simply write what we talked about class parent ok and here instead of t it will replace object with object and here it will write return null this is not a problem but as soon as it changes its code then it will write class child extends parent now here it has expanded in the form of string and removed that because it doesn't need that information because it won't keep that information with it and your override method is doing that it is overriding it in the form of string string get something like this And what are you passing here? Return Hello Something like this But you think, are you actually overwriting the method? No, here the method's return type is object, and here you have given the return type as string. It was fine till here, here it was t and here it was string. Because you were extending parent using string, that was fine. But when compiler removed parent's generic and wrote object, then here it was replaced, but here you had written string. Then actually you are not overwriting it. So what compiler will do, it will write another method of type object get string. And here it will call this get method from inside. It will call this get method. Something like this. So the method written by JVM, So the method written by JVM, What do we call this method? We call this method as bridge method. Bridge method. Because JVM writes it by itself. It works as a bridge so that your code can support polymorphism and inheritance. Which it was already doing. Right? Now comes the biggest question, and after this the lecture will end. We just have to answer two questions. The first question is, why is there a type eraser? Why is it that our JVM does not have any information? Because now think, when were generics introduced in Java? Generics were introduced in Java from Java 5. Before that, before generics, if we declare any list, then we do the same. this. List l equal to new array list. Without using generics, we declared it like this. But Java 5 directly introduced generics. And said, from now on, we will introduce list like this. List of type integer. We have to tell type. L equal to new array list. Something like this. We will do something like this, we will have to tell the type from the beginning. Right? So our entire syntax changed. Now imagine, if Java would have told JVM that you should know about generics. Don't you think that all our existing code, which uses this code, which uses this syntax, would have broken? Because it would not be able to run. We would have changed the rules of JVM. JVM would have said, I understand this, but I don't understand this. So all our existing. class files would not be able to run. Entire ecosystem would have failed. So Javan took a choice. He said, let's do one thing. Let's keep JVM only compiler oriented. It is just a compiler test. So it will be a compiler safety test. Compiler safety test. He will do a compiler test. The type safety should be maintained. Once it is checked, then when your code will run, then bytecode will write it in this way instead of this way. So whatever rules we saw above, it will use those rules and convert it in this way. So that JVM doesn't know any difference between the previous code and the later code. Now the second question is also very easy to answer. And by the way, both the questions are very interview oriented. And that is, why don't the generics in Java support the primitives? Not supported primitives. Why? The answer to this is very simple. So let's say you have made a class box of type T. Okay? And inside this, there is a T value. Something like this. And let's say when you were making its object box, you put int in the generics box int b1 equal to new box. Something like this. Although it is not allowed, you know you have to write integer here. But suppose if it was allowed, then what would happen? You know there are type erasers in java. then this line would have been replaced. This line would have been replaced with box b1 equal to new box. And what would have happened inside? Inside, you would have replaced this t with object. But you would have been able to do this only because object is a class. It is the parent of every class. But every class has a parent. And int is not a class. Right? Meaning, if there was an integer here, then it would have worked. That you replaced the integer with object. That would have worked because it is a hierarchy. But you can't replace int with object. Because you know int is a primitive data type. It is in the stack. And integer is a non primitive data type. It is made in heap. And object is the parent of all. What does it mean? All the classes. So object is the parent of integer. But it has nothing to do with int. So if int is allowed here, then you can't replace it with object. So that we can always replace anyone from the object class. We said that primitives should not be allowed. So this is a very important rule. That why primitives are not allowed in Java in the case of generics. Right. This is a very full lecture. Okay. I hope you enjoyed it. You must have learned a lot of new things. That's all in this video. See you in the next video. Till then, thank you so much.