How Interface Works?
How Interface Works?
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:
// 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.
To access the interface methods, the interface must be "implemented” by another class.
To implement an interface, use the : symbol (just like with inheritance).
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() {
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:
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.
C# does not support multiple class inheritance, so to solve this problem we use
interfaces to achieve multiple class inheritance.
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() {
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;