ArticleslgStudy

science

Interface (Java)

Interface (Java) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Interface (Java) rather than just read about it. In short: An interface in the Java programming language is an abstract type that is used to declare a behavior that classes must implement. They are similar to protocols.

Key takeaways

  • Interface (Java) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Interface (Java) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Interface (Java) from memory before moving on to harder problems.

Reference excerpt

An interface in the Java programming language is an abstract type that is used to declare a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final). All methods of an Interface do not contain implementation (method bodies) as of all versions below Java 8. Starting with Java 8, default and static methods may have implementation in the interface definition. Then, in Java 9, private and private static methods were added. At present, a Java interface can have up to six different types. Such interfaces exist in C#. Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an abstract class. Object references in Java may be specified to be of an interface type; in each case, they must either be null, or be bound to an object that implements the interface. One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java must have exactly one base class, the only exception being java.lang.Object (the root class of the Java type system); multiple inheritance of classes is not allowed. However, an interface may inherit multiple interfaces and a class may implement multiple interfaces.

Overview Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship. For instance, a human and a parrot can both whistle; however, it would not make sense to represent Humans and Parrots as subclasses of a Whistler class. Rather they most likely be subclasses of an Animal class (likely with intermediate classes), but both would implement the Whistler interface. Another use of interfaces is being able to use an object without knowing its type of class, but rather only that it implements a certain interface. For instance, if one were annoyed by a whistling noise, one may not know whether it is a human or a parrot, because all that could be determined is that a whistler is whistling. The call whistler.whistle() will call the implemented method whistle of object whistler no matter what class it has, provided it implements Whistler. In a more practical example, a sorting algorithm may expect an object of type Comparable. Thus, without knowing the specific type, it knows that objects of that type can somehow be sorted. For example:

An interface:

declares only method headers and public constants. cannot be instantiated. can be implemented by a class. cannot extend a class. can extend several other interfaces.

Usage

Defining an interface Interfaces are defined with the following syntax (compare to Java's class definition):

[visibility] interface InterfaceName [extends other interfaces] { constant declarations abstract method declarations static method declarations }

Example:

The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required. Since the interface specifies a set of exposed behaviors, all methods are implicitly public. Thus, a simple interface may be

The member type declarations in an interface are implicitly static, final and public, but otherwise they can be any type of class or interface.

Implementing interfaces in a class The syntax for implementing an interface uses this formula:

... implements InterfaceName[, another interface, another, ...] ...

Classes may implement an interface. For example:

If a class implements an interface and does not implement all its methods, it must be marked as abstract. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods, though if any of the abstract class' subclasses do not implement all interface methods, the subclass itself must be marked again as abstract. Classes can implement multiple interfaces:

Interfaces can share common class methods:

However a given class cannot implement the same or a similar interface multiple times:

Interfaces are commonly used in the Java language for callbacks, as Java does not allow multiple inheritance of classes, nor does it allow the passing of methods (procedures) as arguments. Therefore, in order to pass a method as a parameter to a target method, current practice is to define and pass a reference to an interface as a means of supplying the signature and address of the parameter method to the target method rather than defining multiple variants of the target method to accommodate each possible calling class.

Subinterfaces Interfaces can extend several other interfaces, using the same formula as described below. For example,

is legal and defines a subinterface. It allows multiple inheritance, unlike classes. Predator and Venomous may possibly define or inherit methods with the same signature, say kill(Prey p). When a class implements VenomousPredator it will implement both methods simultaneously.

Examples Some common Java interfaces are:

Comparable has the method compareTo, which is used to describe two objects as equal, or to indicate one is greater than the other. Generics allow implementing classes to specify which class instances can be compared to them. Serializable is a marker interface with no methods or fields - it has an empty body. It is used to indicate that a class can be serialized. Its Javadoc describes how it should function, although nothing is programmatically enforced

See also Interface (object-oriented programming) Mixin Trait (computer programming)

Citations

References Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.

External links What Is an Interface?

Worked examples

Example 1 — a first encounter with Interface (Java)

Start with the simplest possible case. Write down what Interface (Java) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Interface (Java) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Interface (Java) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Interface (Java)

In research
Interface (Java) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Interface (Java) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Interface (Java) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Interfaces, Java (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for Interface (Java) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Interface (Java)” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Interface (Java) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Interface (Java) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Interface (Java) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Interface (Java) in simple terms?

An interface in the Java programming language is an abstract type that is used to declare a behavior that classes must implement. They are similar to protocols.

Why does Interface (Java) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Interface (Java)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Interface (Java).

Tags

  • Interfaces
  • Java (programming language)

Keep exploring