[go: up one dir, main page]

0% found this document useful (0 votes)
34 views20 pages

Unit 3 Dot Net Interface

Uploaded by

richmakercasio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views20 pages

Unit 3 Dot Net Interface

Uploaded by

richmakercasio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Unit 3:Exception & object life

time and Interface and Collection


Interface in C#
• Like a class, Interface can have methods, properties, events,
and indexers as its members. But interfaces will contain only
the declaration of the members. The implementation of the
interface’s members will be given by class who implements
the interface implicitly or explicitly.
• 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 .
• We use the interface keyword to create an interface. For
example,
interface IPolygon { interface <interface_name > {
// declare Events
// method without body // declare indexers
void calculateArea(); // declare methods
// declare properties }
}
• It is considered good practice to start with the letter "I" at the
beginning of an interface, as it makes it easier for yourself and
others to remember that it is an interface and not a class.
• By default, members of an interface are abstract and public.
• Note: Interfaces can contain properties and methods, but not
fields.
• An interface cannot contain constructors and fields.
• Multiple inheritance is possible with the help of Interfaces but
not with classes.
• We cannot use access modifiers inside an interface.
• Note: We must provide the implementation of all the methods
of interface inside the class that implements it.
Implementing an Interface
• We cannot create objects of an interface. To use an
interface, other classes must implement it. Same as in
C# Inheritance, we use : symbol to implement an
interface. For example,
using System;
namespace CsharpInterface {
interface IPolygon {
// method without body
void calculateArea(int l, int b);
}
class Rectangle : IPolygon {
// implementation of methods inside interface
public void calculateArea(int l, int b) {
int area = l * b;
Console.WriteLine("Area of Rectangle: " + area); }
}
class Program {
static void Main (string [] args) {
Rectangle r1 = new Rectangle(); r1.calculateArea(100, 200);
}
}
}
Output:
Area of Rectangle: 20000
Implementing Multiple Interfaces
• Unlike inheritance, a class can implement multiple interfaces. For example,
using System;
namespace CsharpInterface {
interface IPolygon {
// method without body
void calculateArea(int a, int b);
}
interface IColor {
void getColor();
}
// implements two interface
class Rectangle : IPolygon, IColor
{
// implementation of IPolygon interface
public void calculateArea(int a, int b)
{
int area = a * b;
Console.WriteLine("Area of Rectangle: " + area);
// implementation of IColor interface
public void getColor() {
Console.WriteLine("Red Rectangle");
}
}
class Program {
static void Main (string [] args) {
Rectangle r1 = new Rectangle(); r
1.calculateArea(100, 200);
r1.getColor();
}}}
• Output
Area of Rectangle: 20000
Red Rectangle
Using reference variable of an interface
• We can use the reference variable of an interface. For example,
using System;
namespace CsharpInterface {
interface IPolygon {
// method without body
void calculateArea(int l, int b);
}
class Rectangle : IPolygon {
// implementation of methods inside interface
public void calculateArea(int l, int b) {
int area = l * b;
Console.WriteLine("Area of Rectangle: " + area);
}
}
class Program {
static void Main (string [] args) {
// using reference variable of interface
IPolygon r1 = new Rectangle();
r1.calculateArea(100, 200); }
}}
Output
Area of Rectangle: 20000
• Notice, we have used the reference variable of
interface IPolygon. It points to the
class Rectangle that implements it.
• Though we cannot create objects of an interface, we
can still use the reference variable of the interface that
points to its implemented class.
• Advantages of C# interface
• Now that we know what interfaces are, let's learn about why
interfaces are used in C#.
• Similar to abstract classes, interfaces help us to
achieve abstraction in C#.

Here, the method calculateArea() inside the interface, does not


have a body. Thus, it hides the implementation details of the
method.
• Interfaces provide specifications that a class (which
implements it) must follow.

In our previous example, we have used calculateArea() as a


specification inside the interface IPolygon. This is like setting
a rule that we should calculate the area of every polygon.
• Now any class that implements the IPolygon interface
must provide an implementation for
the calculateArea() method.
• Interfaces are used to achieve multiple inheritance in
C#.
• Interfaces provide loose coupling(having no or least
effect on other parts of code when we change one part
of a code).
In our previous example, if we change the
implementation of calculateArea() in the Square class it
does not affect the Rectangle class.
Why And When To Use Interfaces?
1) To achieve security - hide certain details and only
show the important details of an object (interface).
2) C# does not support "multiple inheritance" (a class
can only inherit from one base class). However, it can
be achieved with interfaces, because the class
can implement multiple interfaces.
• Note: To implement multiple interfaces, separate
them with a comma (see example below).
public Interface IPoint {
// Error! Interfaces cannot have fields.
public int numbOfPoints;
// Error! Interface don't have constructors.
public IPoint() {
numberOfPoints = 0;
}
// Error! Interface don't provide an implementation. byte
GetNumberOfPoints() {
return numbOfPoints;
}
}
static void Main(string[] args) {
// Error!
IPoint ptr = new IPoint();
}
Invoking Interface Members at the Object Level
Let's start with an example.
public Interface IPoint {
byte GetNumberOfPoints();
}
• Here is the shapes hierarchy with interfaces we're going to use.
• Here are files for the class hierarchy diagram:
// IPoint.cs
namespace CustomInterface {
// The IPoint behavior as a read-only property public interface
IPoint {
byte Points {
get;
}
}
}
// Circle.cs
using System;
namespace CustomInterface {
public class Circle : Shape {
public Circle() {
}
public Circle(string name) : base(name) {
}
public override void Draw()
{ Console.WriteLine("Drawing {0} the Circle",
shapeName); } } }
// ThreeDCircle
using System;
namespace CustomInterface {
public class ThreeDCircle : Circle {
public void Draw() {
Console.WriteLine("Drawing a 3D Circle");
}}}

// Triangle.cs
using System;
namespace CustomInterface {
public class Triangle : Shape, IPoint {
public Triangle() { }
public Triangle(string name) : base(name) { }
public override void Draw() {
Console.WriteLine("Drawing {0} the Triangle", PetName); }
public byte Points {
get {
return 3;
}}}}
// Hexagon.cs using System;
namespace CustomInterface {
public class Hexagon : Shape, IPoint {
public Hexagon() { }
public Hexagon(string name) : base(name) { } public
override void Draw() { Console.WriteLine("Drawing
{0} the Hexagon", PetName); }
public byte Points {
get {
return 6; } } } }
// Shape.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomInterface {
public abstract class Shape {
protected string shapeName;
public Shape() {
shapeName = "NoName"; }
public Shape(string s) {
shapeName = s;
}
public virtual void Draw() {
Console.WriteLine("Shape.Draw()"); }
public string PetName {
get { return shapeName; }
set { shapeName = value; } }
static void Main(string[] args) {
}}
Now we have a set of types that support IPoint interface. Next question is how we
interact with the new interface. Let's invoke the methods of interface directly from
the object level. First, look at the following Main() method:
static void Main(string[] args)
{
Hexagon hex = new Hexagon();
Console.WriteLine("Points: {0}", hex.Points);
Console.ReadLine(); }

Output from the run is:


• Points: 6
That works well because the Hexagon class implements the IPoint interface and it
has a Points property. However, in cases when we have lots of Shape-compatible
types, and only some of which support IPoint, there is a chance to get an error if
we invoke the Point property on a class that has not implemented IPoint.

You might also like