Lab: Interfaces#
Objective#
In this lab, we will explore the concept of interfaces. We’ll learn how to define and implement interfaces, ensuring our classes adhere to specified contracts.
Provided Code#
Carefully review the provided code. Notice how the interface IShape
defines a contract without specifying implementation details. The interface declares properties and methods that implementing classes must provide.
interface IShape
{
double Width { get; set; }
double Height { get; set; }
double Area { get; }
void Scale (double factor);
}
Instructions#
Step 1: Define classes that implement IShape
#
Write two classes called Rectangle
and Ellipse
that both implement the interface IShape
.
When you’re done, you should be able to run the following code:
IShape shape1 = new Rectangle() { Width=20, Height=10 };
IShape shape2 = new Ellipse() { Width=50, Height=20 };
Console.WriteLine(shape1.Area);
Console.WriteLine(shape2.Area);
shape1.Scale(0.5);
shape2.Scale(2);
Console.WriteLine(shape1.Area);
Console.WriteLine(shape2.Area);
200
3141.5926535897934
50
12566.370614359173
Important
Note how the compile-time type (i.e. the type “on the left”) of both types is IShape
.
We will create a new interface and implement it in different classes to observe interface functionality.
Step 2: Define an interface called IPositionable
#
Define an interface called IPositionable
that demands that its implementors define two publically gettable properties, of type int
, called X
and Y
Add to the Rectangle
and Circle
classes so that they implement the interface IPositionable
.
When you’re done you should be able to run the following code:
IPositionable shape1 = new Rectangle
{
Width = 20,
Height = 10,
X = 10,
Y = 20
};
IPositionable shape2 = new Ellipse
{
Width = 50,
Height = 20,
X = 5,
Y = 2
};
Console.WriteLine($"({shape1.X}, {shape1.Y})");
Console.WriteLine($"({shape2.X}, {shape2.Y})");
(10, 20)
(5, 2)