interface example in java
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 namedVehicle
. 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(); }
Example: public abstract class Animal { public abstract void makeSound(); }
Example: @FunctionalInterface public interface Printable { void print(String message); }
Example: public interface Serializable {} // indicates that the class can be serialized
Example: public interface DynamicMethod { void invoke(String method, Object[] args); }
Example: public interface ActionListener { void actionPerformed(ActionEvent e); } // invoked when an event occurs
Example: public interface MouseListener { void mouseClicked(MouseEvent e); }
Example: public interface ErrorHandler { void handleError(Throwable t); }
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.