Interface
Interface
A contract that defines the methods a class must implement. Used to implement polymorphism in Java, TypeScript, and Go.
Interface
A contract that defines the methods a class must implement. Used to implement polymorphism in Java, TypeScript, and Go.
An interface is a contract that defines the method signatures that a class or object must implement. It specifies only "what must be done" without implementation details, allowing different implementations to be used in the same way. This is the core mechanism of polymorphism.
In Java, an interface is declared with the interface keyword, and a class implements it with implements. All methods are public abstract by default, and since Java 8, interfaces can also have default and static methods. Because a single class can implement multiple interfaces, interfaces serve as an alternative to multiple inheritance.
In TypeScript, an interface defines the structure (shape) of an object. It provides type safety by specifying properties, methods, and index signatures. In particular, because TypeScript uses duck typing (structural typing), a value can be used as that interface type if its structure matches, even without an explicit implementation declaration.
In Go, interfaces are implemented implicitly. A type that has a specific set of methods automatically satisfies the corresponding interface. The empty interface (interface{}) can accept any type, so it also plays the role of generics. This approach naturally encourages loose coupling.
// TypeScript 인터페이스 예제
// 기본 인터페이스 정의
interface User {
id: number;
name: string;
email: string;
createdAt?: Date; // 선택적 프로퍼티
}
// 메서드가 있는 인터페이스
interface Repository<T> {
findById(id: number): Promise<T | null>;
findAll(): Promise<T[]>;
create(data: Omit<T, 'id'>): Promise<T>;
update(id: number, data: Partial<T>): Promise<T>;
delete(id: number): Promise<boolean>;
}
// 인터페이스 구현
class UserRepository implements Repository<User> {
private users: User[] = [];
async findById(id: number): Promise<User | null> {
return this.users.find(u => u.id === id) || null;
}
async findAll(): Promise<User[]> {
return [...this.users];
}
async create(data: Omit<User, 'id'>): Promise<User> {
const user = { id: Date.now(), ...data };
this.users.push(user);
return user;
}
async update(id: number, data: Partial<User>): Promise<User> {
const index = this.users.findIndex(u => u.id === id);
this.users[index] = { ...this.users[index], ...data };
return this.users[index];
}
async delete(id: number): Promise<boolean> {
const index = this.users.findIndex(u => u.id === id);
if (index !== -1) {
this.users.splice(index, 1);
return true;
}
return false;
}
}
Architect: "We need to replace the payment module with an external service, but it looks like we'll have to modify a lot of the existing code."
Tech Lead: "What if we define a PaymentGateway interface and abstract it? Then we only need to swap the implementation, and the client code stays the same."
Architect: "Good idea. If we swap the implementation through dependency injection, we can easily plug in a Mock when testing, too."
Tech Lead: "Right. Depending on the interface gives you loose coupling, which also improves extensibility."
Interviewer: "What is the difference between an abstract class and an interface?"
Candidate: "An abstract class can have state (fields) and implemented methods, and only supports single inheritance. An interface is a pure contract and allows multiple implementation. Since Java 8, interfaces can also have default methods, but their purpose is different."
Interviewer: "When do you use an interface?"
Candidate: "I use an interface when unrelated classes need to exhibit the same behavior, or when multiple types are needed. If there is an inheritance relationship and common implementation needs to be shared, an abstract class is more suitable."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Reviewer: "It depends directly on a concrete class. That looks like it would be hard to test."
Author: "How could I improve it?"
Reviewer: "Extract an interface and invert the dependency. If the constructor accepts the interface type, you can inject a Mock object during testing."
Author: "So you mean applying the D of SOLID (the Dependency Inversion Principle). I'll refactor it."