SlideShare uma empresa Scribd logo
1 de 17
WHAT ARE INTERFACES??

Interfaces are reference types. It is basically a
     class with the following differences:

 1. All members of an interface are explicitly
           PUBLIC and ABSTRACT.

     2. Interface cant contain CONSTANT
 fields, CONSTRUCTORS and DESTRUCTORS.

  3. Its members can’t be declared STATIC.

   4. All the methods are ABSTRACT, hence
   don’t include any implementation code.

5. An Interface can inherit multiple Interfaces.
A                  A              B




   B

                              C


   C
                      THIS IS NOT POSSIBLE
THIS IS POSSIBLE IN
                      IN C#
C#
As evident from the previous diagram, in C#, a class cannot have more than one SUPERCLASS.
For example, a definition like:

Class A: B,C
{
            .........
            .........
}
is not permitted in C#.


However, the designers of the language couldn’t overlook the importance of the concept of
multiple inheritance.
A large number of real life situations require the use of multiple inheritance whereby we inherit
the properties of more than one distinct classes.
For this reason, C# provides the concept of interfaces. Although a class can’t inherit multiple
classes, it can IMPLEMENT multiple interfaces.



                                                   .
DEFINING AN INTERFACE

An interface can contain one or multiple METHODS, POPERTIES, INDEXERS and EVENTS.
But, these are NOT implemented in the interface itself.
They are defined in the class that implements the interface.

Syntax for defining an interface:

interface interfacename
{
  member declarations..
}

interface is a KEYWORD.
interfacename is a valid C# identifier.

Example:

interface Show
{
  void Display();                          // method within interface
  int Aproperty
   {
     get;                                 // property within interface
   }
 event someEvent Changed;
void Display();                           // event within interface
}
EXTENDING AN INTERFACE

Interfaces can be extended like classes. One interface can be sub interfaced
from other interfaces.

Example:

interface addition
{
  int add(int x, int y);
}

interface Compute: addition
{
  int sub(int x, int y);
}

the above code will place both the methods, add() and sub() in the interface
Compute. Any class implementing Compute will be able to implement both
these methods.

INTERFACES CAN EXTEND ONLY INTERFACES, THEY CAN’T EXTEND CLASSES.
This would violate the rule that interfaces can have only abstract members.
IMPLEMENTING INTERFACES

An Interface can be implemented as follows:

class classname: interfacename
{
  body of classname
}

A class can derive from a single class and implement as many interfaces
required:

class classname: superclass, interface1,interface 2,…
{
 body of class name
}

eg:
class A: B,I1,I2,..
 {
…….
……..
}
using System;
interface Addition
{
  int Add();
}
interface Multiplication
{
int Mul();
}
class Compute:Addition,Multiplication
{
  int x,y;
  public Compute(int x, int y)
  {
    this.x=x;
    this.y=y;
   }
public int Add()                        //implement Add()
{
  return(x+y);
}
public int Mul()                        //implement Mul()
{
return(x*y);
}}
Class interfaceTest
{
  public static void Main()
  {
     Compute com=new Compute(10,20);               //Casting
     Addition add=(Addition)com;
     Console.WriteLine(“sum is:”+add.Add());
     Multiplication mul=(Multiplication)com;     //Casting
     Console.WriteLine(“product is:”+mul.Mul());
   }
}



                           OUTPUT

sum is: 30
product is: 200
MULTILPLE IMPEMENTATION OF AN
INTERFACE

Just as a class can implement
multiple interfaces, one single
interface can be implemented by
multiple classes. We demonstrate this
by the following example;

using System;
interface Area;
{
  double Compute(double x)
}
class Square:Area
{
  public double Compute(double x)
  {
    return(x*x);
  }
class Circle:Area
  {
    public double Compute(double x)
     {
       return(Math.PI*x*x);
     }
}
Class InterfaceTest
{
  public static void Main()
  {
    Square sqr=new Square();
    Circle cir=new Circle();
    Area area=(Area)sqr;
    Console.WriteLine(“area of square is:”+area.Compute(10.0));
    area=(Area)cir;
    Console.WriteLine(“area of circle is:”+area.Compute(10.0));
  }
}


                                    OUTPUT
area of square is: 100
area of circle is: 314.159265
INTERFACES AND INHERITANCE:

We may encounter situations where the base class of a derived class
implements an interface. In such situations, when an object of the derived
class is converted to the interface type, the inheritance hierarchy is
searched until it finds a class that has directly implemented the interface.
We consider the following program:


using system;
interface Display
{
  void Print();
}
class B:Display       //implements Display
{
  public void Print()
  {
    Console.Writeline(“base display”);
  }
}
class D:B           //inherits B class
{
 public new void Print()
  {
    Console.Writeline(“Derived display”);
  }
}
class InterfaceTest3
{
 public static void Main()
 {
   D d=new D();
   d.Print();
   Display dis=(Display)d;
   dis.Print();
  }
}

OUTPUT:
Derived display
base display

The statement dis.Print() calls the method in the base class B but not in the derived
class itself.
This is because the derived class doesn’t implement the interface.
EXPLICIT INTERFACE IMPLEMENTATION

The main reason why c# doesn’t support multiple
inheritance is the problem of NAME COLLISION.

But the problem still persists in C# when we are
implementing multiple interfaces.

For example:
let us consider the following scenario, there are two
interfaces and an implementing class as follows:

interface i1
{
  void show();
}
interface i2
{
  void show();
}
class classone: i1,i2
{
  public void display()
  {
    …
    …
  }
The problem is that whether the class classone implements
i1.show() or i2.show()??
This is ambiguous and the compiler will report an error.
To overcome this problem we have something called EXPLICIT
INTERFCAE IMPLEMENTATION. This allows us to specify the name
of the interface we want to implement.

We take the following program as an example:

using system;
intereface i1
{
  void show();
}
interface i2
{
  void show();
}
Class classone: i1,i2
{
  void i1.show()
   {
     Console.Writeline(“ implementing i1”);
   }
void i2.show()
 {
  Console.Writeline(“ implementing i2”);
 }
}
class interfaceImplement
 {
   public static void Main()
    {
      classone c1=new classone();
      i1 x=(i1)c1;
      x.show();
     i2 y=(i2)c1;
     y.show();
   }
}
                                  OUTPUT: implementing i1
                                                    implementing i2
Interfaces c#

Mais conteúdo relacionado

Mais procurados

Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#foreverredpb
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVASAGARDAVE29
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++aleenaguen
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpprajshreemuthiah
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Implementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyImplementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyMartin Odersky
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language samt7
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programmingRiccardo Cardin
 
Boxing & unboxing
Boxing & unboxingBoxing & unboxing
Boxing & unboxingLarry Nung
 

Mais procurados (20)

Java interface
Java interfaceJava interface
Java interface
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
Friend function
Friend functionFriend function
Friend function
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
 
Php essentials
Php essentialsPhp essentials
Php essentials
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Implementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyImplementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in Dotty
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Boxing & unboxing
Boxing & unboxingBoxing & unboxing
Boxing & unboxing
 
Java constructors
Java constructorsJava constructors
Java constructors
 

Semelhante a Interfaces c#

Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfTabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptTabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptTabassumMaktum
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...yazad dumasia
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Abou Bakr Ashraf
 
Indicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docxIndicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docxmigdalialyle
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETOOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETjinaldesailive
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
Interface
InterfaceInterface
Interfacevvpadhu
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & InterfaceLinh Lê
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 

Semelhante a Interfaces c# (20)

Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
Interface
InterfaceInterface
Interface
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
Indicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docxIndicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docx
 
Java interface
Java interfaceJava interface
Java interface
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETOOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Interface
InterfaceInterface
Interface
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
 
Inheritance
InheritanceInheritance
Inheritance
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 

Último

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Interfaces c#

