Primitive data type, and object data type
Student s = new Student():
compiler will create a student object in heap, and then pass a reference of the heap space to variable s (it's the address).
Person p = new Student(); -> works
Student s = new Person(); -> not work,
Person p; p = s; -> works,
Student s; s = p; -> not work
等号左边要包括右边
Protected: same packeage, subclass, same class
Package: same package, same class
These two are not recommended, as other class in same package can access.
Everything extend from Java Object class
Object creation: from subclass follow constructor all the way to object, and then initialize one by one.
From object, to subclass。
Compiler rules:
- if no super class, extends object
- If no constructor, compiler gives one for you
- First line in constructor, must be either this()->same class, another constructor, or super(). Otherwise compiler will do super()
One of the confusing thing is:
public class Person {
private String name;
public Person(String s){
this.setName(s);
}
public Person(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Student extends Person{
private String name;
public Student(){
super("Lily");
this.name = "Amy";
}
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.getName());
System.out.println(s.name);
}
}
这段代码的输出是先Lily,再Amy。子类class继承了父类的所有方法与变量,但如果不加声明 (不在子类中定义一个string name),子类用的是父类的name变量。而s.getName()是子类调用父类的方法,表示取得了父类的name。在子类调用Super时,子类把父类实例化,同时赋值了父类的name
Overloading: Same class has same method, with different parameters. Return type can be different, as long as the parameters are different. Java compiler only identifies parameter list.
Overriding: Sub-class has same signature (name, parameters list) as super class
Polymorphism: what it gives us, is the ability to keep alll of our objects in one big collection, and then call methods on every single obect, and appropriate method will be make sure to get executed.
Polymorphism:
Compile time rules:
- compiler only knows the reference type:
Person p = new Student() -> compile only knows p is a reference to Person object type
- Compiler can only look at reference type class, for method, and Compiler outputs a method signature
Run time Rules
- Follow exact Runtime type of object,
- Find the method that matches Compiler signature, on Runtime object.
Person p = new Student() -> Run time now follows the Student object,
Abstract Class vs Interface
Both two can achieve the following:
- Force subclasses to have/override this method,
- Stop having actual instantiated objects but keep having references.
Interface only provides "Final Static" Variables and Abstract Methods, it doesn't provide common codes. Interface is implicitly abstract, you don't need to declare it. Interface doesn't contains constructor.
Interface is a good way, to force Sub-classes to implement the functions.
However Abstract Class can contain Abstract Methods, and it can also contains common codes (common methods). Common methods are the methods that Sub-classes can just extend, and don't need to implement themselves.
If one method in class in Abstract, class must be Abstract. Abstract class cannot be instantiated. To use Abstract class, you have to extend it, and must override the Abstract method.
Class Relationship
Association: Student select a class
Aggregation: has a relationship, one to many mapping
Combination: has a relationship, one to one mapping,
Dependence: supplier and customer,
Inheritance: Is a relationship