Interfaces
are the contracts in the outside world.
Program.cs
What it means?
Take
an example, let us assume, there is a standard for all mobile phones, as per
the standard, below are the basic functionality for the mobile phones to
provide.
Vendor1
provides the functionality for the mobile in language JAVA
Vendor2
provides the functionality for the mobile in language C++
Vendor3
provides the functionality for the mobile in language JAVA
Vendor4
provides the functionality for the mobile in language C#
That
mean all the vendors providing the same functionality, but the way they are
providing is different.
One
more example is IBM JAVA, Oracle JAVA implements Java specification,
but the way they implemented the features is different.
Interfaces in C#
In
C#, interfaces are reference types contain only method signatures.
Interfaces
doesn't contain method bodies, these are implemented by classes that extend
(or) inherit these interfaces
Defining an Interface
interface InterfaceName{
// method signatures
}
Note
All
the methods in the interface are public.
Implementing an
Interface
A
class that implements the interface provides the method body for the method
signatures in the interface.
Syntax
class
ClassName : Interface1, Interface2, ...Interface N{
//implements the interfaces
}
Program.cs
using System; interface Circle { double getArea(int r); } class MyCircle : Circle { private static double PI = 3.1428; public double getArea(int r) { return (PI * r * r); } } class Program { static void Main(string[] args) { MyCircle circle = new MyCircle(); double area = circle.getArea(10); Console.WriteLine("Area of the Circle is {0}", area); } }
Output
Area
of the Circle is 314.28
Some Points to
Remember
1.If
a class implements an interface, then it must implement all the methods in the
interface, otherwise, the class must declared as abstract.
2.
By default all the methods in the interface are public, while implementing the
interface, class must provide the public access specifier for the methods it
implementing.
Interface as
Reference Types
Interfaces
can be used as Reference types. If you define a reference variable whose type
is an interface, any object you assign to it must be an instance of a class
that implements the interface.
Example
Circle
circle = new MyCircle();
Program.cs
using System; interface Circle { double getArea(int r); } class MyCircle : Circle { private static double PI = 3.1428; public double getArea(int r) { return (PI * r * r); } } class Program { static void Main(string[] args) { Circle circle = new MyCircle(); double area = circle.getArea(10); Console.WriteLine("Area of the Circle is {0}", area); } }
Output
Area
of the Circle is 314.28
By
assigning an object to the interface type, the reference variable “circle1”
call only the methods declared in the interface. Trying to call the methods
which are not declared in the interface cause the compiler error.
using System; interface Circle { double getArea(int r); } class MyCircle : Circle { private static double PI = 3.1428; public double getArea(int r) { return (PI * r * r); } public double getPerimeter(int r) { return (PI * 2 * r); } } class Program { static void Main(string[] args) { Circle circle = new MyCircle(); circle.getPerimeter(10); } }
Above
program, tries to call the getPerimeter(), which is not declared in the
interface Circle. Your program don’t compile.
Class can implement
more than one interface
In
Java, a class can implement more than one interface.
Syntax
class ClassName : Interface1, Interface2{
}
Program.cs
using System; interface Area { double getArea(double r); } interface Perimeter { double getPerimeter(double r); } class MyCircle : Area, Perimeter{ double PI = 3.1428; public double getArea(double r) { return (PI * r * r); } public double getPerimeter(double r) { return (PI * 2 * r); } } class Program { static void Main(string[] args) { MyCircle circle = new MyCircle(); double perimeter = circle.getPerimeter(10); double area = circle.getArea(10); Console.WriteLine("Perimeter : {0}", perimeter); Console.WriteLine("Area : {0}", area); } }
Output
Perimeter
: 62.856
Area
: 314.28
Interface extends other
interface
In
C#, one interface can extends other interface.
Syntax
interface Interface3 : Interface1,
Interface2{
}
Class
that implementing the Interface3 must provide implementation for all the
methods in Interface1, Interface2 and Interface3.
Program.cs
using System; interface Interface1 { int sum(int a, int b); } interface Interface2 : Interface1 { int sum(int a, int b, int c); } class Sum : Interface2 { public int sum(int a, int b, int c) { return (a + b + c); } public int sum(int a, int b) { return (a + b); } } class Program { static void Main(string[] args) { Sum sumCalc = new Sum(); int result1 = sumCalc.sum(10, 20); int result2 = sumCalc.sum(10, 20, 30); Console.WriteLine("result1 : {0}", result1); Console.WriteLine("result2 : {0}", result2); } }
Output
result1
: 30
result2
: 60
No comments:
Post a Comment