Recitation 3

advantages of constructors?
user does not to know the detail implementation
'super' keyword to call superclass constructors

Encapsulation - protect your data

polymorphism

dynamic binding -

circle extends shape
c1 = new circle()
print (shape) c1 --> still circle reference variable

'super' keyword to call superclass constructor
remember to put @Override

use superclass reference variable to reference subclass instance - polymorphism

toString() usage

Recitation 4

array length is fixed and has a field called length
index bound are checked on every access

method:
method signature

"==" vs. equals() (Identity vs. Equality)
compareTo() method

static members

  • belong to the class
  • shared by all the instances

No need to initiate class to use static method

if no equal(), then goes to Object's equal

public boolean equals(Person other) {

if (this == other) {
return true

}

....

}

StringBuilder() has a lot of constructors.....
Remember single quotes are for char literals and it is a mapping between the numberic code valies and "characters"

StringBuider a = new StringBuilder('a') // assign the length of string with 'a' length

Recitation 5

Null vs Empty String

null - does not point to any instance of that type

"" - String object

ex:

setLastName(lastname)

if (lastname == null) {throu illegalargumentexception}

if (lastname.equals(""))

if(lastnmae.trim().equals(""))

List<employ> employ = new ArrayList<employ> <-- polymorphism

public int compareTo(Employee other){

int lastComp = lastName.compareTo\(other.lastName\)

}

Polymorphism

Generics - detect errors at compiling time

getList():
do not return the object itself, return the copy of it

You should always think twice before returning a reference to an internal object that is mutable like list

Boolean(wrapper class) != boolean

static method can be called by null object's method

Integer.valueof(500) == Integer.valueof(500) --> false

static in class will be shared

if the method is independent to instance than make it static method

Why method?
shorter program

comparable v.s. comparator
Collections.sort(list) // default use comparable, compareTo()

Most nested classed can and should be declared static
need to protect mutable data ( int a = new date(a.getTime()))

getHireDate() {

return new Date(hireData.getTime());

}

create outer class first and then use inner static class

if the the class is final, no other class can subclass it

https://stackoverflow.com/questions/279507/what-is-meant-by-immutable

results matching ""

    No results matching ""