Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
How It Works
Before Java 8, it was impossible to include code implementation within a Java interface. An interface is a reference type in Java, similar to a class. However, its original intent only allowed abstract methods, constants, static methods, and nested types. Therefore, classes that implemented an interface must implement each abstract method. In Java 8, that restriction was lifted, and it became possible to include method implementations Chapter 2 enhanCements from Java 9 through Java 17 68 in the form of default methods. A default method can contain an implementation in the interface, or an implementing class could override its implementation to do a complete override or add an extension. Hence, the term default method, meaning that the default method implementation resides in the interface if the implementation class does not provide one. Private methods were not allowed in interfaces. Situations have arisen by which multiple default methods within an interface may contain similar code. This code can now be encapsulated within a private method implementation within the interface. The private method implementation cannot be used outside of the interface. In the solution to this recipe, the volumeCalc() method returns the calculated volume of a square or rectangular swimming pool using a standard formula. Each of the default methods within the interface can utilize the volumeCalc() method to find the volume. However, the volumeCalc() method is not available outside the interface. This seems controversial, as interfaces were originally intended for field and method declarations only. It can also be argued that copying the same code throughout several default method implementations would be a bad practice. Take it as you will, this feature makes it easier to reuse code within an interface, thereby reducing the chance for errors and making maintenance much easier. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling