Day17 Generics
Day17 Generics
A 0
B true
C -3
D false
A 0
B true
C -3
D false
Q2.Which of the following classes, you can create objects without using new
operator?
A StringBuilder
B String
C StringBuffer
D Object
Q2.which of the following classes, you can create objects without using new
operator?
A StringBuilder
B String
C StringBuffer
D Object
Syntax:
● A class that is declared generic type is called generic class in Java. It is type-safe and
can act upon any data type.
● A generic class is also called “parameterized types” because it uses a parameter that
determines which data type it should work upon.
● Java Generic class is designed to work upon objects. Hence, it cannot work with
primitive data types.
● Generic class works on any kind of data type, we cannot specify a specific data type
such as Integer, String, etc.
class Myclass<T>
{
// class code
}
● Errors are integral part of coding. Some errors occur at compile time and some errors
occur at run time.
● Errors which occur at compile time can be easily identified and can be removed. But,
run time errors occur when an application is running in real time. If they happen, they
cause abrupt termination of an application.
● ClassCastException is also such an exception which happens only at run time.
● It occurs when data of one type cannot be casted to another type.
● You will never get a single clue about this exception during compilation.
● A method that takes generic type parameters is called generic method in Java.
● A generic type can be applied for the static method. We can define a generic method
by putting generic type parameter <T> before the method return type and immediately
after the keyword static.
● The general syntax to declare a generic method in Java is as follows:
public static <T> void display(T[ ] list)
{
// method code;
}
● You should explicitly specify type parameters before the actual name of the return
type of the method. The type parameter is delimited by angle brackets.
Example: <T>
● You should also include the type parameters in the declaration of the program and
separate them with commas.
● A type parameter specifies a generic type name in the method.
● All the type parameters are only useful when they have to declare reference types.
Remember that the type parameters cannot denote primitive data types such as
int, float, String etc.
● Generics - single piece of code can manipulate many different data types without
explicitly having a separate entity for each data type.
● Generics - It is declared in the “<>” brackets
● Generic class - It is designed to work upon objects. Hence, it cannot work with primitive
data types.
● Generic Method- A method that takes generic type parameters.
● Unbounded WildCard-An unbounded wildcard is represented by <?>.
● Lower Wildcard - It use the super keyword.
Objective:
Create a generic inventory system that can manage different types of products using
Java generics.
Requirements:
Product Class:
Create a Product class with attributes like id (String), name (String), and price
(double).
TNS India Foundation | ‹#›
Partners in Economic Transformation
Inventory Class:
● Implement a generic Inventory class that can manage various types of products.
● The inventory should use a data structure to store products (e.g., ArrayList).
Display Inventory:
● Create a test program to demonstrate the functionality of the generic inventory system.
● Add products of different types (e.g., electronics, clothing) and display the inventory.