Ccna Security Ch3 Aaa
Ccna Security Ch3 Aaa
Ccna Security Ch3 Aaa
• Classes
• Methods
Argument and return value
Overloading
• Object Creation and Destruction
• Equality
Methods
method
Parameter list
name
The parameter list specifies the type
return and name of each parameter
type
The name of a parameter in the method
declaration is called a formal argument
• Instantiating
Static: Objects are constructed and destructed at the same time as the
surrounding object.
Dynamic: Objects are created by executing a specific command.
• Copying
Often called cloning
• Explicit
An object in use can be destructed.
Not handling destruction can cause memory leaks.
• Implicit
Objects are destructed automatically by a garbage collector.
There is a performance overhead in starting the garbage collector.
There is a scheduling problem in when to start the garbage collector.
• Characteristics of Strings
The notation "fly" instantiates the class String and initialize it with the
values "f"', "l", and "y".
The class String has many different constructors.
Values in a string cannot be modified (use StringBuffer instead).
Class String redefines the method equals() from class Object.
• Reference Equality
Returns whether a and b points to the same object.
• Shallow Equality
Returns whether a and b are structurally similar.
One level of objects are compared.
• Deep Equality
Returns where a and b have object-networks that are structurally similar.
Multiple level of objects are compared recursively.
make: BMW
var1 model: M1
engine: ref
cylinders: 6
KW: 130
make: BMW
var2 model: M1
engine: ref
shallow equal
OOP: Object-Oriented Programming, Part 1 24
Equality Examples, cont.
make: BMW
cylinders: 6
var1 model: M1
KW: 130
engine: ref
make: BMW
cylinders: 6
var2 model: M1
KW: 130
engine: ref
deep equal
OOP: Object-Oriented Programming, Part 1 25
Types of Equality in Java
• ==
Equality on primitive data types
8 == 7
'b' == 'c'
Reference equality on object references
anotherPoint
Strings are special
String s1 = "hello"; String s2 = "hello";
if (s1 == s2){
System.out.println(s1 + " equals" + s2);}
• equals
Method on the class java.lang.Object.
Default works like reference equality.
Can be refined in subclass
onePoint.equals(anotherPoint)
/**
* Implements the equals method
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
return o instanceof Car // is it a Car object?
&& ((Car) o).getMake() == this.make
&& ((Car) o).getModel() == this.model
&& ((Car) o).getPrice() == this.price;
// relies on “short circuiting”
}