Object oriented programming concepts-Session 10
Object oriented programming concepts-Session 10
nl
O
se
U
tre
en
Session: 10 C
h
ec
Generics
pt
rA
Fo
O
se
List ways of implementing Generics
U
Explain Template Function
tre
en
Explain Template Class
C
h
List advantages and disadvantages of Generics
ec
pt
rA
Fo
se
U
tre
The type of the value of a generic function or variable
en
is decided later based on value supplied by user.
C
h
ec
pt
O
se
U
tre
en
C
h
ec
pt
rA
Fo
O
parameters or even the return type.
se
The type for the parameters is decided based on the
U
value passed by the user at runtime.
tre
Syntax
en
<access-modifier> <T> <method-name>
(<T> <parameter-name>,..) C
h
{
ec
// processing statements
pt
}
Fo
O
se
U
The generic type introduces a type variable named ‘T’
tre
which can be used anywhere inside the class.
en
Syntax
C
<access-modifier> class <class-name> <T>
h
ec
{
<data-members>
pt
rA
<methods>
Fo
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 10 6
y
nl
Generics allow a programmer to create classes with
O
flexibility in applying the data type.
se
U
The data type is decided when the object is created
tre
and not when the class is created.
en
Generics allow overloading of template functions with
parameters of unspecified types. C
h
ec
types.
rA
Fo
O
typed Collections such as List, Stack, and Array
se
U
The compiler determines the type associated with the
tre
template that can perform all the required functions
en
C
h
ec
pt
rA
Fo
O
se
Some compilers lack in clear description when they
U
detect a template error.
tre
The compiler usually generates additional code for
en
each template type.
C
h
Nested templates are not supported by all compilers.
ec
pt
O
tools for applying them to specific situations by making
se
relevant changes.
U
tre
Generics is also known as templates in C++.
en
A template function is a method that does not have a
C
specific type attached to the parameters or even the
h
ec
return type.
pt
rA
Fo
O
can be used as a type with the data members and
se
methods of a class.
U
tre
Generics allow overloading of template functions with
en
parameters of unspecified types.
C
The language C++ uses the keyword ‘template’ to
h
ec