Java 17 Recipes
-9. Interacting with a Class via Interfaces
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- How It Works
5-9. Interacting with a Class via Interfaces
Problem You have created a class that implements an interface or class type. You want to interact with the methods of that class by calling on methods declared within the interface rather than working directly with the class. Chapter 5 ObjeCt-Oriented java 179 Solution Declare a field of the same type as an interface. You can then assign classes that implement the interface to the field you had declared and call upon the methods declared in the interface to perform work. In the following example, a field is declared to be of type TeamType. Using the same classes from Recipe 5-8, you can see that the class Team implements the TeamType interface. The field that is created in the following example holds a reference to a new Team object. Because the Team class implements the TeamType interface, the methods that are exposed in the interface can be used. public class InterfaceTester { static TeamType team = new Team(); public static void main(String[] args){ team.setCity("SomeCity"); team.setName("SomeName"); team.setPlayers(null); System.out.println(team.getFullName()); } } Where the interface is public interface TeamType { void setPlayers(List players); void setName(String name); void setCity(String city); String getFullName(); } The resulting output is as follows. SomeName - SomeCity Chapter 5 ObjeCt-Oriented java 180 How It Works Interfaces are useful for many reasons. Two of the most important use cases for interfaces are conformity and abstraction. Interfaces define a model, and any class that implements the interface must conform to that model. Therefore, if a constant is defined within the interface, it is automatically available for use in the class. If there is a method defined within the interface, the class must implement that method unless a default implementation has been defined. Interfaces provide a nice way to allow classes to conform to a standard. Interfaces hide unnecessary information from any class that does not need to see it. Any method defined within the interface is made public and accessible to any class. As demonstrated in the solution to this recipe, an object was created and declared the type of an interface. The interface in the example, TeamType, only includes a small subset of available methods within the Team object. Therefore, the only methods that are accessible to any class working against an object declared to be of TeamType are the ones defined within the interface. The class using this interface type does not have access to any of the other methods or constants, nor does it need to. Interfaces are a great way for hiding logic that does not need to be used by other classes. Another great side effect: A class that implements an interface can be changed and recompiled without affecting code that works against the interface. However, if an interface is changed, there could be an effect on any classes that implement it. Therefore, if the getFullName() method implementation changes, any class coded against the TeamType interface is not affected because the interface is unchanged. The implementation changes behind the scenes, and any class working against the interface begins to use the new implementation without needing to know. Lastly, interfaces help to promote security. They hide implementation details of methods declared in an interface from any class that may call upon that method using the interface. As mentioned in the previous paragraph, if a class calls the getFullName() method against the TeamType interface, it does not need to know the implementation details of that method as long as a result is returned as expected. The older Enterprise JavaBean (EJB) model used interfaces for interacting with methods that performed database work. This model worked very well for hiding the details and logic that were not essential for use from other classes. Other frameworks use similar models, exposing functionality through Java interfaces. Interface use has proven to be a smart way to code software because it promotes reusability, flexibility, and security. Chapter 5 ObjeCt-Oriented java |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling