Podcast Lookup

Java Generics Explained | Bounded Type Parameters Using Extends

The Java Developers Hub

  • December 3, 2026
  • 1 h 5 min
  • Episode 30
Episode page
About this episodeJava Generics make it possible to write reusable, flexible, and type-safe code that works with different data types. However, sometimes…
Java Generics make it possible to write reusable, flexible, and type-safe code that works with different data types. However, sometimes developers need to restrict the types that can be used with a generic class, interface, or method.In this episode, we take a deep dive into bounded types in Java Generics, focusing on the extends keyword and how it helps define restrictions on generic type parameters.You'll learn how to create bounded generic classes and methods, enforce type constraints, and work with inheritance relationships while maintaining compile-time type safety.Whether you're learning advanced Java programming or preparing for technical interviews, this tutorial will help you understand how to build more flexible and reliable generic components.What You'll Learn
  • Understanding Java Generics and their importance.

  • What bounded type parameters are.

  • Why bounded types are useful in Java.

  • Understanding the extends keyword in Generics.

  • Creating upper-bounded generic types.

  • Restricting generic parameters to specific classes or interfaces.

  • Understanding generic type inheritance.

  • Creating bounded generic classes.

  • Implementing bounded generic methods.

  • Working with multiple interface bounds.

  • Understanding the difference between bounded types and wildcards.

  • Practical examples of bounded generic programming.

  • Common mistakes when working with Java Generics.

  • Best practices for writing reusable and type-safe Java code.

Transcript

From the publisher

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

