Skip to content

Java Interfaces

An interface defines a set of methods that a class must implement, serving as a promise to provide certain functionality without specifying how it's done.it is fully abstract

Example:

java
// Define the interface
interface Animal {
    void sound(); // Method declaration without implementation
}

// Implement the interface in a class
class Dog implements Animal {
    @Override
    public void sound() {
        System.out.println("Woof! Woof!");
    }
}

// Implement the interface in another class
class Cat implements Animal {
    @Override
    public void sound() {
        System.out.println("Meow! Meow!");
    }
}

public class InterfaceClassExample {
    public static void main(String[] args) {
        Animal dog = new Dog();
        Animal cat = new Cat();

        dog.sound(); // Output: Woof! Woof!
        cat.sound(); // Output: Meow! Meow!
    }
}

Isn't that fascinating? Our Animal interface acts as a contract, requiring any implementing class to provide a sound() method. Both our Dog and Cat classes fulfill this contract by implementing the sound() method with their respective sounds. And when we call sound() on objects of these classes, Java faithfully executes their unique sounds.

But wait, there's more! Interface classes aren't just about method contracts; they're also about achieving flexibility and polymorphism in your code. Imagine having multiple classes implementing the same interface, each offering its unique behavior. With interfaces, you can treat these objects uniformly through the interface reference, yet dynamically invoke their individual methods based on their concrete types.

NOTE

When you define a method in Java, the compiler automatically throws in those "public" and "abstract" keywords for interface methods. And guess what? For data members, it's even more generous – it pops in "public," "static," and "final" keywords!

Let's expand our horizons with another example:

java
// Define another interface
interface Flying {
    void fly();
}

// Implement multiple interfaces in a class
class Bird implements Animal, Flying {
    @Override
    public void sound() {
        System.out.println("Chirp! Chirp!");
    }

    @Override
    public void fly() {
        System.out.println("I believe I can fly!");
    }
}

public class InterfaceClassExample {
    public static void main(String[] args) {
        Bird bird = new Bird();
        bird.sound(); // Output: Chirp! Chirp!
        bird.fly();   // Output: I believe I can fly!
    }
}

In this example, our Bird class implements both the Animal and Flying interfaces, showcasing the flexibility and power of interface classes. By implementing multiple interfaces, our class can exhibit behaviors characteristic of both animals and flying creatures.

Muti-Inheritance using interfaces

What exactly is multiple inheritance, you ask? Well, think of it as inheriting traits or behaviors from more than one parent. In Java, unlike some other programming languages, a class can only directly inherit from one superclass. However, fear not, for interfaces come to our rescue, offering a solution that allows a class to inherit from multiple sources.

So, how do interfaces make this magic happen? Picture an interface as a contract—a set of rules or behaviors that a class agrees to abide by. By implementing an interface, a class promises to provide implementations for all the methods declared within it. And here's the beauty: a Java class can implement multiple interfaces, effectively inheriting behaviors from all of them.

Example:

java
// Define two interfaces representing different types of employees
interface Employee {
    void work();
}

interface Manager {
    void manage();
}

// Create a class that implements both interfaces
class TeamLead implements Employee, Manager {
    public void work() {
        System.out.println("Team lead working diligently.");
    }

    public void manage() {
        System.out.println("Team lead managing the team.");
    }
}

public class MultipleInheritanceExample {
    public static void main(String[] args) {
        TeamLead teamLead = new TeamLead();
        teamLead.work();    // Output: Team lead working diligently.
        teamLead.manage();  // Output: Team lead managing the team.
    }
}

Marvelous, isn't it? Our TeamLead class implements both the Employee and Manager interfaces, inheriting the work() method from the Employee interface and the manage() method from the Manager interface. This allows our TeamLead to seamlessly fulfill the roles of both an employee and a manager, all thanks to the power of interfaces.

But wait, there's more! Interfaces also offer a way to achieve abstraction and polymorphism, allowing classes to be treated uniformly based on their common behaviors, regardless of their specific implementations.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.