Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet99/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   95   96   97   98   99   100   101   102   ...   245
Bog'liq
Java 17 Recipes

 Problem
You want to create a set of method signatures and fields that can be used as a common 
template to expose the methods and fields that a class implements.
 Solution
Generate a Java interface to declare each field and method that a class must implement. 
Such an interface can then be implemented by a class and represent an object type. 
The following code is an interface that declares the methods that the Team object must 
implement.
public interface TeamType {
void setPlayers(List
players);
void setName(String name);
void setCity(String city);
String getFullName();
}
All the methods in the interface are implicitly abstract. That is, only a method 
signature is provided. It is also possible to include static final field declarations in an 
interface.
 How It Works
A Java interface is a construct that defines the structures, be it fields or methods 
that a class must implement. In most cases, interfaces do not include method 
implementations; rather, they only include method signatures. Interfaces can include 
variables that are implicitly static and final.
The interface does not include any constant field declarations in the solution to this 
recipe. However, it includes four method signatures. All the method signatures have 
no access modifier specified because all declarations within an interface are implicitly 
public. Interfaces expose a set of functionalities; therefore, all methods exposed within 
an interface must be implicitly public. Any class that implements an interface must 
Chapter 5 ObjeCt-Oriented java


169
implement any method signatures declared in the interface, except default methods and 
abstract classes, in which case an interface may leave the implementation for one of its 
subclasses.
While the Java language does not allow multiple inheritance, a Java class can 
implement multiple interfaces, allowing for a controlled form of multiple inheritance. 
Abstract classes can also implement interfaces. The following code demonstrates a 
class implementing an interface: the Team object declaration implements the TeamType 
interface.
public class Team implements TeamType {
private List
players;
private String name;
private String city;
/**
* @return the players
*/
public List
getPlayers() {
return players;
}
/**
* @param players the players to set
*/
@Override
public void setPlayers(List
players) {
this.players = players;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
Chapter 5 ObjeCt-Oriented java


170
*/
@Override
public void setName(String name) {
this.name = name;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
@Override
public void setCity(String city) {
this.city = city;
}
public String getFullName() {
return this.name + " - " + this.city;
}
}
Interfaces can declare a type for an object. Any object declared to have an interface 
type must adhere to all the implementations declared in the interface unless a default 
implementation exists. For instance, the following field declaration defines an object 
containing all the properties declared within the TeamType interface.
TeamType team;
Interfaces can also extend other interfaces (thus the same type of theory provided 
by multiple inheritance). However, because no method implementation is present in an 
interface, it is much safer to implement multiple interfaces in a Java class than extending 
multiple classes in C++.
Chapter 5 ObjeCt-Oriented java


171
Interfaces are some of the single most important constructs of the Java language. 
They provide the interfaces between the user and the class implementations. Although 
it is possible to create entire applications without using interfaces, they help promote 
object orientation and hide method implementations from other classes.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   95   96   97   98   99   100   101   102   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling