- Asked 2/7
- 2072 Asoj · 5 marks
- 2073 Bhadra · 6 marks
Interface vs implementation
Short answer
Interface defines what operations a class provides (signatures, contracts) without revealing how they work. Implementation is the hidden code and data that realize those operations. Separating them enables encapsulation and loose coupling: clients depend only on the stable interface, so implementations can change freely.
Explain the concept of interface and implementation in object oriented design and implementation. (How interface differ from implementation? Explain the concept of interface and implementation in any object oriented programming languages.)
Concept
- Interface: the what, the public face of a class or component. It declares the set of operations (names, parameters, return types and contract) that other objects can use. It says nothing about how they work. In UML it is a classifier with
<<interface>>; in Java it is theinterfaceconstruct. - Implementation: the how, the actual code and data (private fields, algorithms) that carry out the operations. It is hidden from the clients.
Separating them is the basis of encapsulation, abstraction and loose coupling: clients depend only on the interface, so the implementation can be changed or replaced without changing the clients.
Client ----uses----> <<interface>>
Shape
+ area()
^ ^
: : (realization)
Circle Rectangle
(implementations)
Difference
| Point | Interface | Implementation |
|---|---|---|
| Describes | What can be done | How it is done |
| Content | Operation signatures (and constants) | Code, attributes, algorithms |
| Visibility | Public | Mostly private/hidden |
| Instances | Cannot be created directly | Concrete classes create objects |
| Change | Should be stable | Can change freely |
| Multiplicity | A class can implement many interfaces, and one interface can have many implementations | Each implementation is one concrete class |
Example in Java
interface Shape { // interface
double area();
}
class Circle implements Shape { // implementation 1
private double r;
Circle(double r) { this.r = r; }
public double area() { return Math.PI * r * r; }
}
class Rectangle implements Shape { // implementation 2
private double w, h;
Rectangle(double w, double h) { this.w = w; this.h = h; }
public double area() { return w * h; }
}
// Client depends only on the interface
Shape s = new Circle(2);
System.out.println(s.area());
In C++ the same is done with an abstract class that has pure virtual functions (virtual double area() = 0;), and in C# with the interface keyword.
✓ Answer checked by Claude Sonnet 5.5
Related questions
- Algorithmic vs object-oriented decomposition · Object Oriented Analysis and Design, chapter 1
- Object oriented system and core concepts · Object Oriented Analysis and Design, chapter 1
- Structural model vs implementation model · Object Oriented Analysis and Design, chapter 1
- Marina class diagram implementation code · Object Oriented Analysis and Design, chapter 4
- Conceptual, specification, implementation model classification · Object Oriented Analysis and Design, chapter 2
- Short note: Distributed system implementation · Object Oriented Analysis and Design, chapter 4
- Compare procedural and object oriented programming · Object Oriented Programming, chapter 1
- File system layout; interface vs implementation · Operating System, chapter 5
- Generations of programming languages · Computer Programming, chapter 1 · asked 4×
- Categories of programming languages · Computer Programming, chapter 1
All Object Oriented Analysis and Design chapter 4 questions →