Arama butonu
Bu konudaki kullanıcılar: 1 misafir, 1 mobil kullanıcı
7
Cevap
226
Tıklama
0
Öne Çıkarma
Java'da protected access
S
7 yıl
Yüzbaşı
Konu Sahibi

Java çalışırken beni zorlamış bir örnektir, hala tam izah etmekte zorlanırım. Java bilgisini ölçmek isteyen baksın, acele cevap yazmayın, boş cevaplara aldırış etmeyin.
MyItem türü elemanlardan oluşan bir MyQ class ım var. İçinde head ve tail e işaret eden iki protected field var, add() ve remove() public metotları var. Başka bir pakette(!) UniqMyQ olarak bunu extend ediyorum, add() ve remove() override ediyorum ve bir de unique() diye metot ekliyorum. add() ve remove() inherit edilen protected field lara direkt erişebiliyor, unique() metotu UniqMyQ türü başka nesnelerin protected field'ına direkt erişebiliyor, tamam. Fakat MyQ türü nesnenin protected field ına erişemiyor. A UniqMyQ is-a MyQ relationship olmasına rağmen bu neden mümkün değildir?
 
package pk1;

public class MyQ {
protected MyItem head;
protected MyItem tail;

public void add(MyItem t) { }
public MyItem remove() { return null; }
}

 
package pk1;

public class MyItem {
// TODO
}

 
package pk2;

import pk1.*;

public class UniqMyQ extends MyQ {
@Override
public void add(MyItem t) {
MyItem s = head;
// TODO
}

@Override
public MyItem remove() {
MyItem s = head;
return null; // TODO
}

public void uniq(UniqMyQ otherq) {
MyItem s = otherq.head;
// TODO
}

/* UniqMyQ is-a MyQ olmasina ragmen bu neden hatali?
public void uniq(MyQ otherq) {
MyItem s = otherq.head;
// TODO
} */

}



< Bu ileti mini sürüm kullanılarak atıldı >

M
7 yıl
Onbaşı

Protected access modifier ın tanımına baksanız direk anlarsınız durumu zaten.

Size karmaşık gelen kısım tam olarak neresi?

Daha detaylı bir açıklama;

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C. 

In addition, if Id denotes an instance field or instance method, then:

If the access is by a qualified name Q.Id or a method reference expression Q :: Id (§15.13), where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

If the access is by a field access expression E.Id, or a method invocation expression E.Id(...), or a method reference expression E :: Id, where E is a Primary expression (§15.8), then the access is permitted if and only if the type of E is S or a subclass of S.

If the access is by a method reference expression T :: Id, where T is a ReferenceType, then the access is permitted if and only if the type T is S or a subclass of S.


Bu mesaja 1 cevap geldi.
S
7 yıl
Yüzbaşı
Konu Sahibi

JLS ten bır sectıon olduğu gibi alıp kopyalamışsın, biliyorsan direkt soruya cevap ver, en azından o yazdığın section da hangi paragraf(lar) cevabı veriyor onu söyle.



< Bu ileti mini sürüm kullanılarak atıldı >
Bu mesaja 1 cevap geldi.

Bu mesajda bahsedilenler: @MCXXXX
P
7 yıl
Teğmen

umarim yalnis deildir ama soyliyeyim.

protected deiskenlerde farkli paketten disaridan erisim yasaktir fakat paket ayni ise disaridan erisebilirsiniz. `MyItem s = otherq.head; ` buradaki kod hatali cunki otherq nesnesinin tipi yani sinifi farkli bir paketde tanimlansmis dolayisiyla bu sinifa ait protected bir field'a farkli bir paketten erisemezzsiniz ama farkli bir pakette bu sinifi extend eden bir sinifin icinden erisebilirsiniz.


Bu mesaja 1 cevap geldi.
M
7 yıl
Onbaşı

Genel tanım için koydum onu. Kod üzerinde tam olarak hangi kısmı anlamadın?

Daha net olmak gerekirse "farklı package içerisinde "Parent reference" ile protected member a erişemezsin.".


Bu mesaja 1 cevap geldi.

Bu mesajda bahsedilenler: @seyfi84
S
7 yıl
Yüzbaşı
Konu Sahibi

Rationale nedir? ezbere kalkıp, çünkü JLS öyle emretmiş diye cevap verme. Onun bir sebebi var, onu izah et.



< Bu ileti mini sürüm kullanılarak atıldı >


Bu mesajda bahsedilenler: @MCXXXX
S
7 yıl
Yüzbaşı
Konu Sahibi

tamam, farklı pakette de olsa extend ettiğim için erişebilmem lazım, fakat erişemiyorum. sebebi nedir?



< Bu ileti mini sürüm kullanılarak atıldı >
Bu mesaja 1 cevap geldi.

Bu mesajda bahsedilenler: @psikolojikSorunlu.insan
P
7 yıl
Teğmen

cunki field'lar ayni deil. o fonksiyon yani uniq fonksiyonu sadece kendi head deiskenine ve kendi sinifindan olusturulmus baska nesnelerin head deiskenine erisebilir. eger siz o kodu soyle yapsaydiniz hata ortadan kalkardi ama bu seferde calisma zamani hatasi ortaya cikabilirdi: MyItem s = (UniqMyQ)otherq.head; eger otherq ya verilen objectin tipi UniqMyQ deilse calisma zamani hatasi cikar.

umarim yalnis deildir :)

Edit:
Yalnismis test ettim. Sadece inheritance yoluyla kendi ust sinifinin deiskenlerine erisebiliyormus.





< Bu mesaj bu kişi tarafından değiştirildi psikolojikSorunlu.insan -- 13 Mart 2018; 20:3:55 >


Bu mesajda bahsedilenler: @seyfi84
DH Mobil uygulaması ile devam edin. Mobil tarayıcınız ile mümkün olanların yanı sıra, birçok yeni ve faydalı özelliğe erişin. Gizle ve güncelleme çıkana kadar tekrar gösterme.