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

3 Key excerpts on "Java Interfaces"

  • Learn Java 12 Programming
    eBook - ePub

    Learn Java 12 Programming

    A step-by-step guide to learning essential concepts in Java SE 10, 11, and 12

    implement an interface. Since an interface describes how you can interact with an object, but not how an object responds to the interaction, different objects can behave differently while implementing the same interface.
    In Java, a class can have only one direct parent but can implement many interfaces. The ability to behave as any of its ancestors and adhere to multiple interfaces is called polymorphism .
    In this chapter, we will look at these OOP concepts and how they are implemented in Java. The topics discussed include the following:
    • OOP concepts
    • Class
    • Interface
    • Overloading, overriding, and hiding
    • Final variable, method, and class
    • Polymorphism in action
    Passage contains an image

    OOP concepts

    As we have already stated in the introduction, the main OOP concepts are as follows:
    • Object/Class : It defines a state (data) and behavior (methods) and holds them together
    • Inheritance : It propagates behavior down the chain of classes connected via parent-child relationships
    • Abstraction/Interface : It describes how the object data and behavior can be accessed. It isolates (abstracts) an object's appearance from its implementations (behavior)
    • Encapsulation : It hides the state and details of the implementation
    • Polymorphism : It allows an object to assume an appearance of implemented interfaces and behave as any of the ancestor classes
    Passage contains an image

    Object/class

    In principle, you can create a very powerful application with minimal usage of classes and objects. It became even easier to do this after functional programming was added to Java 8, to JDK, which allowed you to pass around behavior as a function. Yet passing data (state) still requires classes/objects. This means that the position of Java as an OOP language remains intact.
    A class defines the types of all internal object properties that hold the object state. A class also defines object behavior expressed by the code of the methods. It is possible to have a class/object without a state or without a behavior. Java also has a provision for making the behavior accessible statically—without creating an object. But these possibilities are no more than just additions to the object/class concept that was introduced for keeping the state and behavior together.
  • Learn Java with Projects
    eBook - ePub

    Learn Java with Projects

    A concise practical guide to learning everything a Java professional really needs to know

    • Dr. Seán Kennedy, Maaike van Putten(Authors)
    • 2023(Publication Date)
    • Packt Publishing
      (Publisher)
    base type.
    A class that implements an interface signs a “contract” to provide code for each of the abstract methods (if any) in the interface. If there is an abstract method in the interface and the concrete, non-abstract class does not provide code implementation for it, the compiler complains. Therefore, interfaces are a great way of guaranteeing that certain methods will be present in a class. Variables in an interface are constants by default. These constants are available to implementing classes, but are read-only. We noted that multiple interface inheritance, where an interface can inherit from several other interfaces, is allowed. This contrasts with classes, be they abstract or concrete, where multiple inheritance is prohibited.
    In Java 8, default and static methods, both with code bodies, were introduced to interfaces. This was the first time code was allowed in interfaces. Regarding inheritance, default methods are inherited by implementing classes, whereas static methods are not. Thus, accessing both requires different syntaxes. As default methods are inherited, they can be overridden by implementing classes. Both types of methods, as with abstract methods, are public by default.
    Next, we saw how the compiler prevents us from experiencing the “Diamond of Death.” This issue could arise when two interfaces have the same default method name. A class that implements these two interfaces is forced to provide a custom implementation to avoid ambiguity. This led nicely to the syntax (using super ), which enables us the default methods in both interfaces and the custom (non-default) version in the class.
    Java 9 introduced private interface methods, which also have code bodies. They were introduced to reduce code duplication and improve encapsulation. We detailed an example where we refactored code by introducing private interface methods.
  • Expert Python Programming
    eBook - ePub

    Expert Python Programming

    Master Python by learning the best coding practices and advanced programming concepts, 4th Edition

    • Michał Jaworski, Tarek Ziadé(Authors)
    • 2021(Publication Date)
    • Packt Publishing
      (Publisher)
    In the spirit of the first meaning, the interface is a specific combination of symbols used to interact with the unit of code. The interface of a function, for instance, will be the name of that function, its input arguments, and the output it returns. The interface of an object will be all of its methods that can be invoked and all the attributes that can be accessed.
    Collections of units of code (functions, objects, classes) are often grouped into libraries. In Python, libraries take the form of modules and packages (collections of modules). They also have interfaces. Contents of modules and packages usually can be used in various combinations and you don't have to interact with all of their contents. That makes them programmable applications, and that's why interfaces of libraries are often referred to as Application Programming Interfaces (APIs ).
    This meaning of interface can be expanded to other elements of the computing world. Operating systems have interfaces in the form of filesystems and system calls. Web and remote services have interfaces in the form of communication protocols.
    The second meaning of interface can be understood as the formalization of the former. Here interface is understood as a contract that a specific element of the code declares to fulfill. Such a formal interface can be extracted from the implementation and can live as a standalone entity. This gives the possibility to build applications that depend on a specific interface but don't care about the actual implementation, as long as it exists and fulfills the contract.
    This formal meaning of interface can also be expanded to larger programming concepts:
    • Libraries: The C programming language defines the API of its standard library, also known as the ISO C Library . Unlike Python, the C standard library has numerous implementations. For Linux, the most common is probably the GNU C Library (glibc ), but it has alternatives like dietlibc or musl
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.