Sobes.tech
Junior — Middle

How to implement inheritance of a universal type from an interface in a programming language?

sobes.tech AI

Answer from AI

In programming languages that support generics, such as Java or Kotlin, inheriting a generic type from an interface is done by specifying the interface with a type parameter when declaring the class.

Example in Java:

interface Repository<T> {
    void save(T item);
    T findById(int id);
}

class UserRepository implements Repository<User> {
    @Override
    public void save(User user) {
        // implementation for saving a user
    }

    @Override
    public User findById(int id) {
        // implementation for finding a user
        return new User();
    }
}

Here, the UserRepository class inherits the Repository interface with a specific type User.

If you want the class itself to be generic, the type parameter is passed further:

class GenericRepository<T> implements Repository<T> {
    @Override
    public void save(T item) { /* ... */ }
    @Override
    public T findById(int id) { /* ... */ return null; }
}

Thus, inheriting a generic type from an interface is achieved by specifying the type parameters when implementing the interface.

How to implement inheritance of a universal type from… - sobes.tech