0% found this document useful (0 votes)
4 views

Ch 5 - Generic Programming (4)

The document provides an overview of Generics in Java, defining it as parameterized types that allow methods, classes, and interfaces to operate on different data types. It explains the two main types of Java Generics: Generic Methods and Generic Classes, detailing their functionalities and providing code examples. The document emphasizes that generics work only with reference types and cannot be used with primitive data types.

Uploaded by

shaimaaabudayyeh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Ch 5 - Generic Programming (4)

The document provides an overview of Generics in Java, defining it as parameterized types that allow methods, classes, and interfaces to operate on different data types. It explains the two main types of Java Generics: Generic Methods and Generic Classes, detailing their functionalities and providing code examples. The document emphasizes that generics work only with reference types and cannot be used with primitive data types.

Uploaded by

shaimaaabudayyeh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Faculty of Information Technology - Computer Science Department 1

Data Structures

Faculty of Information Technology - Computer Science Department 2


Chapter 5

Generic Programming

Faculty of Information Technology - Computer Science Department 3


Outline

 Generics in Java
 What is Generics?
 Types of Java Generics
 Generic Method
 Generic Class

Faculty of Information Technology - Computer Science Department 4


Generics in Java

What is Generics?
 Generics means parameterized types.
 The idea is to allow type (Integer, String, … etc., and user-defined types) to be
a parameter to methods, classes, and interfaces.
 Using Generics, it is possible to create classes that work with different data
types.
 An entity such as a class, interface, or method that operates on a
parameterized type is a generic entity.

Faculty of Information Technology - Computer Science Department 5


Generics in Java

Types of Java Generics


 Generic Method:
 Generic Java method takes a parameter and returns some value after
performing a task.
 It is exactly like a normal method, however, a generic method has type
parameters.
 This allows the generic method to be used in a more general way.

Faculty of Information Technology - Computer Science Department 6


Generics in Java

Types of Java Generics


 Generic Classes:
 A generic class is implemented exactly like a non-generic class.
 The only difference is that it contains a type parameter section.
 There can be more than one type of parameter, separated by a comma.
 The classes that accept one or more parameters are known as parameterized
classes or parameterized types.

Faculty of Information Technology - Computer Science Department 7


Generics in Java - Generic Classes
// Java program to show the working of user-defined Generic classes
// Symbols < > are used to specify the Parameter type Generics Work Only with
class Test<T> { Reference Types:
T obj; // An object of type T is declared compile-time error • When an instance of a
generic type is declared,
Test(T obj) { this.obj = obj; } // constructor the type argument passed
to the type parameter
must be a reference
public T getObject() { return this.obj; }
type.
} • Primitive data types
// Main class to test Test Class like int, char can’t be
class Main { used.
public static void main(String[] args) Test<int> obj =
{ new Test<int>(20);
//compile-time
Test<Integer> iObj = new Test<Integer>(15); // instance of Integer type
error
System.out.println(iObj.getObject());

Test<String> sObj = new Test<String>("String Object"); // instance of


Outputs:
String type
System.out.println(sObj.getObject()); 15
}
String
}
Object
Faculty of Information Technology - Computer Science Department 8
Generics in Java - Generic Classes
// Java program to show multiple type parameters in Java
Generics
class Test<T, U>
{
T obj1; // An object of type T // Main class to test Test Class
U obj2; // An object of type U class Main
{
// constructor public static void main (String[] args)
Test(T obj1, U obj2) {
{ Test <String, Integer> obj =
this.obj1 = obj1; new Test<String, Integer>(“ABC",
this.obj2 = obj2; 15);
}
obj.print();
// To print objects of T and U }
public void print() }
{ Outputs:
System.out.println(obj1);
System.out.println(obj2); ABC
} 15
}

Faculty of Information Technology - Computer Science Department 9


Generics in Java - Generic Methods
Generic Methods:
 Generic methods can be written to be called with different types of arguments based on the
type of arguments passed to the generic method.
// Java program to show the working of user-defined Generic methods
class Test {
// A Generic method example
static <T> void genericDisplay(T element)
{
System.out.println(element);
}
// Main method
public static void main(String[] args)
{
/* Calling generic method with Integer argument*/
genericDisplay(11); Outputs:
11
/* Calling generic method with String
argument*/ genericDisplay(“ABC"); ABC

/* Calling generic method with double 1.0


argument*/ genericDisplay(1.0);
Faculty of Information Technology - Computer Science Department 10
Generics in Java
 Generic types differ based on their type arguments:
// Java program to show the working of user-defined Generic
classes
class Test<T> {
T obj; // An object of type T is declared
Test(T obj) { this.obj = obj; } // constructor
public T getObject() { return this.obj; }
}
// Main class to test Test Class
class Main {
public static void main(String[] args)
{
// instance of Integer type
Test<Integer> iObj = new Test<Integer>(15);
System.out.println(iObj.getObject());
// instance of String type
Test<String> sObj
= new Test<String>("ABC"); Outputs:
System.out.println(sObj.getObject());
iObj = sObj; // This results an error error: incompatible types:
} Test cannot be converted to
}
Faculty of Information Technology - Computer Science Department Test 11

You might also like