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

Java OOP: Casting & Generics Explained

This document covers advanced concepts in Java, specifically focusing on casting and generic programming. It explains type casting between primitive and referenced data types, including widening and narrowing casting, as well as the use of generics to create classes and methods that handle multiple data types without explicit casting. The document also discusses the implementation of generics with arrays and how to utilize Java's generics framework effectively.

Uploaded by

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

Java OOP: Casting & Generics Explained

This document covers advanced concepts in Java, specifically focusing on casting and generic programming. It explains type casting between primitive and referenced data types, including widening and narrowing casting, as well as the use of generics to create classes and methods that handle multiple data types without explicit casting. The document also discusses the implementation of generics with arrays and how to utilize Java's generics framework effectively.

Uploaded by

natolef206
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOP Advanced Java

Lecture 03

Samiullah Noori
Casting and Generic
Programming
Casting
 type casting is used ti convert an object or variable of one type into
another.
 assigning a value of one type to a variable of another type is known as
type casting.

 Syntax
data type variableName = (dataType) variableToConvert;

 types of data types


Primitive or Fundamental Data Types
Referenced or Advanced Data Type
Casting Primitive Data Type

 Casting Primitive Data Type

 Widening – converting lower data type into higher data type is called
Widening.
 Narrowing – converting higher data type into lower data type is
called Narrowing.
Casting Primitive Data Type

 Widening Casting Example


Char ch = ‘A’ ;
int num = ( int ) ch ;
int x = 9500 ;
float sal = ( float ) x ;

 Narrowing Casting Example


int x = 66;
char ch = ( char ) x;
double d = 12.6879;
int n = ( int ) d;
Casting Referenced Data Types
 Casting Referenced Data Types
A class is referenced data type. Converting a class type into
another class type is also possible through casting. But the classes
should have the same relationship between them by the way of
inheritance.
Casting Referenced data Type
Class One{
void show1(){
[Link] (“I am class one“);
}
}
class Two extends One{
void show1(){
[Link](“I am class Two”);
}
}
Casting Referenced data Type

Class Test{
public static void main(String args[])
{
One o;
o = (One) new Two();
o.show1();
}
}
Java Generic Programming
Generic
 Generics are techniques, that allows us to define methods and
classes that work with a variety of data types without the need for
explicit casting.

 Java includes support for writing generic classes and methods that
can operate on a variety of data types while often avoiding the need
for explicit casts.

 The generics framework allows us to define a class in terms of a set


of formal type parameters, which can then be used as the declared
type for variables, parameters, and return values within the class
definition. Those formal type parameters are later specified when
using the generic class as a type elsewhere in a program.
Generic
 The goal of generic programming is to be able to write a single class
that can represent all such pairs.
 generic programming was implemented by relying heavily on Java’s
Object class, which is the universal supertype of all objects.
Using Java’s Generics Framework

 With Java’s generics framework, we can implement a pair class


using formal type parameters to represent the two relevant types in
our composition.
Generics and Arrays
Generic and Array
 Fortunately, it allows an array defined with a parameterized type to
be initialized with a newly created, nonparametric array, which can
then be cast to the parameterized type.

 Class uses <T> as a parameterized type


 A common approach is to instantiate an array of type Object[ ]
Generic and Array
 For example, we show below a nonparametric GenericDemo class
with a parameterized static method that can reverse an array
containing elements of any object type.
End of Chapter 3

You might also like