Introduction to Interfaces in Eiffel Programming Language
Hello, and welcome to this blog about interfaces in the Eiffel programming language!
If you’re new to Eiffel or if you want to refresh your knowledge, then you are on the right page. Interfaces in programming define the required behavior for classes without implementation of this behavior. In Eiffel, these are known as “deferred classes.” Deferred classes in Eiffel play the role of templates that concrete classes have to follow in a way, guaranteeing homogeneity and adherence to some design. Interfaces define what a class should do, leaving the implementation details of how it should do it to the classes implementing them. It enhances not only code reusability and flexibility but also the whole maintainability and readability of code.In this post, we will consider in detail the Eiffel programming language interfaces. We shall consider the Eiffel deferred classes with examples of Eiffel interface and take you through an Eiffel deferred class tutorial. You will also learn how to implement interfaces in Eiffel and how Eiffel polymorphism and interfaces work together. Let us delve deeper into how Eiffel’s deferred classes work and why they are
What is Interfaces in Eiffel Programming Language?
Interfaces in the Eiffel programming language are an important concept, usually referred to as “deferred classes,” providing an abstraction mechanism that defines the expected behavior of classes without implementation of the behavior itself. Such an abstraction mechanism ensures conformance of different classes to a particular contract or design and enhances consistency, reusability, and maintainability in the code base.
Understanding Deferred Classes
Deferred classes in Eiffel are templates for other classes. They define a set of features which any concrete class has to implement, hence inheriting from them. The purpose behind the deferred classes is to define what operations are to be performed on any given kind of object without explaining how these operations should be conducted.
Structure of Deferred Classes
Declare a deferred class by placing the keyword deferred
before the class definition. This class includes one or more deferred features, defined as method declarations without implementations. Any class inheriting from a deferred class must provide concrete implementations for all deferred features.
Key characteristics of deferred classes:
- Abstractness: Deferred classes specify the types of operations on an object without detailing their implementation.
- Consistency: Deferred classes ensure that different classes adhere to the same set of behaviors through a common interface.
- Polymorphism: Deferred classes allow for polymorphic behavior; that is, uniform treatment of objects of different classes based on their common interface.
Example of a Deferred Class
deferred class SHAPE
feature
area: REAL
deferred
end
perimeter: REAL
deferred
end
end
In this example, SHAPE
is a deferred class with two deferred features: area
and perimeter
. These features are declared but not implemented, serving as placeholders for the actual behavior that must be defined by subclasses.
Implementing Deferred Classes
When a class inherits from a deferred class, it must implement all the deferred features to become effective. This ensures that any concrete class derived from a deferred class adheres to the specified interface.
Example of Implementing a Deferred Class
class CIRCLE
inherit
SHAPE
feature
radius: REAL
area: REAL
do
Result := 3.14 * radius * radius
end
perimeter: REAL
do
Result := 2 * 3.14 * radius
end
end
In this example, the CIRCLE
class inherits from SHAPE
and provides concrete implementations for the area
and perimeter
features, making it an effective class that can be instantiated.
Advantages of Interfaces in Eiffel Programming Language
Interfaces, known as deferred classes in the Eiffel programming language, offer several advantages that enhance the strength, flexibility, and maintainability of software systems. This paper highlights the core benefits that deferred classes bring to Eiffel, helping developers create better-designed and more effective software.
1. Abstraction
Eiffel’s deferred classes offer high abstraction by specifying the operations a class must support without detailing how to perform them. This approach allows you to focus on designing the interface while letting the classes handle the implementation details.
2. Polymorphism
Interfaces allow polymorphism. Therefore, uniform treatment of various class objects can be achieved based on the common interface. It can also be pretty useful while writing flexible and reusable code.
3. Homogeneity
Deferred classes ensure homogeneity. A common interface is enforced to be implemented by all concrete classes. This makes the code way more predictable and readable.
4. Reusability
Deferred classes enable code reuse by allowing the same interface to be implemented across different classes. This approach reduces code redundancy and enhances modularity.
5. Testability
Interfaces provide good unit testing since we can use mock objects which implement the same interface. This allows isolating parts of the system independently for testing.
Disadvantages of Interfaces in Eiffel Programming Language
Even though interfaces in Eiffel—the programming language—deferred classes have a lot of advantages, they also have associated disadvantages. Knowing the different disadvantages would help one make a conscious decision on where and how to apply deferred classes in software design.
1. Higher Complexity
Introducing deferred classes is one more degree of complexity added to the design of your software. Developers should design carefully the interfaces and ensure that all implementing classes adhere. This may make it rather tough to understand the codebase for new developers or developers who are not familiar with such design.
2. Performance Overhead
The extra layer of abstraction added by deferred classes may add some performance overhead. Calling methods through interfaces can be less efficient than direct calls, as they may include additional indirection.
3. Rigidity in Design
While deferred classes encourage consistency, they also have a tendency to produce rigidity in the design. Once an interface is defined and it is implemented by several classes, it is difficult to change it as any changes will have to be propagated throughout the code.
4. Possibility of Over-Engineering
Too many deferred classes may give way to over-engineering a design—making it too complex without offering any considerable added advantage. This finally results in a bloated code with unwanted abstractions.
5. Single Inheritance Not Flexible
Eiffel’s single inheritance model limits the flexibility of using deferred classes. When a class needs to inherit behaviors from multiple interfaces, designing the inheritance hierarchy can be challenging. In such cases, developers might use other design patterns, such as composition, to achieve the desired functionality.
6. Learning Curve
The understanding of the concept of deferred classes and how to use them properly may be difficult for a developer new to Eiffel or to the very idea of interfaces. This could reduce the onboarding speed and increase the time before productivity is achieved with the language.
7. Potential for Code Duplication
Deferred classes can result in code duplication if they are not used carefully. This normally occurs for several classes whose implementation only slightly differs in behavior. This will require the maintenance of such similar code at different places, hence increasing the maintenance burden.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.