  • 1.
  • 2. WHAT ARE INTERFACES?? Interfaces are reference types. It is basically a class with the following differences: 1. All members of an interface are explicitly PUBLIC and ABSTRACT. 2. Interface cant contain CONSTANT fields, CONSTRUCTORS and DESTRUCTORS. 3. Its members can’t be declared STATIC. 4. All the methods are ABSTRACT, hence don’t include any implementation code. 5. An Interface can inherit multiple Interfaces.
  • 3. A A B B C C THIS IS NOT POSSIBLE THIS IS POSSIBLE IN IN C# C#
  • 4. As evident from the previous diagram, in C#, a class cannot have more than one SUPERCLASS. For example, a definition like: Class A: B,C { ......... ......... } is not permitted in C#. However, the designers of the language couldn’t overlook the importance of the concept of multiple inheritance. A large number of real life situations require the use of multiple inheritance whereby we inherit the properties of more than one distinct classes. For this reason, C# provides the concept of interfaces. Although a class can’t inherit multiple classes, it can IMPLEMENT multiple interfaces. .
  • 5. DEFINING AN INTERFACE An interface can contain one or multiple METHODS, POPERTIES, INDEXERS and EVENTS. But, these are NOT implemented in the interface itself. They are defined in the class that implements the interface. Syntax for defining an interface: interface interfacename { member declarations.. } interface is a KEYWORD. interfacename is a valid C# identifier. Example: interface Show { void Display(); // method within interface int Aproperty { get; // property within interface } event someEvent Changed; void Display(); // event within interface }
  • 6. EXTENDING AN INTERFACE Interfaces can be extended like classes. One interface can be sub interfaced from other interfaces. Example: interface addition { int add(int x, int y); } interface Compute: addition { int sub(int x, int y); } the above code will place both the methods, add() and sub() in the interface Compute. Any class implementing Compute will be able to implement both these methods. INTERFACES CAN EXTEND ONLY INTERFACES, THEY CAN’T EXTEND CLASSES. This would violate the rule that interfaces can have only abstract members.
  • 7. IMPLEMENTING INTERFACES An Interface can be implemented as follows: class classname: interfacename { body of classname } A class can derive from a single class and implement as many interfaces required: class classname: superclass, interface1,interface 2,… { body of class name } eg: class A: B,I1,I2,.. { ……. …….. }
  • 8. using System; interface Addition { int Add(); } interface Multiplication { int Mul(); } class Compute:Addition,Multiplication { int x,y; public Compute(int x, int y) { this.x=x; this.y=y; } public int Add() //implement Add() { return(x+y); } public int Mul() //implement Mul() { return(x*y); }}
  • 9. Class interfaceTest { public static void Main() { Compute com=new Compute(10,20); //Casting Addition add=(Addition)com; Console.WriteLine(“sum is:”+add.Add()); Multiplication mul=(Multiplication)com; //Casting Console.WriteLine(“product is:”+mul.Mul()); } } OUTPUT sum is: 30 product is: 200
  • 10. MULTILPLE IMPEMENTATION OF AN INTERFACE Just as a class can implement multiple interfaces, one single interface can be implemented by multiple classes. We demonstrate this by the following example; using System; interface Area; { double Compute(double x) } class Square:Area { public double Compute(double x) { return(x*x); } class Circle:Area { public double Compute(double x) { return(Math.PI*x*x); } }
  • 11. Class InterfaceTest { public static void Main() { Square sqr=new Square(); Circle cir=new Circle(); Area area=(Area)sqr; Console.WriteLine(“area of square is:”+area.Compute(10.0)); area=(Area)cir; Console.WriteLine(“area of circle is:”+area.Compute(10.0)); } } OUTPUT area of square is: 100 area of circle is: 314.159265
  • 12. INTERFACES AND INHERITANCE: We may encounter situations where the base class of a derived class implements an interface. In such situations, when an object of the derived class is converted to the interface type, the inheritance hierarchy is searched until it finds a class that has directly implemented the interface. We consider the following program: using system; interface Display { void Print(); } class B:Display //implements Display { public void Print() { Console.Writeline(“base display”); } }
  • 13. class D:B //inherits B class { public new void Print() { Console.Writeline(“Derived display”); } } class InterfaceTest3 { public static void Main() { D d=new D(); d.Print(); Display dis=(Display)d; dis.Print(); } } OUTPUT: Derived display base display The statement dis.Print() calls the method in the base class B but not in the derived class itself. This is because the derived class doesn’t implement the interface.
  • 14. EXPLICIT INTERFACE IMPLEMENTATION The main reason why c# doesn’t support multiple inheritance is the problem of NAME COLLISION. But the problem still persists in C# when we are implementing multiple interfaces. For example: let us consider the following scenario, there are two interfaces and an implementing class as follows: interface i1 { void show(); } interface i2 { void show(); } class classone: i1,i2 { public void display() { … … }
  • 15. The problem is that whether the class classone implements i1.show() or i2.show()?? This is ambiguous and the compiler will report an error. To overcome this problem we have something called EXPLICIT INTERFCAE IMPLEMENTATION. This allows us to specify the name of the interface we want to implement. We take the following program as an example: using system; intereface i1 { void show(); } interface i2 { void show(); }
  • 16. Class classone: i1,i2 { void i1.show() { Console.Writeline(“ implementing i1”); } void i2.show() { Console.Writeline(“ implementing i2”); } } class interfaceImplement { public static void Main() { classone c1=new classone(); i1 x=(i1)c1; x.show(); i2 y=(i2)c1; y.show(); } } OUTPUT: implementing i1 implementing i2