0% found this document useful (0 votes)
9 views12 pages

Generics in Java

Generics in Java allows for the creation of generic classes and methods, where the type of data is specified as a parameter. A generic class defines a type parameter in angle brackets after the class name. The type parameter can then be used like other types within the class definition. When declaring an instance of a generic class, the type argument passed to the type parameter must be a class type, not a primitive type. Generic methods also define type parameters and can be called using any parameter type.

Uploaded by

SaHiL SiNgH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Generics in Java

Generics in Java allows for the creation of generic classes and methods, where the type of data is specified as a parameter. A generic class defines a type parameter in angle brackets after the class name. The type parameter can then be used like other types within the class definition. When declaring an instance of a generic class, the type argument passed to the type parameter must be a class type, not a primitive type. Generic methods also define type parameters and can be called using any parameter type.

Uploaded by

SaHiL SiNgH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Generics in Java

Generics
 Generics means parameterized types
 It enables you to create classes and methods in which the
type of data upon which they operate is specified as a
parameter.
 Generic Classes
 Generic Methods
Generic Class
 A class that is defined with a parameter for a type is
called a generic class or a parameterized class
 The type parameter is included in angular brackets after
the class name in the class definition heading.
 Any non-keyword identifier can be used for the type
parameter, but by convention, the parameter starts with
an uppercase letter.
 The type parameter can be used like other types used
in the definition of a class.
Generic Class with one Type Parameter
Generic Type with Array Variable

Part 1
Generic Type with Array Variable

Part 2
Generic Type with Array Variable

Part 3
Generics works only with Objects!!
 When declaring an instance of a generic type, the type
argument passed to the type parameter must be class
type.
 You cannot use a primitive type, such as int or char…
Sample<int> ob1=new Sample<int>();

Sample<float> ob2=new Sample<float>(); ERROR


Sample<byte> ob3=new Sample<byte>();
Creating a Generic Method
Syntax:
<type-param-list> ret-type methname(paramlist)
{}
Example:
<T> void printArray(T [ ] y)
{ }
– it can be called by any parameter
– behavior of the method will be according
to the parameter passed
Generic Method
Generic Method
Thank You

You might also like