Interface etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Interface etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

2 Mart 2020 Pazartesi

Interface - Java Private Method

Giriş
Java 9'dan itibaren interface'lere private metodlar yazılabiliyor. Bu metodlar Interface içindeki default metodlardan çağrılabilir.

9 Haziran 2019 Pazar

Interface

Giriş
Interface şöyle tanımlanır
public interface FooInterface {
  public void foo ();
}
Interface İsmi
Açıklaması şöyle. Bu yüzden Cloneable, AutoCloseable güzel isimler.
Interface names should be an adjective. Sometimes, it can be noun as well like List or Map. Like classes, it should be in CamelCase, but the first letter should be capitalized.
Static Final Alan
Sadece static final alanlardan oluşan arayüz kötü bir tasarım. Effective Java kitabından bir alıntı şöyle
The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class’s exported API. It is of no consequence to the users of a class that the class implements a constant interface
Yine kitaptan bir alıntı şöyle
There are several constant interfaces in the Java platform libraries...
These interfaces should be regarded as anomalies and should not be emulated.
Java 7 Değişiklikleri
Constant = public static final kabul edilir.
Metod = public abstract kabul edilir.

Java 8 Değişiklikleri
İlave olarak şunlar geldi
1. Default metod
2. Static metod

Bu metodlar ile ilgili eleştiri şöyle.
Java 8 screws this up for methods; an interface now can declare both default methods and static method implementations. This brings a large chunk of the Diamond of Death problem into the language.
Bu yeni metodlar abstract kabul edilirler. Açıklaması şöyle
An interface method lacking a default modifier or a static modifier is implicitly abstract... It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration.
Yani şöyle de yapsak aynı şey
public interface FooInterface {
  public abstract void foo ();
}
1. Default metod

Interface - Default Method yazısına taşıdım.

2. Static metod
Açıklaması şöyle. Interface'teki static metod kalıtılmaz hatta görünmezdir. Interface içindeki static metod sadece Interface içinden kullanılır.
A class does not inherit static methods from its superinterfaces.
Aynı şey kalıtan iki arayüz için de geçerli. Açıklaması şöyle.
An interface does not inherit static methods from its superinterfaces.
Örnek
Şu kod derlenmez.
public interface Super {
  static void test(){ }
}

public interface Sub extends Super {
}

Sub.tes(); //compile error

Java 9 Değişiklikleri
İlave olarak şunlar geldi
1. Private metod
2. Private static metod


Olabilen Şeyler
1. Private metod
Açıklaması şöyle. Yani sadece default metodlara yardımcı olmak içindir
Private methods are not inherited by subclasses, so this feature doesn't affect implementation classes. I believe the private methods in interfaces allow us to share code between default methods.
Açıklaması şöyle
With the release of Java 9, private methods are also supported in interfaces. We cannot create objects of an interface. Hence, private methods are used as helper methods that provide support to other methods in interfaces.
Olamayan Şeyler

1. Constructor
Açıklaması şöyle
Java interfaces still cannot contain fields and constructors.
2. Field yani static Olmayan Alanlar
Açıklaması şöyle
Java interfaces still cannot have non-static members



22 Mayıs 2019 Çarşamba

Interface - Java 8 Default Method

Giriş
Java 8'den itibaren bir arayüz default metodlara sahip olabilir ancak halen state/field/property sahibi olamaz. Örneğin artık Iterable arayüzü remove default metoduna sahip. Bu metod UnsupportedOperationException fırlatıyor. Böylece excepion fırlatan remove metodu yazmamıza gerek kalmadı.

Interface için default metod özelliği C# 8 ile de .Net dünyasında geldi.

Java 9 ile interface içinde private metod da olabiliyor.

1. Tanımlama Konusu
Interface'teki metodun başına default anahtar kelimesi eklenir.
Örnek
Collection sınıfındaki stream default metodu şöyledir.
default Stream<E> stream() {
  return StreamSupport.stream(spliterator(), false);
}
2. Lambda İle Kullanma Konusu
Java 8 ile geliyor. Default metodlar ile lambda'yı kullanmak daha kolay hale geldi. Açıklaması şöyle
one of the major reasons for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.
Örnek
Collection sınıfındanki forEach default metodu şöyledir. Böylece lambda ile çok kolay kullanılabilir.
default void forEach(Consumer<? super T> action) {
  Objects.requireNonNull(action);
  for (T t : this) {
    action.accept(t);
  }
}
3. Kalıtım Konusu

a. You get what is in base interface
Eğer birbirinden kalıtan iki arayüz varsa en üstteki arayüzün default metodunu kullanabiliriz.

b. You may override a default method
Eğer birbirinden kalıtan iki arayüz varsa, kalıtan arayüz ata arayüzden gelen default metodu override edebilir.

c. Ata sınıftaki metod arayüzdeki default metodu ezer
Ata sınıf ve ata arayüzde aynı metod varsa, ata sınıftaki seçilir.

d. Ata sınıftaki metod yoksa ve iki ata arayüzdeki aynı default metod varsa
Bu durumda kod derlenmez. İllaki bir arayüzdeki default metodu çağırmak istersek şöyle yaparız.
MyInterface.super.foo ();

Örnek
Elimizde şöyle bir kod olsun.
interface B {
    default void x() { System.out.println("B::x"); }
}

interface C {
    default void x() { System.out.println("C::x"); }
}

class D implements B, C {}
Şu hatayı alırız.
D inherits unrelated defaults for x() from types B and C
Aşmak için şöyle yaparız. Burada 2. maddedeki gibi default metod override ediliyor.
class D implements B, C {
  @Override
  public void x() {
    B.super.x();       // Note explicit interface names
    C.super.x();
  }
}