Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

IT1907

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;

public Transaction(string code, double amount) {


this.transaction_code = code;
this.amount = amount;
}

public void showTransaction() {


Console.WriteLine("Transaction code: " + this.transaction_code);
Console.WriteLine("Amount: " + this.getAmount());
}

public double getAmount()


{
return this.amount;
}
}
}

08 Handout 1 *Property of STI


 student.feedback@sti.edu Page 1 of 4
IT1907

Creating an instance of the Transaction class using the defined constructor


using System;
using TransactionNamespace;

public class Program


{
static void Main(string[] args)
{
Transaction t1 = new Transaction("ABC20190001", 5000.35);
t1.showTransaction();
}
}
In Example 1, the interface ITransactions declares two (2) methods, namely showTransaction and getAmount, without a
method body or implementation. In Example 2, the class Transaction implements the ITransactions interface. Then, it
defines the two (2) methods and provides them with the implementation. The method implementation in the class musthave
the same signature, parameters, and method name as defined in the interface. Otherwise, the compiler will return an error.
Interfaces ensure that the implementing classes implement all the methods and properties declared in the interface.

Implementing Multiple Interfaces


A class can inherit only one (1) base class, but a class can implement multiple interfaces. When a class implements multiple
interfaces, the implementing class must implement all the members of the interfaces. To implement multiple interfaces, the
colon (:) symbol is used then the interfaces are declared in a comma-separated list. The following example demonstrateshow
to implement multiple interfaces.
Example 3. Impl ementi ng mul ti pl e i nterfa ces
Interface ISquare Interface ITriangle
namespace ShapeInterfaces namespace ShapeInterfaces
{ {
public interface ISquare public interface ITriangle
{ {
int getSquareArea(int side); double getTriangleArea(double b, double h);
} }
} }
Interface IRectangle
namespace ShapeInterfaces
{
public interface IRectangle
{
double getRectArea(double length, double width);
}
}
Implementing multiple interfaces on implementing a class
using ShapeInterfaces;

namespace AreasNamespace
{
public class CalculateAreas : ISquare, IRectangle, ITriangle
{
public int getSquareArea(int side) {
return side * side;
}

public double getRectArea(double length, double width) {


return length * width;
}

08 Handout 1 *Property of STI


 student.feedback@sti.edu Page 2 of 4
IT1907
public double getTriangleArea(double b, double h) {
return (b * h) / 2;
}
}
}

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; }

void setInfo(string FName, string LName, int year_old);


}
}
IMPLEMENTATION
using PersonInterface;

namespace StudentNamespace
{
public class Student : IPerson
{
public string student_FName;
public string student_LName;
public int student_age;

public string first_name


{
get {
return student_FName;
} set {
student_FName = value;
}
}

08 Handout 1 *Property of STI


 student.feedback@sti.edu Page 3 of 4
IT1907
public string last_name
{
get
{
return student_LName;
}
set
{
student_LName = value;
}
}

public int age


{
get
{
return student_age;
}
set
{
student_age = value;
}
}

public void setInfo(string FName, string LName, int year_old)


{
this.first_name = FName;
this.last_name = LName;
this.age = year_old;
}
}
}
Creating an instance of the Student class
using StudentNamespace;
public class Program {
static void Main(string[] args)
{
Student s1 = new Student();
s1.setInfo("Jack", "Paul", 25);
Console.WriteLine("First name: " + s1.first_name);
Console.WriteLine("Last name: " + s1.last_name);
Console.WriteLine("Age: " + s1.age);
}
}
OUTPUT
First name: Jack
Last name: Paul
Age: 25
In Example 4, the class Student implements the IPerson interface. This class implements the method setInfo and the three
(3) properties—namely, first_name, last_name, and age—declared in the IPerson interface. These properties are used to
access the private variables declared on the class Student.

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.

08 Handout 1 *Property of STI


 student.feedback@sti.edu Page 4 of 4

You might also like