From the course: Java: Generic Classes
Generic classes/interface use case - Java Tutorial
From the course: Java: Generic Classes
Generic classes/interface use case
- [Instructor] In this module, we are going to build our own generic class. For this, let us understand the most common use case that is implemented with generic classes. Let's say you have a box. This box could contain anything. It could be a box of fruits or books or chocolates. Similarly, if you have a bottle, it could hold water, milk or juice. So let's say that you have to implement this scenario. You have an object that has to be passed throughout your application. Let's say, for example, the book object. And this object should be encapsulated within a container, for example, the box. Now, why do we need a container? That is because it'll help us do all the operations on the objects that it contains. So let's say the container should be able to store an object, retrieve an object and perform any other tasks that your application desires. Furthermore, there is another requirement. This container should have the capability of holding objects of multiple types. So at one point of time in your application, the box could contain books. And it could be a box of chocolates at another point in time. So how do we implement this? Well, we have the object type approach implementation that we have talked about before. You can design a container and write code such that your container deals with object types only. But then we know the limitations that come with this approach. We've discussed this before. First is, there is no type safety. You can end up adding any wrong thing into that box. Secondly, whenever you try to retrieve an object from that container, you will have to perform an explicit casting to process it further. And even if you do all of this right, we definitely know that there is a possibility that you can encounter problems at runtime. So this kind of use case is implemented with generics. So what we can do is, we write a class called box of T. We take T as the type parameter name. That is because T can refer to any type. This box class will have all the methods to operate on that T. For example, storing the object, retrieving the object, et cetera. When we invoke box of T, we can pass either fruit, or book, or chocolate as the type argument so that we can create different kinds of boxes in the application. So at one time it could be box of fruits, the other time box of books. Similarly, we can write a class bottle of T and water, milk and juice will become the type arguments when we invoke the generic class bottle. This slide shows you an overview of everything that we need to do in order to get this demo up and running. So the first thing we will do is write the class box of T. Remember this box will hold multiple items of a specific type. Next, we will write methods in this box class so we can have a method to add an item to the box. We can also have a method get latest item which fetches you the latest item from the box. And yet another method to get the count of items from the box. After this, we will write the types which will behave as type arguments to work box of T. So we'll write a class fruit and another class book. And these two classes will have all the desired properties and their respective methods. Once we understand the setup and we write the code for it, the next step will be to test it out. In order to test it, we will invoke box of T, and we'll create two kinds of boxes. Box of fruit, box of book. So fruit and book will become the type arguments for the invocation in our test class.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Generic classes/interface use case3m 48s
-
(Locked)
Create a generic class demo8m 17s
-
(Locked)
Understand generic methods and constructors8m 36s
-
(Locked)
Implement a generic method as a utility4m 38s
-
(Locked)
Using bounded types14m 6s
-
(Locked)
Challenge: Write code using generic classes/bounded types1m 38s
-
(Locked)
Solution: Write code using generic classes/bounded types8m 10s
-
-
-