Class and Object
A class is an abstract definition of something that has attributes (sometimes called properties or states) and actions (capabilities or methods).
An object is a specific instance of a class that has its own state separate from any other object instance.
Inheritance and Polymorphism
Inheritance allows a class to be defined as a modified or more specialized version of another class.
Polymorphism is the capability to provide multiple implementations of an action and to select the correct implementation based on the surrounding context.
Overloading: a class might define two versions of a method with different parameters.
Overriding: the same method might be defined both in a parent class and a subclass.
Construction and Destruction
The constructor initializes the state of the object.
Constructor has same name with class, with no return type, invoke use new operator.
Private constructor: no one outside the class can directly instantiate the class. The class can't be inherited.
Java GC is responsible for finding and destroying unused objects, An optional finalizer method is invoked by the system prior to the object’s destruction to give it the opportunity to clean itself up before its “final” destruction.
Interfaces and Abstract Classes
An interface declares a set of related methods, the fields in interface are static and final by default.
Java is a single inheritance language. Interface makes the language more flexible and achieve multi-inheritance.
Java is a single inheritance language. Interface makes the language more flexible and achieve multi-inheritance.
An abstract class is an incomplete class definition that declares but not define all the methods(actually a class with abstract keyword is a abstract class, we can also define a class abstract, even if the definition of methods in this class is complete).
Before the subclass complete the implementation of all the class, we can't create objects with this abstract class.
Why need abstract?
Some classes extends from a abstract class share properties may be different on several methods. Why we don't give this method a default implementation in the abstract class. The reason is that if we forget the give a implementation in the subclass, we can find the error on compile time. Without the abstract, we need to find the error on the run time.
We can use an interface to create a object?
In fact, during the compiling, there is a virtual class implements this interface.
public Comparator<Integer> ListCom = new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { return o1 - o2; } };
Virtual Methods
In OOP, child classes can override (redefine) methods defined by ancestor classes. If the method is virtual, the method definition to invoke is determined at run time based on the actual type (class) of the object on which it is invoked. Non-static Java methods are always virtual.
Virtual methods aren’t free. It (almost always) takes longer to invoke a virtual method because the address of the appropriate method definition must be looked up in a table before it is invoked. This table also requires a small amount of extra memory.
Example of OOD
abstract class A { public void print() { System.out.println("I am class A"); } public void print2() { System.out.println("I am another method in A"); } } class B extends A { public void print() { System.out.println("I am class B"); } } class C extends B { public void print() { System.out.println("I am class C"); } public void print(int a) { System.out.println("I am another method in C"); } } public class Example { public static void main(String[] args) { //A a = new A(); error A ab = new B(); A ac = new C(); B bb = new B(); B bc = new C(); C cc = new C(); //C cb = new B(); error System.out.println("ab"); ab.print(); ab.print2(); System.out.println("ac"); ac.print(); System.out.println("bb"); bb.print(); System.out.println("bc"); bc.print(); System.out.println("cc"); cc.print(); cc.print(1); } } ab I am class B I am another method in A ac I am class C bb I am class B bc I am class C cc I am class C I am another method in C
No comments:
Post a Comment