Showing posts with label 常用知识点. Show all posts
Showing posts with label 常用知识点. Show all posts

Monday, July 14, 2014

Java OO Conceptions

What is Composition  and Inheritance
Composition:  create objects of your existing class inside the new class.
Inheritance: create a new class as a type of an existing class.
Classes is allowed to inherit commonly used state and behavior from other classes.

What is Polymorphism(dynamic binding)
The ability of an object to take on many forms(You have the same interface from the base class, and different forms using that interface: the different versions of the dynamically bound methods. ).
Like override( when a parent class reference is used to refer to a child class object.).

Data Abstract
Abstract Method: a method that is incomplete; it has only a declaration and no method body;
abstract void f();
An abstract class 可以没有abstract method, 但是有abstract method的class一定是abstract class。
You can not make an object of an abstract class!!!

Abstract Class:
  • The implementation is provided by inheritors. 
  • Abstract class can have fields. 
  • Can have complete default code / details to be overridden. 
  • Can have access modifiers. 
  • Is-a relationship
Interface:
  • Support multiple inheritance. 
  • Interface can also have fields, but these are implicitly static and final. 
  • Completely abstract class, can not have code, just signature. 
  • Can not have Access Modfiers, everything is assumed as public. 
  • Has-a realtionship.
Encapsulation(data hiding):
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods.

Design Pattern

Singleton
  • Ensures that a class has only one instance and ensures access to the instance through the application.
  • Useful in cases where you have a “global” object with exactly one instance.
public class Restaurant{
    private static Restaurant instance = null;
    public static Restaurant getInstance(){
        if(instance == null)
            instance =  new Restaurant();
        return instance;
    }
}
Factory Method
Offers an interface for creating an instance of a class, with its subclasses deciding which class to instantiate.
  • Set creator class as an abstract class.
  • Creator class provides an implementation for the Factory method.
public class CardGame{
    public static CardGame createCardGame(GameType type){
        if(type == GameType.Poker)
            return new PokerGame();
        else if(type == GameType.BlackJack)
            return new BlackJackGame();
        return null;
    }
}

Database Conceptions

DB - Database Transactions
A transaction groups a set of related database manipulations together into a single unit. If any operation within the transaction fails, the entire transaction fails, and any changes made by the transaction are abandoned (rolled back). Conversely, if all the operations succeed, then all the changes are committed together as a group.

DB - Four properties of a transaction : ACID
Atomicity — The database system guarantees that either all operations within the transaction
succeed or else they all fail.
Consistency — The transaction must ensure that the database is in a correct, consistent state
at the start and the end of the transaction. No referential integrity constraints can be broken,
for example.
Isolation — All changes to the database within a transaction are isolated from all other queries
and transactions until the transaction is committed.
Durability — When committed, changes made in a transaction are permanent. The database
system must have some way to recover from crashes and other problems so that the current
state of the database is never lost.

DB - Normalization and Denormalization
Normalization:  designed to minimize redundancy. 
Denormalization: designed to optimize read time by adding redundant data,  commonly used to create highly scalable systems. 
cons:
·       updates and inserts are more expensive.
·       Data may be inconsistent.
·       Data redundancy uses more storage.
pros:
·       Retrieving data is faster since we do fewer joins.
·       Queries to retrieve can be simpler.

DB – Different types of joins
Inner join: the result would contain only data where the criteria match.
outer join:
 left join, right join, full outer join

SQL and NoSQL
SQL has predefined schema. For Large organizations,  the relationships and tables can be numbered in millions, combine those relationships can suffer major performance issue. 

NoSQL databases scale up horizontally, adding more servers to deal with larger loads. NoSQL is much more flexible, not using schemas defined beforehand. This allows users to add information and make changes at any time without disrupting the system or needing to transfer a large amount of data.

Java Basic Conceptions

Difference between final, finally and finalize() in Java
final - constant declaration.
· Variable: cannot be changed once initialized
· Method:  cannot be overridden by a subclass.
· Class: cannot be subclassed.

finally - handles exception.
Used with a try/catch block, guarantees that a section of code will be executed.

finalize() - method helps in garbage collection. A method that is invoked before an object is destroyed by the garbage collector. Can be overridden to define custom behavior during garbage collection.

Static Class loading and Dynamic Class loading
Static Class Loading : Using new operator you load classes statically in java.
In static class loading NoClassDefFoundException can be thrown if the loaded class is not available in run time.
Dynamic Class Loading: This can be achieved by invoking the Class loader functions programmatically. Typically we use Class.forName(String className) to get the class first and then we will call the newInstance() method on the returned class to get the instance.

What is Java garbage collection?
It frees memory allocated to objects that are not being used by the program any more.
In Java the GC runs automatically, but you can also call it explicitly with System.gc() and try to force a major garbage collection.

Strong reference, weak reference, soft reference, phantom reference
Strong reference never be collected by GC.
Weak reference can be collected when there is no strong reference points to the same object.
Soft reference can be collected when there is not enough memory.
Phantom’s get method will always return null.

Difference between StringBuffer and StringBuilder 
Java String is immutable and Final.  StringBuffer creates an array of all the strings, copying them back to a string only when necessary.

StringBuffer is synchronize(thread safe)
StringBuilder is not, which makes StringBuilder fast.

Java Static keyword
The  static method or field is not tied to any particular object instance of that class.  So you can call that method or field without creating any object.

Interface and Abstract Class



Overloading vs. Overriding
Overloading: describe when two methods have the same name but differ in the type or number of arguments.
Overriding: when a method shares the same name and function signature as another method win its super class.