Java Reference
In-Depth Information
Needless to say that both types of methods can use their parameters.
throws clause.
You are not done with default methods yet. I will discuss their roles in inheritance shortly.
Both methods can have a
Nested Type Declarations
A nested type declaration in an interface defines a new reference type. You can declare a class, interface, enum, and
annotation as nested types. I have not discussed enum and annotation yet, so I will restrict the discussion to nested
interfaces and classes in this section. An interface/class declared inside an interface is called a nested interface/class.
An interface and a class define new reference types, so do a nested interface and a nested class. Sometimes a type
makes more sense as a nested type. Suppose you have an ATM interface and you want to define another interface called
ATMCard . The ATMCard interface can be defined as a top-level interface or a nested interface of ATM . Since an ATM card
is used with an ATM, it might make more sense to define ATMCard as a nested interface of the ATM interface. Since you
are defining ATMCard as a nested interface of ATM , you can also drop the “ ATM ” from its name and you can name it just
Card , as shown:
public interface ATM {
boolean login(int account) throws AccountNotFoundException;
boolean deposit(double amount);
boolean withdraw(double amount) throws InsufficientFundException;
double getBalance();
// Card is a nested interface. You can omit the keywors public and static.
public static interface Card {
String getNumber();
String getSecurityCode();
LocalDate getExpirationDate();
String getCardHolderName();
}
}
A nested interface is always accessed through its enclosing interface. In the above snippet of code, ATM is a
top-level interface (or simply an interface) and Card is a nested interface. The ATM interface is also called an enclosing
interface for the Card interface. The fully qualified name of the ATM interface is com.jdojo.interfaces.ATM ; the
fully qualified name of the nested Card interface is com.jdojo.interfaces.ATM.Card . All nested types are implicitly
public and static . The above code snippet used the keywords public and static to declare the ATMCard interface,
which are redundant.
You can also declare a nested class inside an interface. You may not understand the use of a nested class
described in this section until you know how to implement an interface, which is described in the next section. The
following discussion is included here to complete the discussion about the nested types of an interface. You may
revisit this section after reading about how to implement an interface in the next few sections.
It is not common to declare a nested class inside an interface. However, you should not be surprised if you find
an interface that declares a nested class as its member. What advantage does it offer to have a nested class inside
an interface? There is only one advantage of doing this and it is a better organization of related entities: interfaces
and classes. Suppose you want to develop a Job interface that will let the user submit a job to a job scheduler. The
following is the code for the Job interface:
public interface Job {
public void runJob();
}
 
Search WWH ::




Custom Search