Anda di halaman 1dari 3

CONSTRUCTORS IN DERIVED CLASSES

constructors may be declared in a base class as well as in a derived class. constructors are not mambers of a class, and are not inherited by a derived class by following the syntax of constructors. the name of the constructors is the same as that of the name of class. constructors may have parameters, if the members of a class are to be initialized with specific values. the constructor - if not declared private - of he base class can be invoked within the derived class using the keyword super. If a derived class constructor invokes a constructor of its base class, either explicitly, yhere will be a whole chain of constructors called, all the way back to the ultimate base class. This is called constructor chaining, and this occurs when the inheritance tree is deep. Even though, private access modifier is allowed in a constructor, to invoke a base class constructor, the access modifier must be other than private. Private constructors are commonly used in classes that contain only static mambers or when it is important to control the creation of instaces of the class. Such constructors prevent instantiationof a class from each code context outside its declaration. In order to create an instance of a class that has only private constructors, a static method is normally declared inthat paticular class encapsulating the logic which decides whether the request of creating a new instance can be accomplished or not. TERJEMAHAN konstruktor dapat dinyatakan dalam sebuah basis kelas serta dalam kelas turunan . konstruktor bukan anggota kelas , dan tidak diwariskan oleh kelas turunan dengan mengikuti sintaks konstruktor . nama konstruktor adalah sama dengan nama kelas . konstruktor mungkin memiliki parameter , jika anggota kelas harus diinisialisasi dengan nilai-nilai tertentu . konstruktor - jika tidak dinyatakan swasta - dari dia kelas dasar dapat dipanggil dalam kelas turunan dengan menggunakan kata kunci super . Jika konstruktor kelas turunan memanggil konstruktor kelas dasar , baik secara eksplisit , akan ada seluruh rantai konstruktor disebut , semua jalan kembali ke kelas dasar utama . Ini disebut konstruktor chaining , dan ini terjadi ketika pohon warisan yang mendalam . Meskipun , pengubah akses pribadi diperbolehkan dalam konstruktor , untuk memanggil konstruktor kelas dasar , pengubah akses harus selain pribadi. Konstruktor swasta umumnya digunakan dalam kelas yang hanya berisi mambers statis atau saat penting untuk mengendalikan penciptaan instaces kelas. Konstruktor seperti mencegah instantiationof kelas dari setiap konteks kode luar deklarasi . Dalam rangka untuk membuat sebuah instance dari sebuah kelas yang hanya memiliki konstruktor pribadi , metode statis biasanya dinyatakan dalam kelas paticular encapsulating logika yang menentukan apakah permintaan membuat contoh baru dapat dicapai atau tidak .

INHERITACE AND MAMBER ACCESSIBILITY


A mamber of a class is said to be accessible when acces to it is allowed. When acces to a particular member is disallowed, the member is said to be inaccessible. Declarations of members in a class allow control over member acces. The accessibility of member is specified by including access modifiers in its declaration. Access modifier in a declaration controls the accessibility of an entity in a java program. It determines which components of a class can be seen and also from which classes they can be accessed. A summary of accessibility of members of classes for different modifier is shown in table 8.1. Access modifier restrict the visibility of a class and its member and thereby can deny the accessibility of the same. A class or a classmember may hide itself from other classes or other libraries by adding the acces modifier in its declaration. The accessibility control promotes encapsulation. When no access modifier is included, a default access allows members of the same class and other classes in the same package (package is discussed later). If the declaration of a member variable or a member method does not include any access modifier, the access modifier package is the default accessibility for class members. A derived class has access to all public and protected members of the base class, with both classes not necessarily in the same package. Private members are also inherited by the derived class, but are not directly accessible. The accessible members can be used, replaced, hidden, or supplamented with new members: New field not present in the base class can be declared in the derived class. The inherited fields can be used directly, just like the new field. A field can be declared with the same name as in the base class, thus hiding the latter. New methods not present in the base class can be declared in the derived class. The inherited methods can be used directly, just like new methods. A new instance method in the derived class, which has the same signature as yhe method in the base class can be defined, thus overriding the latter. A new static method in the derived class, which has the same signature as the one in the base class can be derived, thus hiding the latter. A derived class constructor that invokes the constructor of the base class can be defined, either indirectly or by using the keyboard super.

The effect of inheritanceis logically represented in fig. 8.5. Consider the following declarations of a base class A and a derived class B to illustrate private, protected, and public inheritance. In the derived class B, protected and public members of class A can be accessed directly. Hence any access to data fields ( protectedA and publicA) of class A from class B is valid. If private data fields have to be accessed, then base class method needs to provide set/get methods to access as in having method getPrivateA() in the base class A, with the proper access modifier.

TERJEMAHAN: Seorang anggota kelas dikatakan diakses ketika akses ke itu diperbolehkan . Ketika akses ke anggota tertentu dilarang , anggota yang dikatakan tidak dapat diakses . Deklarasi anggota dalam kelas memungkinkan kontrol atas akses anggota . Aksesibilitas anggota ditentukan

oleh termasuk pengubah akses dalam deklarasi . Akses pengubah dalam deklarasi mengontrol aksesibilitas dari suatu entitas dalam program java . Ini menentukan komponen kelas bisa dilihat dan juga dari kelas yang mereka dapat diakses . Ringkasan aksesibilitas anggota kelas untuk modifikator yang berbeda ditunjukkan pada tabel 8.1 . Pengubah akses membatasi visibilitas kelas dan anggotanya dan dengan demikian dapat menyangkal aksesibilitas yang sama . Sebuah kelas atau anggota kelas dapat menyembunyikan dirinya dari kelas lain atau pustaka lain dengan menambahkan pengubah akses dalam deklarasi . Kontrol aksesibilitas mempromosikan enkapsulasi . Bila tidak ada modifier akses disertakan , akses standar memungkinkan anggota dari kelas yang sama dan kelas-kelas lain dalam paket yang sama (paket dibahas nanti ) . Jika deklarasi variabel anggota atau metode anggota tidak termasuk pengubah akses , akses paket pengubah adalah aksesibilitas default untuk anggota kelas . Sebuah kelas turunan memiliki akses ke semua anggota masyarakat dan dilindungi dari kelas dasar , dengan kedua kelas tidak harus dalam paket yang sama . Anggota swasta juga diwarisi oleh kelas turunan , tetapi tidak langsung dapat diakses . Para anggota dapat diakses dapat digunakan , diganti , disembunyikan , atau ditambah dengan anggota baru : Bidang Baru - tidak hadir di kelas dasar - dapat dideklarasikan di kelas turunan . Bidang diwariskan dapat digunakan secara langsung , seperti bidang baru . Field dapat dideklarasikan dengan nama yang sama di kelas dasar , sehingga menyembunyikan kedua. Metode baru - tidak hadir di kelas dasar - dapat dideklarasikan di kelas turunan . Metode diwariskan dapat digunakan secara langsung , seperti metode baru . Sebuah metode contoh baru di kelas turunan , yang memiliki tanda tangan yang sama sebagai metode di kelas dasar dapat didefinisikan , sehingga mengesampingkan kedua. Sebuah metode statis baru di kelas turunan , yang memiliki tanda tangan yang sama dengan yang di kelas dasar dapat diturunkan , sehingga menyembunyikan kedua. Sebuah konstruktor kelas turunan yang memanggil konstruktor kelas dasar dapat didefinisikan , baik secara langsung maupun dengan menggunakan super Keyboard . Pengaruh warisan logis diwakili dalam ara . 8.5 . Pertimbangkan deklarasi berikut dari kelas dasar A dan kelas B diturunkan untuk menggambarkan warisan swasta, dilindungi , dan masyarakat . Dalam diturunkan kelas B , anggota protected dan masyarakat kelas A dapat diakses secara langsung . Oleh karena itu setiap akses ke bidang data ( protectedA dan publica ) kelas A dari kelas B berlaku . Jika bidang data pribadi harus diakses , maka metode kelas dasar perlu menyediakan set / mendapatkan metode untuk mengakses seperti memiliki metode getPrivateA ( ) di kelas dasar A , dengan pengubah akses yang tepat .

Anda mungkin juga menyukai