Java Generics Explained | Bounded Type Parameters Using Extends

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 talk about a very interesting topic which is Java Generics. Now what are generics, what are wildcards, we will study everything in depth. So you will have a lot of fun. Come quickly to the screen and let's start. Yes, so look, before understanding generics, we will have to understand what type safety is in Java. And what does it mean? Type safety. Okay. Look, all the data types that exist with us and their respective values can be. For example, let's say I say that we have a value of 10. Okay. Which I know is an integer type value. In the same way, I can have a value of hello. Now I know that this is a string type value, right? So here I am writing their values. These are my values on the left side. And what is on the right side? Their respective data types. And there can be multiple types of data types in our java. Let's see one more example. Suppose we write true. And I know what is its data type. Its data type is boolean. Something like this. Okay. So basically, the types in any language, specifically Java, tell us a set of rules that you can perform on their values. For example, if I have an integer, So I know that I can perform arithmetic operations on integer. Arithmetic operations. I know this. Now similarly, I have string. So I know which methods I can call on string. I can call string class method. So I can call string methods. Right? Similarly, let's say I have a defined data type. So I must have made my own class, student. So which operations or methods can I call on student? I know that I can call student class methods on student. Student methods. So basically, all the types we have in Java, what do they tell us? They define some rules that what can we do with those types, which methods can we call, which fields can we access, etc. Now the rules of these types in Java, why are they made? They are made so that we can't access their values in the wrong way. What does it mean in the wrong way? For example, suppose I have written int So I know what I can store in this. I can store a number or 10. If I try to store 10.2 here, then I should get an error. Right? Similarly, I know string x or let's say string s. So, here I can store a normal string. Hello. Something like this. Now, can I cast this s directly in a number int y? Absolutely not. Right? Because what is this? This is an integer. That too is primitive. And what is this s? This is our string datatype. Can I convert this string into an integer? Absolutely not. So, here also I will get an error. Right. So, basically we have some rules defined by the datatype. That what operations you can perform on it. Which rules are applied on it. So, in general I can say that our java language. Java. It is a typed language. Typed language. Okay. Means it defines the type earlier itself. Like this data type is integer, so it can have the work of integer. If there is a float, then it can have the work of floating. If there is a string, then it can have the work of string. Right? We can't do any unknown operation. We can never do like, I took a string, hello. Plus 5, any random operation that I'm storing in an integer x I can't perform all these operations In our Java language, right? Now if you understood this whole story Then we'll have to understand one more thing and then only we'll be able to move on to generics That is, upcasting and downcasting Yes, so what is upcasting and downcasting? Look, we had read about casting in primitive data types We had seen that if I have, let's say Let's say I have an int x equal to 4 and I want to convert this x to long. So let's say I have a long l and I want to convert this x to long. So I can write long l equal to x directly. I won't get any error. I won't have to cast anything. Why? Because we know that our integer is small and long is big. So if integer takes this much space of x, then long will take this much space. So x can handle in this long. But if I do the opposite, suppose I have a long, so let's say long L equal to a very big number 1, 2, 3, 4, 5, 6, 7, 8, something like this. And I want to cast it in my int i. So I can't write int i equal to ldirect. I will get a compiler error. It will tell me that you are trying to cast a broader data type in a shorter data type. It will be a narrow conversion. For that case, I have to do casting. So I write something like this. I say that I am doing this knowingly. Cast just tells the system that you want to do this. This is purposeful. So there we understood casting. That was the case of primitives. In that widening conversion, narrowing conversion. Something like this we used to see. Now if we take the rules of casting to non-primitives. Then we divide it into two parts. One we call upcasting and one we call downcasting. So what happens is we understand this one by one. Now, what is upcasting? Imagine that I have an animal. So, I have a class animal. Okay, very good. I will leave it empty now. We can fill in the fields later. Again, let's assume that I have a class dog. Since dogs can inherit animals, I'll write extends animal. This is how we write code. So basically, I have an animal class which is my parent class. And I have a dog class which is my child class. And in the same way, I can make multiple animals as a child. Now, in my code, what can I write? Animal A equal to new dog. I can write it. Why? Because I know that in Java, there is a very basic rule that a parent class reference variable can point an object of a child class. We have understood this. In this case, a dog object will be created in our heap memory and a reference variable will be created in our stack memory. A will be of animal type and A will be pointing this dog. Now we know why we can write this. Think that animal is my parent class and dog is my child class. So let's assume that the animal has some methods. Let's say the animal has the basic method walk and let's say run. These are the two methods. And let's keep eat as well. Now the dog will have these three methods. But it can also define some other method. Let's say it defined a specific method bark. But when I write such a line animal a equal to new dog. So what type of reference variable is this? This is the animal type reference variable. So now the reference What methods can I call in this variable a? The same method which is defined in the animal. So I can do a.walk, a.run, a.eat. But can I ever do a.bark? Absolutely not. Because in this animal, bark method is not defined. Bark is defined in the dog. And if I want to call bark, then I will have to make a reference variable for it. So I will write something like dogD equal to newDog. Right? And now I can call the bark method on this D reference variable. Now we already know all these stories. We were understanding what is upcasting. This is what upcasting is. Basically, you imagine that you have a reference variable named dog. In this case, we have D. Okay? You referred it with the reference of an animal class. So let's say you wrote a line like this. Now you wrote in your code. After all these statements, you wrote A equal to D. Something like this, right? So, as soon as you did equal to d, what would have happened? Something like this would have happened if we understood it from the perspective of heap memory, that this would have been our heap memory. This would be our stack memory, stack and heap. Now, ignore this line for now. Just look at the second line. As soon as I write this line, dog d equal to new dog, what will happen? First of all, an object named dog will be made in our heap memory. And a reference variable d will be made in our stack memory. And d will point to this dog. Okay? This is absolutely correct. Okay. Then let's say, we have written a new line ahead of this, a equal to d. Something like this, which I have written here as well. As soon as I have written this line, what will happen? I am saying, make another reference variable a and this a reference variable is of animal type and this d reference variable is of dog type. So I am saying, a will also point to the one I will draw a line like this, right? Okay. That means, the object of my dog is being pointed at by the reference variable of an animal class. Right? And again, what did I explain to you? Which methods can I call on this A dot? The same methods which are defined in this animal class. I cannot call specific methods of the dog. This is upcasting. I picked up a dog and upcast it. That means, I cast it up. In which? In its supertype. That means, in animal. Right? So, if I say this definition-wise, then what did I do? I made a specific class I converted it into a general class. Now you know that this dog is a specific type of animal. I converted it into a general class. Now imagine that I can write like this. Let's say I have a string s. In which it is written let's say hello. And you know that object class is a parent class. In every class of java. So that's why I took a reference variable of my object class. obj. And I wrote obj equal to s. Can I write like this? Now will I get an error? Absolutely not. Why? Because object class is a parent class of every class. So its reference variable can point to a string class object. Right? So again if we make it in memory then what will happen? We will have a string type object in which hello will be saved. And what will happen in our stack memory? S will be there in the stack memory which will point it. After that, I again made a reference variable named obj and made it point here as well. Again the same thing. Now if I write a dot after this obj, then which methods can I call? I can call the same methods which will be defined in the object. 2 string, equals, hashcode etc. So this is upcasting. I have converted a specific class into a generic class. For this, I don't have to do any casting. No manual casting required. No casting, right? I don't have to write any casting keyword. Like, it's exactly the same that I put a variable of int in a long variable. Int x was written above. I put long l equal to x. Something like this. Okay? So, here also, if I don't have to do any casting by myself, similarly, here also, I don't have to do any manual casting. Now, if we go the other way, then we will understand what downcasting is. Down casting. Now imagine I have an object class. Object obj. And I have stored a string in it. I can do that. Again the same thing. Now I know object is a parent class of every class. And I can store anything in it. So I have stored a string. Now if I write like this. String s. Equal to obj. As soon as I write this. I will get compiler error. Compiler error. Why will it come? It will come because. Object class is our parent class, let's say this is our object class And string class is our child class, this is our string class So string is the child of object, right? You are saying, convert this object into string But it can't do it directly from compiler It doesn't know that the hello defined in object is of string type Because object can be multiple things, right? Integer is also an object, integer class, right? Boolean class is also an object In fact, your own defined data type, let's say student class is also an object Everything is an object You are asking to convert this object directly into a string. Compiler is not sure that it can do this. It doesn't trust that the object is actually a string. So it can't directly convert this obj into a string. So here comes the compiler error. To remove this compiler error, you will have to do manual casting. So apart from this line, you will have to write something like this. You will have to write string s equal to. You are casting it in the string. You will have to tell it by yourself and then you will write obj. Now it will work. And this is what we call downcasting. Right? What did we do in downcasting? We tried to convert a general object into a specific object. If you do this, then again you will have to tell the compiler that I am doing this intentionally. I want you to convert this object into a string. Because again if it couldn't convert then it will throw a class cast exception. So it will tell you that if you know that there will always be a string stored in this object then do this casting and if you are not sure then don't do this casting. Now this down casting can be dangerous because Imagine that in my object class, object obj had 10 which is an integer But if I try to cast it in string, string s equal to obj Now obviously it won't be direct so I have to cast it, so I wrote string in bracket and then obj This is how I have written my code Right? Will I get any compiler error? The answer is no. I won't get any compiler error. Why won't I get it? Because the compiler doesn't know if it can do this or not. If the compiler was sure, it would have thrown the compiler error. The compiler doesn't know this. In runtime, we get to know that there is an integer store in this obj and I am trying to convert that integer to a string. And if you do this, you will get an error class cast exception. Not an error but an exception which is called class cast exception Now some people might be thinking that we have written obj equal to 10 So when it is compiling from the compiler then it must be able to understand this line that 10 is written here So the compiler doesn't know that 10 is written here so I can't convert it directly to string Why am I waiting for this error to run? Very simple reason. In this case, you can see that we have written object obj equal to 10 directly. Compiler should know this. But you can imagine, if we hadn't written 10 here, this 10 would have been coming through a user. Right? Or it could have been coming through an API call. Or it could have been coming through a console or a user. Anything can happen. So obviously, the compilers rules are also written in a general way to handle every situation. We don't know the value of any object. Where does it come from? It can come from user input, file, or API call. We are just telling here that we are converting obj to s. Compiler is not sure. Compiler is saying that I am not sure. If you think you want to do it, then put a keyword for casting here, string. I'll leave it on JVM, it'll check it and if it can't do it, it'll throw a runtime exception. Now, when JVM will try to run it, by then it must have got the value. If you take input from console or call API, then where do we get those values? When our application is running, it must have got 10 values. Now, there are 10 stores in the object. So obj will be in the stack and in the heap we will have an object which will contain 10 Now you will ask how 10 is in the object because we have an object named integer It gets auto-boxed So 10 will be auto-boxed so it will be an object And obj will be pointing this 10 Now I am saying convert this obj in string Before converting our jvm will check the class of both of them Are the classes of both different? And obviously they are different This 10 will be of our integer class And the type we want to convert into is the string type Because both types are different, we will get the error class cast exception So, what we just saw, let's see it in the code So, first of all, let's see the upcasting In which we try to convert a specific type into a generic type So, again, let's take the example of string and object So, I took a string, let's say hello And I am saying that if I convert it into an object Object obj equal to s. So can I do this? Of course I can do this, I didn't get any error. In fact, I can print this too, obj. Now why am I able to print this? Because in my obj, there is a method called. toString, which will be my direct call, so whatever is written in it, it will print as a string. Now what is written in it? Exactly hello is written, so it will print hello. And we don't even need to put a toString here, because you know that println always puts it from inside. So now if I run it and see, I got the answer. Hello, no problem. I didn't have to do any casting. So this is our up casting. Okay, now let's see our down casting. Okay, now what do we do in down casting? We try to convert a generic value into a specific value. So again, this time I go the other way. I take our object obj2. Okay, this time let's put another string in it. Let's say aditya. Something like this. Now let's take our string s2. Let's assign obj2 directly to it. Is it possible? No. Let's see if there is an error. Type mismatch cannot convert from object to string. You can't directly convert because our compiler is not sure. Compiler is saying that you are putting object class, which is a parent class, in child class. Now object can be anything. Why are you putting it in string? Object can be integer, double, boolean, student class, anything. So compiler is not sure. Compiler is saying if you want to do it, then do casting. So I said okay, I will do casting in string. Here I did it. Now as soon as I did casting, my compiler error went away. Now if I want, then system.out.println I can print this s2. Let's comment this for now. And let's see if my output is coming on the screen. Now we will see the case in which we said that downcasting can be dangerous. So let's take our object variable again, let's take obj3. This time I have put a number in it, i.e. 10. I can put it, right? Now how did I put 10 directly in it? 10 is primitive. We know that auto boxing is done. This primitive 10 is non-primitive 10, i.e. it must have been converted to integer class. So its type will be integer. Now what we will do is we will take our string s3. Let's try to put it directly in that. First, obj3 will not let us put it. We will get an error. Again, which one? Of type mismatch. Okay, type mismatch. I have cast it in string. Okay. So, my compiler error is gone. Now, see what happened in this case. The compiler is not sure whether the error will come or not. Okay. That's why it told you to cast it manually. You tell me whether what you are doing today is intentional or not. He left it on JVM that JVM will match the class of OBJ3 and S3. If it happens, then it's good. Our S3 will be printed. If it doesn't happen, then we will get an exception. So in this case, we should get an exception. And we got it. The name of the exception is class cast exception. Now if you understood this, then we got to know that we can get two types of errors. One error can come on our compile time. Compile time. And another type of error can come on our run time. Like our classCastException came on our runtime and some errors were coming on our compile time. For example, if I write int x equal to hello, this is our compile time error. It is showing that you are trying to store a string in an integer. This is not allowed at all. You will get an error on the compiler time. But again, what was the runtime error? When you remember, we have stored a string s So let's say we typecast in the string and we got an obj stored And obj was an object class reference variable so let's say it was 10 So when we wrote this code obj equal to 10 and tried to typecast it in the string The compiler didn't give us an error, we got a runtime error Which error? Class cast exception, we already know that Now imagine, which error is better for us? Compile time or run time? Obviously, compile time errors are much better than run time errors. Why? Because if you get these earlier, then you don't get any bug on production. Your production gets bug avoided. Early catch happens. You get to know the code while writing that you have written it wrong, fix it. So you fix it there. This has a lot of benefits. Your code is safe. Your code is safer. From whom? From a lot of bugs. And on compiler error you also get IDE support So better IDE support Like if we are using VS code Then VS code itself will tell us that you can't do this It will give you a red line there And we still have a lot of debugging left. So if this error comes in production, then we have to debug the error. So we have a lot of debugging left. Because of all this, we will always say that compile time errors are better than run time errors. That's why as a developer, we always want to know all the errors that are coming at compile time. We don't have to wait till run time. Now we understood what is upcasting and downcasting, what are the types and what are the rules of the types, and which is better between compile time error and compile time and run time error. We always prefer compile time error. So now we have to understand what is actually generics. Before that, let's understand what our goal is. What are generics? You must be understanding from the name that we must be generalizing something here. So first of all, let's see what the problem is in java and then why generics had to come to solve that problem. So let's go to the code and see. So I have created an empty file called demo2. java. First of all, let's understand our problem. So let's say I want to make a class, a box class. And this is a very simple class, it does nothing. It holds a value. Because again it is a box, so it will hold a value, there will be some value in it. So what can be the data type of that value? So let's say I say int for now. So basically I have created an int value field here and let's make it private because we generally do that. So now let's make our constructor getter and setter. So let's first make our constructor box which will take an int value and assign it to my this.value. something like this and I can make a getter let's say we keep it public so public int get value what will it do? it will return this.value and similarly we can make a setter public public void void set value something like this we take an int value and this.value we know that we have always done this we have made a simple class box which has one value now we can make its object we can make box b1 equal to let's say new box and let's put value 10 ok very good now if I let's say system.out.println println and I will say b1. getvalue ok so my screen will get 10 print So our class is doing the right thing. But tell me one thing. Now I need this box class. Not to hold an integer. To hold a string. So what can I do? Can I pass a string here? Let's say hello. I can't do it at all. Because obviously, this constructor is made to take an integer. I can't call setValue here because it is also made to take an integer. And who will I set? I have an integer field. I can't save a string in it, so this line is invalid. Yes, I can do one more thing. I can make a new box class. Let's say class box2. Something like this. And I can take a string value in it. Something like this. And I can make the same getters and setters that I made above here. And I can use this as a string box. Now apart from string, if I need a double box that holds double value, then I will make another class, class box3. And what will it do? It will hold my double value, let's say value, something like this. So you must be understanding the problem, right? I have to make different classes to hold each data type in the box. Now you understand my goal. What happens is that I can make a class whose name I will keep as box only. Let's say box. Now let's remove all these classes. Something like this. So I want to make a generic class box. And that generic class It will have a value. I don't know what its datatype will be. But if there is a datatype that stores everything. If I give it a string, it will also store a string. If I give it an integer, it will also store an integer. If I give it a boolean variable, it will also store that. Can we imagine a datatype in Java that can store everything? The answer is yes. Its name is object. Right? Now we know what is an object class, it is a parent class in every class of java. So I can store anything in this object. Remember we understood upcasting just now. Everything can be upcast to the object. So now imagine I have a private field, private object whose variable name is value. So if I make a constructor in it and its getter and setter. So I put all the things in the box, I put a constructor, a getter and a setter which we did earlier. So now you think that my entire code will not be valid here. So let's say box b1 equal to new box. I can also put 10 in this. So I put an integer here. No problem. Similarly, I made a box b2 equal to new box. I can also put a string in this. So let's say I put hello. No problem. This also went. Okay. What about box b3 equal to new box. And if I want to put a boolean in this, then new box. And I put true value in it. So I have stored all the values here. I have stored an integer, a string and a boolean variable. But I will not be able to perform any operations on them. What if I write system.out.println and let's say I want to print. I know that there is an integer value stored in b1. And if I do b1. getvalue then I should get that integer value. What if I do plus 5 in it? Can I do this? I can't do it at all, I will get an error The operator plus is undefined for the argument type object Why? Because Our box class holds an object. And we don't know what this object is. Now this object can be anything. It can be a string, integer, boolean, float, a student, anything. Because object is a parent class. Which is parent class of every class in Java. So our compiler doesn't have the surety that you are getting the value from b1. getValue. Which is an object type value. If you are doing plus 5 in that, what should be the answer? Because if you would have done it in integer, then 5 would have been added in 10. And if you would have done it in string, then it would have been concatenated in any string. Everything has different responses. And if it was boolean, then there would have been no sense of plus in boolean type. Your compiler is not sure whether what you are doing is allowed or not. For that case, if you want to do something like this, we will have to do downcasting. You must be understanding why we have to do downcasting. Because look at it this way, our object class is a very generic class. to generic. Now because of this, our type information gets lost. Type information is lost. Type information means, this box doesn't know that the value which is of object type, is specifically of which type. Whether it is string, float, boolean, it doesn't know anything. And again, if it doesn't know this, then it also doesn't know which operations it can do. As we saw in the starting, we can do arithmetic operations on integer, boolean operations on boolean, etc. It doesn't know which operations we can do on this value. For that, I have to do downcast. How will I do downcast? If I know the first integer, then I have made an integer here, let's say x equal to. So, I will typecast it in integer. Here, my integer capital will come. And I will do b1. getValue. Something like this. My type cast is done. Same, I know there is a string in B2. So I will type cast it in string. So I will write string here. And I will write b2. getValue. Okay, very good. And same, I know I have boolean variable in third. So let's write boolean b equal to and type cast it in boolean. This should also be my capital. Okay. And I will do b3. getValue. Something like this. Okay. Now everything is done. Now I can do anything if I want. I can do system.out.println. I will write here x plus 5, no problem. In that case, my compiler will understand that x is an integer. If I add 5 to the integer, then the value of 10 will be 5, which is 15. In the same way, I can add 5 to the string s. And you know in string, when I add operator, they both get concatenated. So here I have hello, and I have concatenated 5 after hello, so the answer should be hello 5. And the third one is boolean. So if I am doing boolean too, then it can't do plus. It doesn't make any operator sense. So operator plus is undefined for the argument type boolean. So I will do one thing. I will print it directly. Something like this. Now if I will run it. My three things came out properly. Right? I hope you understood why we had to do downcasting. So basically, if we want to make a box class, which is a generic class, which can hold anything, and we treat the object as a universal type, that we will keep the object and its name, let's say value, and it can hold anything, whether it is integer, double, string, float, it can hold anything. If you follow this expectation, then we will have to do downcasting manually. Because we lost the type information. Now this box doesn't know what is being stored in this value. It only knows that there is an object stored in it. It can be anything, we don't know. Now, we know that downcasting is very risky. Because we can see here that b1 contains 10, b2 contains a string, b3 contains a boolean. But let's assume that these values are coming from a file, a server, or Java is coming from some other file or method. We are just getting them. So, if we make any mistake in this downcasting or typecasting, then we will get a class cast exception which is a runtime exception. And what we discussed is that we always prefer Compile time exceptions. We always want to have exceptions before us. We don't have to wait till the runtime. So this becomes very risky. So it means that using objects is not a very good way to make a generic class that can hold anything in that case. This is where your generics come in. So here I have mentioned all the pointers that we had seen that whenever we try to make an object class a universal type class, then what is the problem? Our type information gets lost, right? Now he doesn't know that this value is actually a string, integer, etc. We can insert any wrong value. It is possible that we are expecting a string and then we cast a type in the integer. Right? Or we expected a string but we gave a dull integer value. We can do anything wrong. We have to cast necessarily. We have to cast. If we are doing it in integer, then we have to write integer. If we are doing it in string, then we have to write string. And a lot of errors shift to our runtime, which we don't want. Now see what I have written here, Generics solves all these problems So Java has introduced Generics, which will solve all these problems of the object class So let's go to the code and see what Generics actually does So again we have created our empty file by calling demo3. java First of all, let's copy the box class from demo2. java and paste it here Okay, so our goal is simple. We have to make a general purpose class, box, which can work with any value. Integer, float, string, with anyone. But we don't use object because it's not better. We understood the problem with it. So we have to use something else. And let's say we will use generics here. Now what is generic? Okay, so let's first question that we can't take an object here, it's a data type, right? We have to take some other data type. But apart from object, nothing exists in java that stores everything in this world like integer, float, double, everything, right? It's just an object. So let's do one thing, let's take t instead of object here. Okay, now which data type is this t? This t is not a data type. This t is just a placeholder, I could take anything in its place. I could take x here, I could take z, I could take q, anything. For now, I took t here, which is just a placeholder. I am saying that the type of this value can be anything. I don't know the type yet, so instead of that I have taken T. I don't know when I will tell the type, when I will be making its object, then I will define its type. This is the syntax of generics that I am telling you. Now if I have taken T here, then wherever I will use the object, there I will have to replace T. So let's say I have taken T in constructor. I have taken t in return type and t in set type as well. Now I will get a lot of errors. If I hover over anything, I will get to know that t cannot be resolved as a type. Obviously, t is not a type. Java doesn't know why you have used t. Now we have to apply the generics syntax. which says that whenever you make a class, let's say box, for which you have to make a general purpose class, then in front of it, put a bracket like this, angular bracket, and here mention the name of that data type which you are passing. So here I have mentioned t. We call this, Type parameter. Till now we were told that there can be parameters in methods. Let's say this is our method setter. Here we take a parameter value. We can take a parameter of a class too. So here we took a type parameter. So here we took a data type parameter. Let's say t. Now when will we tell what is t? When we will be making the object of this box class. I hope you understood what we did. Now let's see, we are not getting any error now, which means our syntax is absolutely correct. Now let's see how we will make its object. We write box. Okay, if we know that its normal object was not there, box b1 equal to new, let's say new box. And again, some value was passed in the integer, let's say 10. So this shouldn't be allowed here, because I haven't told it what type of object it is. But I'm getting a warning, I'm not getting any error. And what is the warning telling me? Type safety, the constructor box belongs to the raw type box. What does this mean? We'll come to this later, why are we able to do this? If I tell you a little, then again, Java is treating this box class internally as an object class. Java is imagining that an object is still written here. Why? Because you didn't pass the type And if you don't pass any type then it will automatically think of replacing it with an object So let's do one thing, because we can't write like this We pass our type here So let's say I said that we need integer type objects So I passed the integer here And if I pass here, then I will also make an angular bracket here and pass the integer Now see, I am not getting any error, I am not getting any warning, right? So what did I do? I said, make an object of box in heap memory, okay? And inside that box, the type of our value will be integer. I will tell you right now, okay? And again, if there is an integer type box in this heap memory, then the reference variable we will point to, the box, b1, will also have an integer type inside it, okay? Okay. So here I filled this t with this integer. So here what do we call this? We call this type argument. It's the same terminology used for methods. In methods, what we expect is a parameter. And the value we give here is an argument. So, what we are giving here is an argument. And what we are expecting here is a parameter. Now, writing an integer here It is optional. Why? Because it will find its reference variables. If there is an integer here, then obviously the heap object should also be of integer type. Right? Basically, its value should be of integer type. So, it is optional. Okay. This is done. Now, think. As soon as I wrote this, my work became easy. Now, if I want, I can directly type system.out.println ln b1. getValue, so I will get the value. And I can do plus 5 in this, which I was not able to do directly before. See, I didn't get any error. Now if I run it, then my 15 will also come. Absolutely right, right? So basically, see here we have made a new box, in which we have treated this value as an integer. Okay. What about I make a new box, let's say b2. Here I write b2. And this time I treat it as a string, which is the value inside. Absolutely I can do it. Now I can make a new box like this. I just need to add a syntax and pass a string. Let's say hello. No error. Again, I make a new box and pass a boolean. I passed boolean in the type argument. Equal to new box. Let's leave it empty and give false in the value. New keyword will appear here. Now I can do the same thing I did earlier. If I want, I can Let's print both of them. b2. getValue plus 5. Now we need concatenation here. And the third one is b3. getValue. There is no use of plus in this. So let's remove it and print it directly. Now we should get the same values as expected. 15, hello5 and false. Absolutely correct. So this is Java's generics. What did we do again? We made a general purpose class box. If you want you can take integer, string or boolean. And we can work with them easily. We don't have to do any manual downcasting. And see, we won't get any error on runtime. We can never get class cast exception. Why can't we get it? See, in this case, in our demo 2.java, when class cast exception comes? It comes when we have put an integer in b1. But let's say, for now, I will comment all these. Assume everything is commented here. I have put an integer in b1. What if I try to take that b1 in a string? String as equal to b1 and here I cast it. Let's say string. And b1. getValue should be here So I cast b1. getValue in the string And I tried to print this string So I wrote printlns Now as soon as I wrote this code You know I should have class cast exception Why? Because you know that in B1, I have 10 and 10 is integer. Now integer class and here I am trying to type cast it in string. These two are different classes. So, I will get class cast exception on runtime. So, here I can get exception. Now see, here the line I have written, can I write it again like this? Basically, what if I comment all of them for now? So, I will write that I have made a new string here as well. S equal to let's say B1 dot get value. B1 dot get value. And I have type cast it in string. This is what I was doing earlier, right? I type cast in the string. If you see, I had written string as equal to string b1. getValue. And here I wrote the same. But here I am not able to do it. I will get a direct error. It cannot cast from integer to string. Even though you have written the cast keyword here. But how can you cast an integer in a string? This is neither upcasting nor downcasting. Because integer and string are of different classes. There is no parent-child relationship between them. So you cannot convert an integer into a string. Up-casting and down-casting only works if there is parent-child relation between two classes. If you convert a child into a parent, it is up-casting. And if you convert a parent into a child, it is down-casting. But integer and string are not related to each other. They are siblings. Because both are object classes. So you can't cast siblings in each other, it won't let you do it. So the error I got is at compile time, I didn't have to wait till run time. See, the error I got here was at run time, here it came at compile time. Because this b1 is not of object class, this b1 reference variable, the value in it is not of object class, I have replaced t with integer. So java won't let me do anything that rules don't apply on integer. For example, If I tell you that let's say we try to print this again. Here, if after this b1. getValue, if I assume that this? Absolutely not. Because the method of substring is not available in integer. So basically, I am not able to do anything due to which I got a runtime exception. Whatever error it is, I will know it at the compile time only. Because you know that my reference variable b1 here, the value it has is not of the object class, it is of the integer class. I have replaced this t with integer. In the case of b2, it is of string type and in the case of b3, it is of boolean type. Now, do you know what happened in this case? My type information is not lost. Now, every reference variable b1, b2, b3, all of them know their types. All of them know that b1 is integer, b2 is string, and b3 is boolean. None of them are of object type. So, all of them know what operations can be done on b1. Now, b1 knows that any operation on string, sorry, any operation on integer can be done on b1. Addition, subtraction, multiplication, division can be all arithmetic operations. Now in same B2, there can be those operations which can be in a string. If I call a string method dot substring on B2. getValue, then I can call it easily. There is no problem. Because B2 already knows that it is a string type object. And same goes for B3, it is a Boolean type object. So I have created an empty demo4. java file to see one more example. Let's make a class this time, a pair class. Now you know what a pair class is, first I make it a normal class and then we will generate it. In which I can store two integers, let's say int x and int y. I can store two integers Now I can store two integers Now let's say we don't make a constructor or getter setter I keep them public By default they are default So I can directly access them There is no problem So I keep them like this Now what does pair class mean Whenever I make its object Let's say Pair p Let's not make a constructor here Let's say pair Or int x comma int y Here we expect And instead of x or y Let's give it a better name Let's say first second First value second value Okay So now I can do this dot first is first and this dot second is second. This dot second is second. Okay, so I made a pair class. Now let me make an object of it. Let's say pair p1 equal to new pair. Now it can hold 2 pairs of integers. Let's say 2,3. It can easily hold them. Now I can print them as per my wish. So I can say system.out.println. Whenever I have to print these two pairs, I will do p2. first. Let's say I separated the two commas. And printed p1. second. Something like this. Now let's run it once. My output is 2.3. Very good. But again, this is working for integer. If I want a pair class for string, then I have to make it different for string and different for float. So I have to make it generic. So what will I do? First of all, let's change the arguments of these two. Now again, what can I do? I change the arguments of these two. I call this t and this t. And what will I expect here? I will expect t here and here. And you know, I will have to write t here. Something like this. Now what did I do? I said that I don't know what t is, but when I make an object, I will decide. But I know that the first and second type of both are t types. And when will it decide? When I make an object. So while making an object, I have to give here, let's say, integer. p1 equal to new pair. You know that I have to give here. And now again my output will be same. If I print it now, then my same will be again 2,3. But the special thing is, if I make it string type, Let's say string type. So I can store two strings in it. So let's say I have stored two pairs of strings. One I wrote name. And the other I wrote Aditya. Something like this. Now I will print it. So my name is now printed. Very good. Now look, I have made a generic class here, which can take string and int as well. But if I want to make it more generic, I mean, the type of first can be different, the type of second can be different. If this is integer and this is double, if this is integer and this is string, basically, they don't have any relation of types. So in that case, I will need two data types. As I have written t here, so let's do one thing, let the first be t, let the second be u. I took two, one t and one u. Now I have to tell that the name of the second one is u and I have to pass t comma u. So I have to expect two parameters here, t for the first one and u for the second one. Now when I make its object, I can pass two parameters here too. Let's say integer comma string. Equal to new pair. Now here I have to give integer in first. So let's say I gave 23. And gave string in second. Let's say again Aditya. So let's keep this Aditya. Okay. Now if I print them. So my first will be 23. And second will be Aditya. So see we made it more generic. We said there is no relation between first and second type. First can be anything. Second can be anything. Now if you understood, let's go and see how we can declare generic methods. Yes, so I have made an empty file by doing demo5. java. And I want to explain you about generic methods. Now see, as we made a generic class here. What was this? It was a generic class. We can also make generic methods that can work on any data type. And again, when we are calling them, then we tell them which data type you want to work on. So again, let's say I make a method in my demo 5 class. And that's a very simple method. It's a public method. Again, you know, I have to make it static here because I am not going to make its object of demo 5. And what does it do? It returns an integer value. And let's say its name is get. And again it expects integer value int x And returns me whatever is x by adding 5 I made a random method So how can I call this method? Let's do one thing system.out.println Let's print whatever comes on the screen And let's say I give a value int y equal to Let's call getResult and pass 5. I will call getResult by giving it the value 5. I know it will return 10 in y and I will print this y. I should get the output of 10. This method of getResult only works with integer. It adds 5 to integer. I want it to work with anyone. With string, with boolean. But again, if you think, how can I add 5 to boolean? So, I change this method a little bit. Let's do one thing. It returns what it takes. So, in this case, it took x and returned x. Now, my answer should be 5 to 5. Very good. Now, if I want to make it generic, then my first thought principle will be to make it work on object and return object type object, right? Something like this. So, I can give anything here. Now, I gave 5 in the get result. Okay, the type is coming out well because I am converting it into a straight integer. Here first, I will have to write capital integer and type cast it into integer. Something like this. I can also do this. And if I want, I can also give a string here. Let's say hello, I gave a string. Hello. Okay. And this time I typecast it in string. And here I took string instead of integer. Something like this. I can also do this. If I see now, then my y, our string will be hello print. There will be no problem. Now again this work will be done by me, but again the same problems will come in it, which was coming in my object class, that if I am passing string from let, but if I type cast it by mistake in integer, it will not stop me, but as soon as I will run, it will give me class cast exception, right? So if this is not wrong, then again I will have to use it better instead of object, so I again go to my t, that I say its type is t, right? Means its return type should also be t, right? It takes t and gives it to t only. Now there is something missing in its syntax. In its syntax, I have to tell one more thing. I have to put t in my angular bracket. Before that, before the data type of the class. And do you know what t is? The angular bracket t that I have made here, it means your type parameter. So it is telling that the method getResult takes a parameter of type t So I have already told it before, even before its return type And its return type is also t, means it takes only t and gives only t And returns x, again you know x is an object of type t So now I don't need to do any type casting here Let's make it string here Now I can do it directly like this I gave hello in getResult and took it in stringY My hello should be printed like this Now again I can pass an integer here and I can take it in integer. No problem, this should also come to me. And I can't do wrong casting. Let's say I am giving integer here but I try to collect it in string. I can't do this. Even if I try to cast it in string, I can't do this. You know sibling casting is not possible. So again all my runtime errors are over and I got an error on compiler if I write something wrong. Rather than runtime. So my problem is also solved. So again if I tell you what is a standard format to make a method generic So we write whatever method type is t Then comes its return type Then comes the name of your method So here comes the method name And then again it will take a value of t type x Something like this So let's call this x parameter So let's call this x parameter Something like this So this is your generic syntax of a method Which you make generic Now let's see how we can work with two parameters. Let's make another method public static obviously and name it printPair. One will take a T-type value. Let's say value 1. Or let's call it first. First. And one will take a U-type value. Let's say second. And it will print those two on the screen. So system.out Sorry, system.out.out println which is your first plus comma which is second Something like this. But again, you know that our syntax is incomplete. We have to write here before return type that it takes two type parameters t, u. Okay, so print pair t, u. Its return type is void. Now my syntax is absolutely correct. I took it and it works on two parameters. t, u. Here is that t, u. Return type is void. No problem. And the method's name is print pair. Now when I call this method, let's comment on it for now. You know how I can call it. I can call it directly. print pair and here I can give value of first and second so let's say instead of first I gave an integer let's say 11 and instead of second I gave a string hello so as soon as I run it my screen will come 11 comma hello no problem okay when we used to call this method then we didn't tell that this first and this second are of which type right it used to automatically deduce how with their values as soon as I called print pair I started the argument of methods As soon as I called print pair, what did I give inside? Arguments. My first argument, I gave it as an integer. Now the compiler saw that 11 is an integer, so here it matched that the first thing it was expecting, its t would be an integer. And when I called second as a string, then the compiler matched it that this u would be a string. So it automatically considered t as an integer and u as a string. If I had given an integer instead of this hello, let's say 23, then again, sorry. So again, it marks the first and second as an integer. This is called type inference in Java. So basically Java itself infer the type with its values. I hope you understood everything till now. Now let's talk about another concept in generics. Till now when we were making a class box, As a generic, we used to call it t And it used to take a t-type value Something like this And we used to say that this is our generic class Basically it can work on any data type So when I make its object, I will pass it here That on which box it has to work Let's say it has to work on string So box b1 equals to new box Something like this And if I want, I can pass my string in constructor It will start working on it, no problem So let's say I had passed aditya here So my b1. value should have been set under the value of aditya. I know this whole story. So basically what does this mean? This means that it can work on any type of class. But let's say I want to put some restriction on it. I want it to work on a class that deals with numbers. It can work with integer, double, float, etc. It can work with all types of numbers. It can work with string, boolean, etc. And it should not work with any user-defined data type like a student. It should only work with the number-oriented data types, the wrapper classes. Okay. You know in Java, all our number-oriented wrapper classes like integer, double, float, short, byte, all those numbers oriented, they all extend to our parent class whose name is number. If you want, we can go to the code and see it. Number is a class that extends all the number oriented data types. So I want this box class, it is very free now. It says that I can work with anyone. But I want it to work only with some numbers. Integer, double, float, or with all these. Float, byte, short, long, etc. This string, boolean, don't work with all these. Why would I want this? It's possible that I have a method written here which is number oriented. Let's say I'm performing some addition in someone. And I know that addition means proper addition. If I'm adding 2 in 5, I should get 7. Not that 5 or 2 get concatenated. Because if I work with string, it will get concatenated. It won't be supported in boolean. It won't be supported in student class. It will be supported in my number class. Or you can assume that I'm calling a method which is made for numbers only. Let's say double So double value is a method that you will get in every number data type. You will get it in integer, float, double, in everything. And what does double value return? It returns a double value of any data type. Basically, it converts it into double and returns it. So if you have, let's say, an integer x and you put 4 in it, and if you do x.double value, then what will it do to you? It will convert this 4 into double and return it. So you can call it a double value. as a reference variable, you can take it in D. So this double value is defined in all integer double float. I want to call this method. For them, I can't call on all of them. So what I can do is, I can bound my generics a little bit. So that it doesn't work on all of them, but on a limited class only, on a hierarchy only. So let's go to the code and see how we can do this. So here I have made a file called demo6. java. So what I told you is that let's say we make an integer x equal to 4. Now if I show you inside the integer class. Integer extends number. So our integer class extends number. Again if I make a double class, double d. And if I go inside this then it will also see you extending number. In fact if I show you let's say a Float class. Float also extends the number from inside. So basically all the classes that deal with numbers. They extend the number from inside. No problem. But let's say another class. Let's say string. Does string extend the number? Answer is no. It doesn't. Same goes for boolean. If we have a boolean class. Does it extend the number? Answer is no. It doesn't. So I want to work with all the classes. Which extend the number. Extends or inherits it. So let me first create a box class. And I would write t which is a generic class. And I would write t value here. Again I am not making a constructor or getter setter here. I will assume that I will change it in values. So what I would do is I would create its object. Let's say box. And let's say I want to work with integer. So I used to pass here b1 equal to new box And here I don't need to pass And I just close the bracket Now I can play with this value But I was saying I just have to deal with numbers And not with anyone else So let's say I have a function So let's say I name it public void print double So it will print double value for someone So what will it do? It will print double value for this value So I can print on screen System.out.println And I will write here whatever value I have, dot getDouble. First of all, can I call getDouble directly from here? The answer is no. Why can't I do it? Look, there will be an error. The method getDouble is undefined for the type T. Because look, what is this box classic? The value in it is filled with T. Now what is T? I will tell you T when I am creating the object. But till the time I have written this method, my object is not created yet. But till the time my object is not created, can I call any such method on that value? No, I can't. Java will say that I don't know the type of this value. Right? It will be decided when its object is created. So, till the time I don't know its type, how do I know that in whatever type it is, the method getDouble name will be defined or not? Right? So, right now, I can't call much method on it. I only know which method I can call. Which is in the object class. Because it is in sub. So, I can call two string in it. No problem. I can call a hash code in it, no problem. I can only call the method that is in the object class and can't call any other specific method. Right? So this is also a problem. So here we introduce bounds in generics. Bounds in generics. And first of all we are going to see upper bound. Okay. So what I meant by upper bound is that I want this box to work only with numbers. So the parent class is number and all the respective child classes like integer, float, double and all. Right? It should not work with anyone else. So for this box, the parent class was object. Right? I had written t here. So it was saying that until you tell me the exact datatype of this value, I will assume that its datatype is object. That's why I could only call the methods of object. If I write T extends number. Number. So now do you know what this means? I told it that this t, the datatype, which I will tell you when I make its object, whatever it is, it will either be a number or a subtype of a number. Like integer, float, double, etc. Nothing else can happen. Now I can call value.doubleValueMethod on it. Because now the compiler knows that the value, what is its type? Its type is either a number or any class below it. Nothing else can happen. Right? Now when I create an object, I can either pass an integer, float, or double pass. I can pass any subtype of a number or even a full number. But I can't pass a string here because there will be an error. What will the error say? Bound mismatch. The type string is not valid substitute for the bounded parameter. You have written t extends number. But string doesn't have any subtype. So I can't do this. Right? So my generics used to say that T can be anything. What did upper bound say to it? It said that T is at least number or its subtype. Either it can be a number or a subtype, it can be anything. So I can't write a string here. Let's make it an integer again, b. And let's call it b1. Let's set b1. value first. b1. value equal to 5. And now I say b1. print. Sorry, b1. printDouble. Something like this. Now my function should work. My 5.0 will be printed on the screen. Because I printed its double value. Very good. Okay, you know, I can add more conditions to this. You know, I just wrote here, T extends number. So, I just said here that this T is bounded that it extends the number class. But I can add more conditions here that it extends the number class and implements some interfaces. Okay, what does it mean? To explain that, I will make a new class. So, see, I have made a new class here to explain that. For that, I will make a class and show you. Let's say, I made an animal class first. Class animal. Now, let's leave it empty. Let's make another class. Let's say dog. Class dog. And call it extends animal. Okay. Let's make one more class, let's say fish and this also extends animal. Now see, there is a difference between fish and dog. Fish is an animal that lives in water and dog lives on the ground. So I am saying that the fish that lives in water, it doesn't have a special property of swimming. So imagine that I also have an interface and the name of this interface is Swimmable. And it says that whoever can swim will implement me. So suppose there is a method on it, let's say void swim. Something like this method would have been declared. Now suppose there will be a main class in the animal class. Let's call it void display. So every animal can be displayed. And it will have a parent representation. System.out.println Now we can do anything here. Let's say displaying animal. Now you know that the display method is extended by the dog and the fish. So they don't need to make their own display method. But they can make it if they want. But the swimmable interface, only the fish can implement it. So I can write here, let's say implements swimmable. The dog can't swim, so it won't do it. Now the fish will do the work of overriding the swimmable method. Sorry, the swim method. So let's say void swim. And here I can print anything. Something like this. Okay, so I have an animal class and a swimmable interface. Dog and fish extend the animal and fish implement the swimmable. We know that we can extend one class and implement multiple classes. Now imagine that I am making a generic class. So I am making a class called box and in this box, for now, let's say I accept t and anything can be held and I would have written t value here in this way. But now I am saying that there is a rule that in this generic class, in this box, I can only hold the same things that are animal or the animal is the subtype of animal. So for that, what keyboard do I have? I write extends animal. So now it can work with animals, dogs, and fish. Now I can add one more condition. And swimmable. It means a class that extends the animal and implements the swimmable interface. Now I can only work with fish. If I give you the proof, let's say I make an object of this box, box B1, and what do I have to pass here? I have to pass a type, so I passed the fish type, B1 equal to New box. So I can make something like this. I won't get any error here. But if I pass dog instead of fish, I will also get an error. Bound mismatch. If I pass animal, I will also get an error. Because animal itself doesn't implement. To whom? To swimmable. But if I remove swimmable condition, only extends animal, then I won't get an error. Now if I want, I can work with animal, with dog or with fish. So if I tell you a standard way of writing it, we write it like this. We write t extends let's say a class and and all the interfaces we have to implement and interface 1 comma interface to, because we know that a class can implement multiple interfaces, so t extends class, which extends the class and implements these interfaces. We don't need to write implement keyword, so we write it like this. So this is its standard way of writing, and we can put any boundary here. I hope you understood what generics are in Java. Just remember this rule that the class name will come first and then the interface name. You can't write the name of the first interface. Chalo, I hope aapko maza aaya, aapko bohot kuch neya seekhne ko mila hoga, agar aapko generics ka koi idea nahi hai. Is video me itna hi, next video me hum generics ke aur depth me jayenge, hum wildcards ke baare me samjhenge, jo ki bohot jaroori hai generics me. Ab aapse milta hai next video me, tab tak ke liye, thank you so much.