Professional Documents
Culture Documents
08 Handout 1
08 Handout 1
Interfaces
Invoking Interface Members at the Object Level
An interface only contains the signatures of methods, properties, and
events as its members. It is the responsibility of the class implementing
the interface to provide the implementation of the members. After
defining an interface, it can be implemented in a class. This means that
the implementing class must implement all the declared properties and
members on the interface. Interfaces cannot be instantiated, and its
members cannot contain any code that implement its members; onlythe
signatures can.
Interfaces are not classes. In C#, interfaces are defined using the
interface keyword. A common naming convention is to begin all
interface names with a capital I. The modifiers of the members of an Figure 1. Tra ns a cti on cl a s s i mpl ements ITra ns a cti ons
interface are all public by default. Example 1 demonstrates how tocreate i nterfa ce
an interface.
Example 1. Defi ni ng i nterfa ce ITra ns a cti ons .cs
namespace TransactionInterfaces
{
public interface ITransactions
{
void showTransaction();
double getAmount();
}
}
When a class implements an interface, the class can invoke the interface members directly from the object level. An interface
can be implemented on a class using the colon (:) symbol. The example below demonstrates how to implement an interface.
Example 2. Tra ns a cti on cl a s s i mpl ements ITra ns a cti ons i nterfa ce
using System;
using TransactionInterfaces;
namespace TransactionNamespace
{
public class Transaction : ITransactions
{
private string transaction_code;
private double amount;
namespace AreasNamespace
{
public class CalculateAreas : ISquare, IRectangle, ITriangle
{
public int getSquareArea(int side) {
return side * side;
}
Example 3 shows that the interfaces ISquare, IRectangle, and ITriangle are declared separately and that they are all
members of the namespace ShapeInterfaces. The class CalculateAreas implements these interfaces and their method
signatures.
Implementing Interface Properties
An interface cannot contain instance variables or fields butmay
contain properties. A property can access a private data
member of the class. A read ( get) and write ( set) property can
be declared in an interface using this syntax:
datatype propertyName { get; set; }
The accessors (get and set) of an interface does not have a
body. The purpose of the accessors is to indicate the properties
are read-write. Example 4 demonstrates how to declare
properties on the interface and how to implement them,
including how to use them.
Figure 2. Student cl ass i mplements IPerson i nterface with properti es
Example 4. Student cl a s s i mpl ements IPers on i nterfa ce wi th decl a red properti es
INTERFACE
namespace PersonInterface
{
public interface IPerson
{
string first_name { get; set; }
string last_name { get; set; }
int age { get; set; }
namespace StudentNamespace
{
public class Student : IPerson
{
public string student_FName;
public string student_LName;
public int student_age;
REFERENCES:
Deitel, P. and Deitel, H. (2015). Visual C# 2012 how to program (5th Ed.). USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual C# (4th Ed.). USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.