Lesson 1
private: this Class.
package-private(default): Classes in same package
protected: Subclasses + Classes in same package
public: All
static: create class method and variables
final: finalize the implementation
abstract: create abstract class and methods
synchronized: prvent
Static:
- Static variable
Refer the common property of all objects of a class Gets memory only once in class area at the time of class loading
isolated from enclosing class instance.
- Static method
class Arrays {
public static void sort(int[] a) {
...
}
}
public static void main(Strings[] args) {
int a[] = {2, 3, 1};
Arrays.sort(a[]);
}
Belongs to class rahter than object of the class
Can be invoked without creating an instance of a class
Can access and update static member of the class
Cannot access non-static members
the same, cannot access non-static members.
- Static class
Outer class cannot be static Inner class can be static
** User inner class without creating an instance of outer class
static inner class isolated from enclosing class instance.
- Static initializer
public class Solution { |
Run only one time before constructor/mmain method called
No return
No arguments
No this/super support
Final
Final Class: class cannot be extended Final method: method cannot be override
** Final variable: variable can only be assigned once. (Reference!)Data Structures
What you’ll learn in this course:
ArrayList LinkedList
HashTable/Map/Set Tree
Stack/Queue Heap
ArrayList in Jave - method review
create - List
add - list.add(e) - return: boolean
remove - list.remove(index) - return: element removed
get - list.get(index) - return element
size - list.size() - return: int
*Algorithms
What you’ll learn in this course:
Binary Search
Sort (quickSort/mergeSort)
DFS & BFS
Dynamic Programming
Linear Search vs Binary Search
Search target: 51
Middle index = (start + end) / 2 = (0+8)/2 = 4
Search
Linear Search: O(n)
Binary Search: O(logN)
Lesson 2
4 Fundamental Concepts
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Inheritance
- SubClass acquire properties from SuperClass
- Java does not support multi-inheritance => can only extend from one class.
Overriding vs Overloading
- Overloading: allow a class to have multiple methods of the same name signature
public Vehicle() { |
native implementation which provides the memory address to a certain extent.
Polymorphism
- Single object -> multiple forms
Abstraction
- Provide a generalization over a set of behaviors
- User cares about high-level functionality not implementation
- abstract class & interface
Interface - Java 8 - Default Methodinterface Vehicle {
String getOwner();
default void start() {
System.out.println(
String.format("%s is starting vehicle... ", getOwner());
)
}
}
Interfaces vs Abstract Classes
Interfaces (Interface => Description (e.g. Icomparable or Iserializable))
- Do not maintain state (all variables are static final).
- Multiple inheritance
Abstract Class (Abstract Class => Code Sharing Basis )
- Share code among classes.
- Share common non-public method
- Share code which could chnange the state of the object.
Encapsulation
- Hiding implemementation details
- Easy to change implementation with new requirements
- Control “who” can access “what”
Factory Pattern
- Remove instantiation of actual implmentation classes from client code.
4 Fundamental Concepts
- Inheritance: one class based on another
- Polymorphism: single object -> many forms
- Abstraction: provide generalization
- Encapsulation: hide implememention details
Test: ParkingLot
- ParkingLot
- Slot - small, large, etc
- Vehicle - Car, Bus, etc
- Parker ?
- Park car
- UnPark car
- Find empty space
- Is full?
Java Multi-threading
Thread vs Process
- A process has one or more threads
- Threads share the address space; processes have own address space.
- Cancel, change priority, etc to main thread may affect the behavior of other threads;
changes to parent process does not affect child process.
Life-cycle of a thread
------------End of execution--------------
| V
New Thread() -> New -> (Start()) -> Runnable -> (run()) -> Running -> (Sleep(), Wait()) -> Waiting -> Dead
| ^
| |
---------------------------------------------------------------------------------------
Create a Thread
public class VehicleClient implemeents Runnable { |
public class VehicleClient extends Thread { |
Algorithms
English-language description
Compute the greatest common divisor of two nonnegative integers p
and q
as follows: if q
is 0, the answer is p
. If not, divide p
by q
and take the remainder r
. The answer is the greatest common divisor of q
and r
.
Java-language description
```java
public static int gcd(int p, int q)
{
if (q == 0) return p;
int r = p % q;
return gcd(q, r);
}