D84838GC10 Les05
D84838GC10 Les05
D84838GC10 Les05
e to
cens
l i
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Objectives
following:
• Design general-purpose base classes by using abstract
classes
• Construct abstract Java classes and subclasses
• Apply the final keyword in Java
e to
• Distinguish between top-level and nested classes
cens
l i
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Java SE 8 Programming 5 - 2
Modeling Business Problems with Classes
Java SE 8 Programming 5 - 3
Enabling Generalization
Java SE 8 Programming 5 - 4
Identifying the Need for Abstract Classes
Java SE 8 Programming 5 - 5
Defining Abstract Classes
Java SE 8 Programming 5 - 6
Defining Abstract Methods
method-level modifier.
Java SE 8 Programming 5 - 7
Validating Abstract Classes
Java SE 8 Programming 5 - 8
Final Methods
A method can be declared final. Final methods may not be
overridden.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2019, Oracle and/or its affiliatesฺ
Java SE 8 Programming 5 - 9
Final Classes
extended.
public final class FinalParentClass { }
// compile-time error
public class ChildClass extends FinalParentClass { }
e to
cens
l i
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Java SE 8 Programming 5 - 10
Final Variables
initialized.
Final variables can be:
• Class fields
— Final fields with compile-time constant expressions are constant
variables.
to
— Static can be combined with final to create an always-available, nse
never-changing variable. lice le
• Method parameters r a b
sf e
• Local variables tr a n
n -
no ฺ the same object,
Note: Final references must always reference
a
but the contents of that object may
h a s modified.
be i d e
) u
ฺ c om ent G
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
g
Benefitsdand Drawbacks of Final Variables
z E
i l u s
Bug Prevention
V Final variables can never have their values modified after they are initialized. This behavior
functions as a bug-prevention mechanism.
Thread Safety
The immutable nature of final variables eliminates any of the concerns that come with
concurrent access by multiple threads.
Final Reference to Objects
A final object reference only prevents a reference from pointing to another object. If you are
designing immutable objects, you must prevent the object's fields from being modified. Final
references also prevent you from assigning a value of null to the reference. Maintaining an
object's references prevents that object from being available for garbage collection.
Java SE 8 Programming 5 - 11
Declaring Final Variables
public VariableExampleClass() {
field = 100;
}
e to
ns
public void changeValues(final int param) {
param = 1; // compile-time error
le lice
final int localVar;
r a b
localVar = 42;
n s fe
n - tra
localVar = 43; // compile-time error
} o
}
s a n eฺ
) ha Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
g
Ed
Final Fields
z
i l u s
Initializing
V Final fields (instance variables) must be either of the following:
• Assigned a value when declared
• Assigned a value in every constructor
Static and Final Combined
A field that is both static and final is considered a constant. By convention, constant fields use
identifiers consisting of only uppercase letters and underscores.
Java SE 8 Programming 5 - 12
Nested Classes
/*Setters ad Getters*/
Inner class,
EMICalculatorHelper
private class EMICalculatorHelper {
int loanTerm = 60;
double interestRate = 0.9;
e to
double interestpermonth=interestRate/loanTerm;
cens
l i
ble
protected double calcMonthlyPayment(double loanAmount)
f e ra
{
an s
double EMI= (loanAmount * interestpermonth) / ((1.0) - ((1.0) /
- t r
Math.pow(1.0 + interestpermonth, loanTerm)));
n on
return(Math.round(EMI));
s a eฺ
}
) a
h Guid
}
ฺ c om ent
}
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
The example in the slide demonstrates an inner class, EMICalculatorHelper, which is
E
i l u sz defined in the BankEMICalculator class.
V
Java SE 8 Programming 5 - 14
Enumerations
r ( vil u
dgá Range Checking
Compile-Time
E
i l u szthe example in the slide, the compiler performs a compile-time check to ensure that only
In
V valid PowerState instances are passed to the setState method. No range checking
overhead is incurred at runtime.
Java SE 8 Programming 5 - 15
Enum Usage
Java SE 8 Programming 5 - 16
Complex Enums
Java SE 8 Programming 5 - 17
Complex Enums
ble
comp.getState().getDescription());
} e r a f
a n s
}
-t r
• Output n on
s a eฺ
Current state: SUSPEND
) a
h Guid
Description: The power usage
ฺ c om is e ntlow
ud Stu d
c l o
i © 2014,hOracle
z @
Copyright t is and/or its affiliates. All rights reserved.
v i l us use
á r (
dg
Enum Usage
E
i l u sz sent to be printed, the default behavior for an enumeration is to print the current value.
When
V Typically, an additional call to the enumation's methods is necessary to get information stored
in the enumeration.
Java SE 8 Programming 5 - 18
Summary
Java SE 8 Programming 5 - 19
Practice 5-1 Overview:
Applying the Abstract Keyword
This practice covers the following topics:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2019, Oracle and/or its affiliatesฺ
e to
cens
l i
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Java SE 8 Programming 5 - 20
Practice 5-2 Overview:
Using Inner Class As a Helper Class
This practice covers using an inner class as a helper class to
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2019, Oracle and/or its affiliatesฺ
e to
cens
l i
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Java SE 8 Programming 5 - 21
Practice 5-3 Overview:
Using Java Enumerations
This practice covers taking an existing application and
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2019, Oracle and/or its affiliatesฺ
e to
cens
l i
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Java SE 8 Programming 5 - 22
Quiz
to compile successfully?
a. A return value
b. A method implementation
c. Method parameters
d. private access to
ns e
l i ce
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Java SE 8 Programming 5 - 23
Quiz
a. Anonymous
b. Local
c. Static
d. Member
e to
cens
l i
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Java SE 8 Programming 5 - 24
Quiz
e to
cens
l i
b le
f e ra
an s
- t r
n on
s a eฺ
) a
h Guid
ฺ c om ent
l o ud Stud
@ ic© 2014,hOracle
is and/or its affiliates. All rights reserved.
z
Copyright t
v i l us use
á r (
d g
E
i l u sz
V
Java SE 8 Programming 5 - 25
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2019, Oracle and/or its affiliatesฺ
Vi l u sz
Ed g á r
(vil
u s @
u
l o
z se t
ฺ
ic his
c o m )
ud Stud
h
e
a
n
s a
tG
u i d
n
e
-
no ฺ
n tra
s f e ra
b
le l i ce
ns e to