Docsity

Java Generics: Bounded/Unbounded Wildcards, Raw Types, Ensure & Restrictions, Slides of Java Programming

Toggle sidebar
bg1
B
ou
n
ded
๎˜ƒ
Ge
n
e
ri
c
๎˜ƒT
ype
p
ublic๎˜ƒstatic๎˜ƒvoid๎˜ƒmain
(
Strin
g[]
๎˜ƒar
g
s๎˜ƒ
)
๎˜ƒ
{
ou ded Ge e c ype
p(g[]g ) {
Rectangle๎˜ƒrectangle๎˜ƒ=๎˜ƒnew๎˜ƒRectangle(2,๎˜ƒ2);
Circle9๎˜ƒcircle๎˜ƒ=๎˜ƒnew๎˜ƒCircle9(2);
System out println(
"
Same area?
"
+equalArea(rectangle circle));
System
.
out
.
println( Same
๎˜ƒ
area?
๎˜ƒ ๎˜ƒ
+
๎˜ƒ
equalArea(rectangle
,๎˜ƒ
circle));
}
bli tti EtdGtiObjtbl
pu
bli
c๎˜ƒs
t
a
ti
c๎˜ƒ<
E
๎˜ƒex
t
en
d
s๎˜ƒ
G
eome
t
r
i
c
Obj
ec
t
>๎˜ƒ
b
oo
l
ean๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ
equalArea(E๎˜ƒobject1,๎˜ƒE๎˜ƒobject2)๎˜ƒ{
return๎˜ƒobject1.getArea()๎˜ƒ==๎˜ƒobject2.getArea();
}
15
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Java Generics: Bounded/Unbounded Wildcards, Raw Types, Ensure & Restrictions and more Slides Java Programming in PDF only on Docsity!

Bounded

Generic

Type

public^ static

void^ main(String[]

args^ )^ {

ou ded Ge e c

ype

p^

(^ g[]^ g

) { Rectangle^ rectangle

=^ new^ Rectangle(2,

2);

Circle9^ circle

=^ new^ Circle9(2); System out println("Same area? " + equalArea(rectangle

circle));

System.out.println( Same

area?^ +^ equalArea(rectangle

,^ circle));

} bli^ t ti^

E^ t^ d^ G^

t i Obj^ t^ b

l

public^ static

boolean equalArea(E^ object1,

E^ object2)^ { return^ object1.getArea()

==^ object2.getArea(); }

Raw Type and backward Compatibility Raw^ Type // raw type

and^ backward

Compatibility

//^ raw^ type ArrayList^ list

=^ new^ ArrayList();

This^ is^ roughly

equivalent^

to ArrayList

list^ =^ new

ArrayList();

Make^ it^ Sa

fe

//^ Max1.java:

Find^ a^ maximum

object

a e t Sa epublic class Max1 { public^ class

Max1^ { /**^ Return

the^ maximum

between^ two

objects^ */

public^ static

>

E

p^

p max(E^ o1,^

E^ o2)^ { if^ (o1.compareTo(o2)

>^ 0) return^ o1;elsereturn o2;return^ o2; } } Max.max("Welcome", 23);

18

}

Wildcards Why^ wildcards

are^ necessary?

See^ this^ example.

Wildcards^ WildCardDemo1WildCardDemo1?

unbounded

wildcard

?^ t^ d^

T^ b

d d^

ild^ d

?^ extends

T^ b

ounded^ wildcard

?^ super^ T^

lower^ bound

wildcard

WildCardDemo2WildCardDemo

WildCardDemo3WildCardDemo

Avoiding

Unsafe^ Raw

Types

o d^ Use

g U sa e

a^ ypes

new^ ArrayList() Instead^ of new^ ArrayList();

TestArrayListNewTestArrayListNew

RunRun

Ensure and Restrictions on Generics Generics^ are

implemented

using^ an^ approach

called

Ensure^ and

Restrictions

on^ Generics

type^ erasure

.^ The^ compiler

uses^ the^ generic

type

information

to^ compile

the^ code,

but^ erases

it

ft^ d^

S^ th^

i^ i f^

ti^ i^ t

il bl

afterwards.

So^ the^ generic

information

is^ not^ available

at^ run^ time.

This^ approach

enables^ the

generic^ code

to

be backward

compatible with the legacy code that

be^ backward

โ€compatible

with^ the^ legacy

code^ that

uses^ raw^ types.

22 docsity.com

Important

Facts

It^ is^ important

to^ note^ that

a^ generic

class^ is

h^ d b^

ll it^ i^ t^

dl^ f it

t^ l

po ta t^

acts

shared^ by

all^ its^ instances

regardless

of^ its^ actual

generic^ type.^ GenericStack

stack1^ =^ new

GenericStack();

GenericStack

stack2^ =^ new

GenericStack();GenericStack();

Although^ GenericStack and

GenericStack b^ h^ i l^

l^ G^ i S

k

are^ two^ types,

but^ there^ i

s^ only^ one^

class^ GenericStack

loaded^ into

the^ JVM.

Retractions

on^ Generics

ย€^ Restriction

1:^ Cannot^

Create^ an^ Instance

of^ a^ Generic

Type (i e^

new E())

et act o s o

Ge e cs

Type.^ (i.e.,^

new^ E()). ย€^ Restriction 2: Generic Array Creation is Not Allowedย€^ Restriction

2:^ Generic

Array^ Creation

is^ Not^ Allowed

(i.e.,^ new^ E[100]). ย€ Restriction

3:^ A^ Generic

Type^ Parameter

of^ a^ Class^ Is

Not^ Allowed

in^ a^ Static^

Context. ย€^ Restriction

4:^ Exception

Classes^ Cannot

be^ Generic.^25

UML^ DiagramU^ ag a^ GenericMatrix^ # add(element1:E, element2: E): E^ # multiply(element1: E, element2: E): E

IntegerMatrix

multiply(element1:

E, element2: E): E

zero(): E +addMatrix(matrix1: E[][], matrix2: E[][]): E[][]+multiplyMatrix(matrix1: E[][], matrix2: E[][]): E[][]+printResult(m1: Number[][], m2: Number[][],printResult(m1: Number[][], m2: Number[][],m3: Number[][], op: char): void

RationalMatrix^27

Generic^

Type ย€ Objective:^ This^ example

gives^ two^ programs

that

yp utilize the^ GenericMatrix

class^ for^ integer

matrix

arithmetic^

and^ rational

matrix^ arithmetic. TestIntegerMatrixTestIntegerMatrix^

RunRun

IntegerMatrixIntegerMatrix

TestRationalMatrixTestRationalMatrix

RunRun

RationalMatrixRationalMatrix