0% found this document useful (0 votes)
72 views7 pages

How Interface Works?

The interfaces Taxable and Printable are defined. The House class implements Taxable and Printable, calculating taxes based on location and area. The Car class implements Taxable and Printable, calculating taxes based on value and number of seats. Both classes output details when the detailsToPrint method is called.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views7 pages

How Interface Works?

The interfaces Taxable and Printable are defined. The House class implements Taxable and Printable, calculating taxes based on location and area. The Car class implements Taxable and Printable, calculating taxes based on value and number of seats. Both classes output details when the detailsToPrint method is called.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Interface

 Another way to achieve abstraction in C#, is with interfaces.


 An interface is a completely "abstract class", which can only contain abstract methods
and properties (with empty bodies)
 By default, members of an interface are abstract and public.
 Interfaces can contain properties and methods, but not fields.

How interface works?

 Interfaces specify what a class must do and not how.


 Interfaces can’t have private members.
 By default all the members of Interface are public and abstract.
 The interface will always defined with the help of keyword ‘interface‘.
 Interface cannot contain fields because they represent a particular implementation of data.
 Multiple inheritance is possible with the help of Interfaces but not with classes.

How to declare interface?

 To declare an interface, use interface keyword. It is used to provide total abstraction. That
means all the members in the interface are declared with the empty body and are public and
abstract by default.
 A class that implements interface must implement all the methods declared in the interface.

Syntax:

interface <interface_name >

// declare methods

// declare properties

Examples:
interface MyVehicle
{
int ComputeCarage(int year);
void displayDetails();
}

interface BankAccount
{
double computeInterest(double rate);
void displayDetails();
}
How Interface works?

 interface is defined as a syntactical contract that all the classes inheriting the interface
should follow.
 interface defines the 'what' part of the syntactical contract and the deriving classes
define the 'how' part of the syntactical contract.
 Interfaces define properties, methods, and events, which are the members of the
interface.
 Interfaces contain only the declaration of the members and derive class must define all
the declared members of the interface.

How to access an interface?

 To access the interface methods, the interface must be "implemented” by another class.
 To implement an interface, use the : symbol (just like with inheritance).

Syntax to implement interface:

class name: interface name{

Example:

//interface class( same method when adding a class but this time select the interface)
interface MyVehicle
{
int ComputeCarAge(int currentyear);
void displayDetails();
}
// implementing class
class Car:MyVehicle
{
private int yearmodel;
private string manufacturer;

public Car() {

public void setYearmodel(int yearmodel) {


this.yearmodel = yearmodel;
}
public void setManufacturer(string manufacturer) {
this.manufacturer = manufacturer;

public int getYearmodel() {


return yearmodel;
}
public string getManufacturer() {
return manufacturer;
}
//we need to implement method declared in the interface
public int ComputeCarAge(int currentyear) {
if (getYearmodel() < currentyear)
return currentyear - getYearmodel();
else
return 0;
}
public void displayDetails() {
Console.Write("Year model:" + getYearmodel() + "\nManufacturer:" +
getManufacturer());
}

Program.cs
class Program
{
static void Main(string[] args)
{
int year;
Car c1 = new Car();
c1.setYearmodel(1990);
c1.setManufacturer("Ford");
Console.Write("Input current year:");
year = Convert.ToInt32(Console.ReadLine());
c1.displayDetails();
Console.WriteLine("\nCar age:" + c1.ComputeCarAge(year));
Console.ReadKey();

}
}

Outputs:
Implementing multiple interface

Syntax:

class name: interface name1, interface name2{

You can implement many interface, but take into consideration that when implementing interfaces, all
its declared methods must have methods definition in the class implementing the interface.

How to have multiple inheritance using interfaces

 C# does not support multiple class inheritance, so to solve this problem we use
interfaces to achieve multiple class inheritance.

Multiple Inheritance Illustration:

With the help of the interface, class C( as shown in the above diagram) can get the features of class A
and B.

Example:

//first interface
interface MyVehicle
{
int ComputeCarAge(int currentyear);
void displayDetails();
}
//second interface
interface MyVehicel2
{
double computeSpeed();
}
//implementing class
// to implements the myVehicle interface
class Car:MyVehicle,MyVehicel2
{
private int yearmodel;
private string manufacturer;

public Car() {

public void setYearmodel(int yearmodel) {


this.yearmodel = yearmodel;
}
public void setManufacturer(string manufacturer) {
this.manufacturer = manufacturer;

public int getYearmodel() {


return yearmodel;
}
public string getManufacturer() {
return manufacturer;
}
//we need to implement method declared in the interface
public int ComputeCarAge(int currentyear) {
if (getYearmodel() < currentyear)
return currentyear - getYearmodel();
else
return 0;
}
public void displayDetails() {
Console.Write("\nYear model:" + getYearmodel() + "\nManufacturer:" +
getManufacturer());
}
//implementation of the second interface
public double computeSpeed(int d, int t) {
return (double)d / t;
}

Program.cs
class Program
{
static void Main(string[] args)
{
int year,dis,time;
Car c1 = new Car();
c1.setYearmodel(1990);
c1.setManufacturer("Ford");
Console.Write("Input current year:");
year = Convert.ToInt32(Console.ReadLine());
Console.Write("\nInput distance :");
dis = Convert.ToInt32(Console.ReadLine());
Console.Write("\nInput time:");
time = Convert.ToInt32(Console.ReadLine());
c1.displayDetails();
Console.WriteLine("\nCar age:" + c1.ComputeCarAge(year));
Console.WriteLine("\nCar speed:" +
c1.computeSpeed(dis,time).ToString("0.00"));
Console.ReadKey();

}
}

Output:

Exercise (Graded)

Declare an interface Taxable with a method TaxValue which returns a decimal number.

Declare an interface Printable with a method detailsToprint which display details of the instance of a
class.

Then, create a class name House and class name Car which implement the Taxable and Printable
interface. House class is define by the attributes: string location, bool inCity, double area, decimal value.
Car class is define by the attributes: int numberOfSeats, int regNumber, decimal value.

The tax computation formula for house will be: if the house is in the city, estimatedValue / 3075) * 250 +
50 * (decimal)area, otherwise , estimatedValue / 1075) * 150.

The tax computation formula for Car will be: value / 10 + 105 * numberOfSeats;

You might also like