interface example in java

Cheney 118 Published: 07/17/2024

interface example in java

I'm happy to help! Here's an example of an interface in Java that demonstrates how you can define a method signature with no implementation, as well as several other important concepts.

public interface Vehicle {

// This is the abstract method declaration,

// which means it has no implementation.

String getMake();

// This method takes an argument and returns a value.

int calculateSpeed(int speed);

// This method does not take any arguments and

// returns void, indicating that it does nothing.

default void honkHorn() {

System.out.println("Beep Beep!");

}

}

In the above code:

We define an interface named Vehicle. The getMake() method is declared but not implemented. This means you have to override it in any class that implements this interface. The calculateSpeed(int speed) method takes a single integer argument and returns an integer value. It does not necessarily have anything to do with the make or model of a vehicle, but we can call it whenever we need to calculate a speed value based on some input. The honkHorn() method is also declared in this interface, which is a good practice because even if a class implements an interface doesn't mean that every single instance of that class has to implement the default behavior. For example, you can create a new vehicle that honks its horn differently than what's provided by the Vehicle interface.

Here's how we could use this interface with a concrete class:

public class Car implements Vehicle {

private String make;

private int speed;

public Car(String make) {

this.make = make;

}

@Override

public String getMake() {

return make;

}

@Override

public int calculateSpeed(int input) {

// Assuming we're calculating the speed based on some algorithm.

return (input * 2) + 30;

}

}

In this code, the Car class implements the Vehicle interface. It overrides the getMake() and calculateSpeed() methods to provide its own implementation of these behaviors.

Now let's see how we can use these classes:

public class Main {

public static void main(String[] args) {

Car myCar = new Car("Toyota");

System.out.println(myCar.getMake()); // prints "Toyota"

int speed = myCar.calculateSpeed(100);

System.out.println(speed); // calculates the speed based on the input

myCar.honkHorn(); // outputs "Beep Beep!"

}

}

In this example, we create an instance of Car named myCar, then use it to print its make and calculate a new speed value. Finally, we call honkHorn() on the vehicle (which is actually implemented by the default method in the interface), which outputs "Beep Beep!".

I hope this helps you understand Java interfaces better!

types of interface in java

I'm happy to help!

In Java, there are several types of interfaces that provide a way to define a contract or a blueprint for classes. Here's an overview:

Interface: An interface is a abstract class that can have only constant and abstract methods. It serves as a template for defining the behavior of objects. Interfaces cannot be instantiated on their own; they can only be implemented by classes.

Example: public interface Printable { void print(); }

Abstract Class: An abstract class, like an interface, is a blueprint that defines a template for its subclasses. However, an abstract class can have both abstract and concrete methods. Abstract classes are more powerful than interfaces as they allow for some implementation while still requiring subclasses to implement the abstract methods.

Example: public abstract class Animal { public abstract void makeSound(); }

Functional Interface: A functional interface is a special type of interface that has exactly one abstract method (SAM). These interfaces are often used with lambda expressions and method references.

Example: @FunctionalInterface public interface Printable { void print(String message); }

Marker Interface: A marker interface is an interface that does not define any methods but is used to identify a class's role or characteristic. This type of interface is also known as a "tag" or "marker".

Example: public interface Serializable {} // indicates that the class can be serialized

Dynamic Method Invocation (DMI) Interface: A DMI interface allows for dynamic method invocation, which enables you to invoke methods at runtime based on their names.

Example: public interface DynamicMethod { void invoke(String method, Object[] args); }

Callback Interface: A callback interface defines a method that is invoked by another class or object.

Example: public interface ActionListener { void actionPerformed(ActionEvent e); } // invoked when an event occurs

Event Handling Interface: An event handling interface is used for handling events in Java, such as mouse clicks or key presses.

Example: public interface MouseListener { void mouseClicked(MouseEvent e); }

Error Handler Interface: An error handler interface defines a method that handles errors or exceptions.

Example: public interface ErrorHandler { void handleError(Throwable t); }

Observer Pattern Interface: The observer pattern interface is used for defining the contract between an observable object and its observers.

Example: public interface Observer { void update(Observable o, Object arg); }

These are just a few examples of the different types of interfaces in Java. Interfaces play a crucial role in defining the behavior of classes and enabling polymorphism and multiple inheritance.