@gone
2014-10-05T16:10:04.000000Z
字数 8824
阅读 1499
OOP DS Java
>>> indicates unsigned right shift
e.g,
22 = 0b00010110
22>>>100001011-22>>>1 //-22 补码表示110100101110100
Reference data type
When an instance of certain class is created, its type and interior are determined and cannot be changed. Any typecasts action just cut down some abilities of the object, e.g, some functions may be unvisible, but the iterior is still the same.
public class TypeCasting {public static void main(String[] args) {Father father = new Son();/** the `father` reference to a Son object, but based on its type(Father), it's only visible to `show(), show_father()` functions, in the meanwhile, `show()` has been overriden.*/father.show(); // output : `This is son`father.show_father(); //output : `This is father`Son s = new Son();s.show_father(); // output : `This is father`s.show_son(); // output: `This is son`father = (Father) father; // cast to Father, but the object it references to is still type of Sonfather.show(); //output: 'This is Son'father.show_father(); // output : `This is father`}}class Father {public int name = 0;public void show() {System.out.println("This is father");}public void show_father() {System.out.println("father!");}}class Son extends Father {public int name = 1;public void show() {System.out.println("This is Son");}public void show_son(){System.out.println("Son!");}}
First of all, "Collection" and "Collections" are two different concepts. As you will see from the hierarchy diagram below, "Collection" is a root interface in the Collection hierarchy but "Collections" is a class which provide static methods to manipulate on some Collection types.

| Interfaces | Hashtable | Resizable Array | Tree | Linked List | Hash table + Linked List |
|---|---|---|---|---|---|
| Set | HashSet | TreeSet | LinkedHashSet | ||
| List | ArrayList | LinkedList | |||
| Queue | |||||
| Map | HashMap | TreeMap | LinkedHashMap |

Also known as "Data Hiding"
he encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how thinigs work and just exposing the requests a user can do.
Inheritance is the property which allows a Child class to inherit some properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.
public class Parent{public String parentName;public int parentage;public String familyName;}public class Child extends Parent{public String childName;public int childAge;public void printMyName(){System.out.println(“ My name is “+ chidName+” “ +familyName)}}
What is multiple inheritance and does java support?
If a child class inherits the property from multiple classes is known as multiple inheritance.
Java does not allow to extend multiple classes but to overcome this problem it allows to implement multiple Interfaces.
The abiltiy to define more than one function with the same name is called Polymorphism. In Java,C++ there are two type of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).
overloading
Class BookDetails{String title;String publisher;float price;setBook(String title){}setBook(String title, String publisher){}setBook(String title, String publisher,float price){}}
overriding
class BookDetails{String title;setBook(String title){ }}class ScienceBook extends BookDetails{setBook(String title){} //overridingsetBook(String title, String publisher,float price){ } //overloading}
Abstraction is way of converting real world objects in terms of class. For example creating a class Vehicle and injecting properties into it. E.g
public class Vehicle {public String colour;public String model;}
The difference between Abstraction and Encapsulation
Encapsulation is a strategy used as part of abstraction. Encapsulation refers to the state of objects - objects encapsulate their state and hide it from the outside; outside users of the class interact with it through its methods, but cannot access the classes state directly. So the class abstracts away the implementation details related to its state.
Abstraction is a more generic term, it can also be achieved by (amongst others) subclassing. For example, the class List in the standard library is an abstraction for a sequence of items, indexed by their position, concrete examples of a List are an ArrayList or a LinkedList. Code that interacts with a List abstracts over the detail of which kind of a list it is using.
Abstraction is often not possible without hiding underlying state by encapsulation - if a class exposes its internal state, it can't change its inner workings, and thus cannot be abstracted.
What is association?
Association is a relationship between two classes. In this relationship the object of one instance perform an action on behalf of the other class. The typical behaviour can be invoking the method of other class and using the member of the other class.
public class MyMainClass {public void init(){new OtherClass.init();}}
What is Aggregation?
Aggregation has a relationship between two classes. In this relationship the object of one class is a member of the other class. Aggregation always insists for a direction.
public class MyMainClass{OtherClass otherClassObj = new OtherClass();}
Java has several modifiers keywords public, protected, private, the last modfier keywords is null or by default(no explicit).
Java's access level can be divided into two parts:
1. Access Level for class
2. Access Level for members(fields and methods)
For 1
public class means this class can be accessed outside, class by default means it's private (only visible within package);
For 2
There're 4 levels for members: public, private, protected, package-private(no explicit modifier). The following table summarizes the access levels of each keyword:
| keyword | class | package | subclass | world |
|---|---|---|---|---|
| public | yes | yes | yes | yes |
| protected | yes | yes | yes | no |
| no modifier | yes | yes | no | no |
| private | yes | no | no | no |
throw
public void parent(){try{child();}catch(MyCustomException e){ }}public void child{String iAmMandatory=null;if(iAmMandatory == null){throw (new MyCustomException("Throwing exception using throw keyword");}}
Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references and those references are passed by value.
Dog aDog = new Dog("Max");foo(aDog);aDog.getName().equals("Max");public void foo(Dog f) {f.getName().equals("Max"); // return truef = new Dog("Fif"); // the reference is changedd.getName().equals("Fif"); // return true}
What is responsiblity of Garbage Collector?
Answer Garbage collector frees the memory occupied by the unreachable objects during the java program by deleting these unreachable objects.
It ensures that the available memory will be used efficiently, but does not guarantee that there will be sufficient memory for the program to run. GC is a daemon thread behind the application, started and controled by JVM, it stops when all the non-daemon threads stop.
Define unreachable objects:An object becomes unreachable when no live thread can access it
One can request garbage collection to happen within the java program, but there's no guarantee that this request will be taken care of the JVM. So that Garbage Collection cannot be forced by any means.
Purpose of finallize()
The finallize() method should be overridden fo an object to include the clean up code or to dispose of the system resources to be done before the object is garbage collected.
There are several ways to request the garabge collection from JVM
1. Runtime.getRuntime().gc()
2. System.gc()
In Java, exception can be checked or unchecked. They both fit into a class hierarchy. The following diagram shows Java Exception classes hierarchy.
Red colored are checked exceptions. Any checked exceptions that may be thrown in a method must either be caught or declared in the method's throws clause. Checked exceptions must be caught at compile time. Checked exceptions are so called because both the Java compiler and the Java virtual machine check to make sure this rule is obeyed. Green colored are uncheck exceptions. They are exceptions that are not expected to be recovered, such as null pointer, divide by 0, etc.
If an exception can be properly handled then it should be caught, otherwise, it should be thrown.

Commonly Used Exceptions:
IllegalArgumentException
ArrayIndexOutOfBoundsException
A、Interface changes with default and static methods
B、Functional interfaces and Lambda Expressions
C、Java Stream API for collection classes
D、Java Date Time API