Computer Science
Java Interfaces
Java interfaces are a way to define a set of methods that a class must implement. They allow for multiple classes to implement the same interface, providing a way to achieve polymorphism and abstraction. Interfaces are declared using the "interface" keyword in Java.
Written by Perlego with AI-assistance
Related key terms
1 of 5
5 Key excerpts on "Java Interfaces"
- eBook - PDF
- Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser(Authors)
- 2014(Publication Date)
- Wiley(Publisher)
2.3.1 Interfaces in Java The main structural element in Java that enforces an API is an interface. An in- terface is a collection of method declarations with no data and no bodies. That is, the methods of an interface are always empty; they are simply method signatures. Interfaces do not have constructors and they cannot be directly instantiated. When a class implements an interface, it must implement all of the methods declared in the interface. In this way, interfaces enforce requirements that an im- plementing class has methods with certain specified signatures. Suppose, for example, that we want to create an inventory of antiques we own, categorized as objects of various types and with various properties. We might, for instance, wish to identify some of our objects as sellable, in which case they could implement the Sellable interface shown in Code Fragment 2.6. We can then define a concrete class, Photograph, shown in Code Fragment 2.7, that implements the Sellable interface, indicating that we would be willing to sell any of our Photograph objects. This class defines an object that implements each of the methods of the Sellable interface, as required. In addition, it adds a method, isColor, which is specialized for Photograph objects. Another kind of object in our collection might be something we could transport. For such objects, we define the interface shown in Code Fragment 2.8. 2.3. Interfaces and Abstract Classes 69 1 /** Interface for objects that can be sold. */ 2 public interface Sellable { 3 4 /** Returns a description of the object. */ 5 public String description( ); 6 7 /** Returns the list price in cents. */ 8 public int listPrice( ); 9 10 /** Returns the lowest price in cents we will accept. */ 11 public int lowestPrice(); 12 } Code Fragment 2.6: Interface Sellable. - eBook - PDF
Data Structures
Abstraction and Design Using Java
- Elliot B. Koffman, Paul A. T. Wolfgang(Authors)
- 2021(Publication Date)
- Wiley(Publisher)
Interfaces A Java interface is a way to specify or describe an ADT to an applications programmer. An interface is like a contract that tells the applications programmer precisely what methods are available and describes the operations they perform. It also tells the applications programmer 1.1 Abstract Data Types (ADTs), Interfaces, and the Java API 3 what arguments, if any, must be passed to each method and what result the method will return. Of course, in order to make use of these methods, someone else must have written a class that implements the interface by providing the code for these methods. The interface tells the coder precisely what methods must be written, but it does not provide a detailed algorithm or prescription for how to write them. The coder must “program to the interface,” which means he or she must develop the methods described in the interface without variation. If each coder does this job well, that ensures that other programmers can use the completed class exactly as it is written, without needing to know the details of how it was coded. There may be more than one way to implement the methods; hence, several classes may implement the interface, but each must satisfy the contract. One class may be more efficient than the others at performing certain kinds of operations (e.g., retrieving information from a database), so that class will be used if retrieval operations are more likely in a particular application. The important point is that the particular implementation that is used will not affect other classes that interact with it because every implementation satisfies the contract. Besides providing the complete definition (implementation) of all methods declared in the interface, each implementer of an interface may declare data fields and define other methods not in the interface, including constructors. An interface cannot contain constructors because it cannot be instantiated—that is, one cannot create objects, or instances, of it. - eBook - PDF
- John C. Mitchell(Author)
- 2002(Publication Date)
- Cambridge University Press(Publisher)
} 13.2 Java Classes and Inheritance 395 Java also has a “pure abstract” form of class called an interface. An interface is defined in a manner similar to that of a class, except that all interface members must be constants or abstract methods. An interface has no direct implementa-tion, but classes may be declared to implement an interface. In addition, an in-terface may be declared as an extension of another, providing a form of interface inheritance. One reason that Java programmers use interfaces instead of pure abstract classes when a concept is being defined but not implemented is that Java allows a single class to implement several interfaces, whereas a class can have only one superclass. The following interfaces and class illustrate this possibility. The Shape interface identifies some properties of simple geometric shapes, namely, each has a center point and a rotate method. Drawable similarly identifies properties of objects that can be displayed on a screen. If circles are geometric shapes that can be displayed on a screen, then the Circle class can be declared to implement both Shape and Drawable , as subsequently shown. Interfaces are often used as the type of argument to a method. For example, if the windows system has a method for drawing items on a screen, then the argument type of this method could be Drawable , allowing every object that implements the Drawable interface to be displayed: interface Shape { public Point center(); public void rotate(float degrees); } interface Drawable { public void setColor(Color c); public void draw(); } class Circle implements Shape, Drawable { // does not inherit any implementation // but must define Shape, Drawable methods } Unlike C++ multiple inheritance (discussed in Section 12.5 ), there is no name clash problem for Java Interfaces. More specifically, suppose that the preceding two inter-faces Shape and Circle also both define a Size method. - eBook - PDF
Brief Java
Early Objects
- Cay S. Horstmann(Author)
- 2019(Publication Date)
- Wiley(Publisher)
An interface describes the behavior that an implementation should supply. Prior to Java 8, an interface could not provide any implementation. Now, it is possible to supply a default implementation, and the distinction between interfaces and abstract classes (see Special Topic 9.3) has become more subtle. The significant difference that remains is that an interface type has no state (that is, no instance variables). 346 Chapter 10 Interfaces Generally, you will develop interfaces when you have code that processes objects of different classes in a common way. For example, a drawing program might have different objects that can be drawn, such as lines, images, text, and so on. In this situ- ation, a Drawable interface with a draw method will be useful. Another example is a traffic simulation that models the movement of people, cars, dogs, balls, and so on. In this example, you might create a Moveable interface with methods move and getPosition. Common Error 10.1 Forgetting to Declare Implementing Methods as Public The methods in an interface are not declared as public, because they are public by default. However, the methods in a class are not public by default—their default access level is “pack- age” access, which we discussed in Chapter 8. It is a common error to forget the public reserved word when declaring a method from an interface: public class BankAccount implements Measurable { . . . double getMeasure() // Oops—should be public { return balance; } } Then the compiler complains that the method has a weaker access level, namely package access instead of public access. The remedy is to declare the method as public. Common Error 10.2 Trying to Instantiate an Interface You can declare variables whose type is an interface, for example: Measurable meas; However, you can never construct an object of an interface type: Measurable meas = new Measurable(); // Error Interfaces aren’t classes. - eBook - PDF
- Scott Selikoff, Jeanne Boyarsky(Authors)
- 2022(Publication Date)
- Sybex(Publisher)
This applies to classes, enums, records, and so on. Also, remember that a top-level type can only be declared with public or package access. Another top-level type available in Java is annotations. Knowing how to create a custom annotation can be a useful skill in practice, although it is not required for the exam. You should still know how to use certain anno-tations for the exam, such as @Override . Implementing Interfaces In Chapter 6, you learned about abstract classes, specifically how to create and extend one. Since classes can only extend one class, they had limited use for inheritance. On the other hand, a class may implement any number of interfaces. An interface is an abstract data type that declares a list of abstract methods that any class implementing the interface must provide. Over time, the precise definition of an interface has changed, as new method types are now supported. In this chapter, we start with a rudimentary definition of an interface and expand it to cover all of the supported members. Declaring and Using an Interface In Java, an interface is defined with the interface keyword, analogous to the class keyword used when defining a class. Refer to Figure 7.1 for a proper interface declaration. Implementing Interfaces 347 In Figure 7.1, our interface declaration includes an abstract method and a constant vari-able. Interface variables are referred to as constants because they are assumed to be public , static , and final . They are initialized with a constant value when they are declared. Since they are public and static , they can be used outside the interface declaration without requiring an instance of the interface. Figure 7.1 also includes an abstract method that, like an interface variable, is assumed to be public . For brevity, we often say “an instance of an interface” in this chapter to mean an instance of a class that implements the interface.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.




