JAVA OOD 소결

4205 단어
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 입 니 다.하위 클래스 클 라 스 는 부모 클래스 의 모든 방법 과 변 수 를 계승 합 니 다. 그러나 성명 을 추가 하지 않 으 면 (하위 클래스 에서 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

    좋은 웹페이지 즐겨찾기