Please enable JavaScript.
Coggle requires JavaScript to display documents.
Chapter 01. ADVANCED CLASS DESIGN (Creating Nested Classes (Member Inner…
Chapter 01. ADVANCED CLASS DESIGN
Basic Concepts
instanceof
a instanceof B = true
a is an instance of class B
a is an instance of a subclass of B (directly or indirectly)
a is an instance of a class that implements the B interface
Virtual Method Invocation
@
Override
Implement
a method from an
interface
Override
a
superclass
method
Override
a method declared in
Object
such as
hashCode
,
equals
, or
toString
Coding equals, hashCode, and toString
toString
automatically call
toString
() method if you try to print out an object
Easy way (Apache Commons Lang)
@
Override public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@
Override public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
hashCode
Used when storing the object as a key in a map
Contract
Within the
same program
, the result of
hashCode()
must
not change
.
If
equals()
returns true
when called with 2 objects, calling
hashCode()
on each of those objects
must return the same result
.
If
equals()
returns false
when called with 2 objects, calling
hashCode()
on each of those objects
does not have to return a different result
.
Easy way
Multiply by a prime number when combining multiple fields in the hash code
equals
Check if 2 objects are
equivalent
uses the
equals()
Contracts
Reflexive
(x not null) x.equals(x) = true;
Symmetric
(x,y not null) x.equals(y) <=> y.equals(x)
Transitive
(x,y,z not null) x.equals(y) = true and y.equals(z) = true => x.equals(z) = true
Consistent
(x not null) x.equals(null) = false;
Easy way (Apache Commons Lang)
public boolean equals(Object obj){
return EqualsBuilder.reflectionEquals(this, obj);
}
Working with Enums
An enum type is NOT an
int
.
Add Constructors, Fields, and Methods
The
first time
that we ask for any of the enum values, Java constructs all of the enum value.
Can define an
abstract method
or a
default method
Creating Nested Classes
Member Inner Classes
Properties
Can be
extend any class
and
implement interfaces
Can be
abstract
or
final
CanNOT declared
static
fields or methods
Can access members of the outer class including
private
member
Can be declared
public
,
private
, or
protected
or use
default
access
Defined at the member level of a class
Local Inner Classes
Defined within a method
Properties
Not have an access specifier
CanNOT be declared static
CanNOT declare static fields and methods
Have access to all fields and methods of the enclosing class
NOT have access to local variables of a method unless those variables are
final
or
effective final
Anonymous Inner Classes
local inner class that does not have a name
Required to extend an existing class or implement an existing interface
Static Nested Classes
static class defined at the member level
Properties
Create a
namespace
because the enclosing class name must be used to refer to it
Can be made
private
or use one of the
other access modifier
to encapsulate it
Enclosing class can refer to the fields and methods of the static nested class