Java 程式設計入門
講師:陳昭源
CSIE, NTU
August 6, 2005
Outline
• Access Control
• Inheritance
Dept. of Computer Science & Information Engineering,
National Taiwan University August 6, 2005 Page 3
Access Control
• 控制存取的關鍵字
– public、protected、private – 置於類別或成員宣告之前
– 若沒有指定任何存取權限,表示此類別或成員僅供相 同類別庫的其他類別使用,稱為 package access 層級
– 將類別或成員設定為 package access 層級的好處
是可使同在一個類別庫下的每個類別互相使用成員
Access Control
• public
– 任何類別皆可存取
• default
– packetage access
• protected
– 允許宣告的類別、子類別與同一個套件中的類別使用
• private
– 只有在類別內部可以存取
Dept. of Computer Science & Information Engineering,
National Taiwan University August 6, 2005 Page 5
Access Control
• Class Access
– 類別不可設定為 private 或 protected(Why?)
– 為了避免他人任意使用 constructors 建立物件 –將 constructors 設定為 private
–提供 static 的 methods 建立物件、複製物件 – 在一個 Java 檔案中只能有有一個 public class,且
必須與檔案名稱相同,包含大小寫
Inheritance
• Code Reusing
– more than copy and change it
– reuse code by creating new classes with the use of the existing classes that someone has already built
– composition vs. inheritance
• Composition
– creating objects of an existing class in the classes
• Inheritance 繼承
– creating a new class by literally using the form of the
Dept. of Computer Science & Information Engineering,
National Taiwan University August 6, 2005 Page 7
Inheritance
• Composition
– Creating objects of an existing class in the classes
– Each object reference in the class is initialized to null.
– Before using objects, we have to call the constructor to initialize them.
– We can initialize an object right before using it, and it is called “lazy initialization.”
– Lazy initialization can reduce unnecessary
overhead, for it is expensive to create an
object.
Inheritance
• Inheritance
語法: class ClassName extends BaseClass – Each class in Java inherits from Java
standard root class, “Object,” without any explicitly claim.
– Each class in Java can inherit from only one (non-interface) base class.
– Base Class (SuperClass):基底類別、父類別 – Derived Class (Subclass):衍生類別、子類別 – The derived class would inherit all
accessible members in the base class,
Dept. of Computer Science & Information Engineering,
National Taiwan University August 6, 2005 Page 9
Inheritance
• Inheritance Rules
– The derived class inherits the public or protected members in the base class.
– Members without any access level specified can be inherits only in the same package.
– If the derived class has members whose names are the same as those in the base class, they would not be
inherited from the base class.
Æ variable-hiding & method-overriding
– Whenever an object of the derived class is initialized, it would always call the constructor of the base class
before calling its own. (Why?)
Inheritance
• From the outside, it looks like the derived class has the same interface as the base class and maybe some
additional methods and fields. But inheritance does not just copy the interface of the base class.
• When you create an object of the derived class, it contains within it a subobject of the base class. This subobject is the same as if you had created an object of the base class by itself.
• It is just that from the outside, the subobject of the base class is wrapped within the derived-class object.
• It is essential that the base-class subobject be initialized correctly, and there’s only one way to guarantee this: