1. Home
  2. Interview Questions
  3. Java
  4. How do you implement a generic interface and its functions i…

Try answering first

In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.

1 Answer

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best answer ·

Short answer: To implement a generic interface, you define a class that specifies the type parameter when using the interface.

Detailed explanation

Generic interfaces provide a way to define methods and properties that can operate on a specified type without needing to know the exact type until runtime. This allows for greater flexibility and reusability of code. To implement a generic interface, a class must declare the interface with a specific type or a type parameter.

Here's a general structure for creating and implementing a generic interface:

  1. Define the generic interface with a type parameter using syntax like interface MyGenericInterface<T> { ... }.
  2. Implement the interface in a class and specify the type parameter, e.g., class MyClass implements MyGenericInterface<String> { ... }.
  3. Within the implementing class, use the type parameter in methods to work with the provided type.

This approach allows for strong typing at compile time while maintaining flexibility.

Example

interface Container<T> {
    void add(T element);
    T get(int index);
}

class Box<T> implements Container<T> {
    private List<T> elements = new ArrayList<>();

    @Override
    public void add(T element) {
        elements.add(element);
    }

    @Override
    public T get(int index) {
        return elements.get(index);
    }
}

// Using the Box class:
// Box<String> box = new Box<>();
// box.add("Hello");
// System.out.println(box.get(0)); // prints "Hello"

Common mistakes

  • Not specifying type parameters when implementing the interface, which can lead to raw types and warnings.
  • Assuming all methods can take any type without considering type bounds defined in the generic interface.
  • Not overriding methods correctly, which can lead to compile-time errors or inconsistent method behavior.

Follow-up questions

  • Can you explain what a bounded type parameter is?
  • What is the difference between a generic interface and a generic class?
  • How do generic interfaces enhance polymorphism in Java?

Interview tip: Familiarize yourself with how generics are used in Java's Collections Framework.

Reviewed by the Interview Questions editorial team · Last updated

How well did you know this?

Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.

Your Answer

More hard Java questions

All hard questions →

Related Questions

‹
Q13 of 18 · Java
Next ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA