Wednesday, December 23, 2015

Polymorphism

What:
多态

Why:
So the methods can be called based on the dynamic object type. (one of them).

Details:

Step1: Compiler interprets the code.
Step2: Runtime environment executes interpreted code.
Compile time rules:
Compiler only look at reference type. (solution: casting)
Can only look in reference type class for method.
Outputs a method signature.
Run time rules:
Follow exact runtime type of object to find method.
Must match compile time method signature
Run time type check: "instanceof"

Abstract classes and Interface:
  Abstract:
    - Can make any class abstract with keyword:
      public abstract class Person {
    - Class must be abstract if any methods are:
      public abstract void monthlyStatement() { (concrete subclass must override this method)

Implementation vs. Interface:
Implementation: instance variables and methods which define common behavior
Interface: method signatures which define required behaviors
Abstract class can do both.
If only interface needed, use Interface.

  Interfaces:
    - Interfaces only define required methods
    - Classes can inherit from multiple Interfaces

If you just want to define a required method: Interface
If you want to define potentially required methods AND common behavior: Abstract class




No comments:

Post a Comment