ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Interface and abstraction
About Abstract Class
• Abstract class in csharp is defined using "abstract"
keyword
• An abstract class may contain abstract methods and
accesses (properties).
• Non abstract methods or properties should provide
actual implementation in an abstract class.
• Abstract classes that cannot be instantiated mean we
cannot able to create an object of an abstract class but
abstract class can be implemented to child classes like
a common base class. An abstract class can be partially
implemented or not at all implemented.
Syntax
1
2
3

abstract class absCalculator
...code starts here
}
Abstract Method
In an abstract class a method which has a
keyword "abstract" and doesn't provide any
implementation is called abstract method.The
implementation logic of abstract methods is
provided by the child classes or derived
classes.Child classes use keyword "override"
with same method name (as abstract method
name) to provide further implementation of
abstract methods.
Non Abstract Method
In an abstract class a method which doesn't
have a keyword "abstract" and provide any
implementation is called non abstract method.
Why Abstract Class
Abstract class enforces derived classes to
provide all implementation logic for abstract
methods or properties.
create an abstract class "absCalculate" having
abstract methods and non abstract methods.
01
02
03
04
05
06
07
08
09
10
11

abstract class absCalculate

//A Non abstract method
public int Addition(int Num1, int Num2)
return Num1 + Num2;
}
//An abstract method, to be overridden in derived class
public abstract int Multiplication(int Num1, int Num2);
}
create a class "clsCalculate" and
implement it with abstract class
"absCalculate".
1
2
3
4

class clsCalculate:absCalculate

...code
}
Now lets give implementation for an abstract
method using override keyword implementing
the abstract method
1
class clsCalculate:absCalculate
2
3
4
//using override keyword implementing the
abstract method
5
public override int Multiplication(int Num1, int
Num2)
6
7
return Num1 * Num2;
8
}
9
}
create an object of class "clsCalculate" in our
main program of an console application
01
class Program
02
03
04
static void Main(string[] args)
05
06
07
Console.WriteLine("Please Enter First Number");
08
int num1 = Convert.ToInt16(Console.ReadLine());
09
10
Console.WriteLine("Please Enter Second Number");
11
int num2 = Convert.ToInt16(Console.ReadLine());
12
13
absCalculate objabCal = new clsCalculate();
14
int add = objabCal.Addition(num1, num2);
15
int multiplied = objabCal.Multiplication(num1, num2);
16
Console.WriteLine("Added Number is : 0}, Multiplied Number is :
1}", add, multiplied);
17
}
18
}
Output
What is an Interface?
• An interface looks like a class but has got no implementation. In an
interface we cannot do any implementation but we can declare
signatures of properties, methods, delegates and events.
• Implementation part is been handle by the class that implements
the interface. Implemented interface enforces the class like a
standard contract to provide all implementation of interface
members.
• We can implement single interface or multiple interfaces to the
class. By default interfaces are public.

• We declare interface by using "interface" keyword.
interface "IEmployee" with signature
of a method
"DisplayEmployeeDetails".
•
•
•
•
•

1 interface IEmployee{
2
3 void DisplayEmployeeDetails();
4
5 }
Various Forms of implementing
interface
There are two of ways of implementing
interfaces
• Explicit
• Implicit
Explicit interface implementation
• When we have two different interfaces with
same method name then in that scenario we
can use explicit interface implementation.
• To implement an interface explicitly we have
to define an interface name followed by (".")
dot operator then the method name
we have two different interfaces "IEmployee"
and "ICompany" with same method name.
1
2
3
4
5
6
7

public interface IEmployee{
void DisplayEmployeeDetails();
}
public interface ICompany{
void DisplayEmployeeDetails();
}
In order to implement an interface explicitly we
have to define the interface name followed by
(".") dot operator before method name
1
public class Employee : IEmployee,ICompany
2
{
3
void ICompany.DisplayEmployeeDetails(){
4
Console.WriteLine("ICompany Employee
Name --> Questpond and Employee Code --> 009");
5
}
6
void IEmployee.DisplayEmployeeDetails(){
7
Console.WriteLine("IEmployee Employee
Name --> Questpond and Employee Code --> 009");
8
}
9
}
create the objects of two interfaces "IEmployee"
and "ICompany" in our main method of a
Console Application program.
01
02
03
04
05
06
07
08
09
10
11
12

class Program{
static void Main(string[] args)
{
IEmployee IEmp = new clsEmployee();
IEmp.DisplayEmployeeDetails();
ICompany IComp = new clsEmployee();
IComp.DisplayEmployeeDetails();
}
}
Output
Implicit interface implementation
demonstration of an interface
"IEmployee" with signature of a
method "DisplayEmployeeDetails".
1
2
3
4

interface IEmployee
{
void DisplayEmployeeDetails();
}
create a class "Employee" and
implement the interface implicitly
1
public class Employee : IEmployee
2
{
3
public void DisplayEmployeeDetails()
4
{
5
Console.WriteLine("Employee Name
--> Questpond and Employee Code --> 009");
6
}
7
}
create the objects of an interface
"IEmployee" in our main method of a
Console Application program
1
2
3
4
5
6
7
8
9

public class Program{
static void Main(string[] args)
{
IEmployee IEmp = new clsEmployee();
IEmp.DisplayEmployeeDetails();
}
}
Output

More Related Content

What's hot (20)

Compiler lab final report writing
Compiler lab final report writingCompiler lab final report writing
Compiler lab final report writing
Umme habiba
Ìý
Functions
FunctionsFunctions
Functions
Michael Gordon
Ìý
Operators In Java Part - 8
Operators In Java Part - 8Operators In Java Part - 8
Operators In Java Part - 8
MuhammadAtif231
Ìý
Core java day4
Core java day4Core java day4
Core java day4
Soham Sengupta
Ìý
Reading and writting
Reading and writtingReading and writting
Reading and writting
andreeamolnar
Ìý
Interface
InterfaceInterface
Interface
vvpadhu
Ìý
Operators
OperatorsOperators
Operators
vvpadhu
Ìý
Operators
OperatorsOperators
Operators
loidasacueza
Ìý
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# program
Micheal Ogundero
Ìý
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
Ìý
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
Munsif Ullah
Ìý
Java codes
Java codesJava codes
Java codes
Hussain Sherwani
Ìý
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
AravindSankaran
Ìý
Introduction to Matlab Scripts
Introduction to Matlab ScriptsIntroduction to Matlab Scripts
Introduction to Matlab Scripts
Shameer Ahmed Koya
Ìý
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
Mahmoud Ouf
Ìý
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
Upender Upr
Ìý
Js
JsJs
Js
Daniele Armanasco
Ìý
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
mcollison
Ìý
Java small steps - 2019
Java   small steps - 2019Java   small steps - 2019
Java small steps - 2019
Christopher Akinlade
Ìý
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
Muhammad Alhalaby
Ìý
Compiler lab final report writing
Compiler lab final report writingCompiler lab final report writing
Compiler lab final report writing
Umme habiba
Ìý
Operators In Java Part - 8
Operators In Java Part - 8Operators In Java Part - 8
Operators In Java Part - 8
MuhammadAtif231
Ìý
Reading and writting
Reading and writtingReading and writting
Reading and writting
andreeamolnar
Ìý
Interface
InterfaceInterface
Interface
vvpadhu
Ìý
Operators
OperatorsOperators
Operators
vvpadhu
Ìý
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# program
Micheal Ogundero
Ìý
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
Ìý
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
Munsif Ullah
Ìý
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
AravindSankaran
Ìý
Introduction to Matlab Scripts
Introduction to Matlab ScriptsIntroduction to Matlab Scripts
Introduction to Matlab Scripts
Shameer Ahmed Koya
Ìý
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
Mahmoud Ouf
Ìý
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
Upender Upr
Ìý
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
mcollison
Ìý

Viewers also liked (20)

AR+S The Role Of Abstraction In Human Computer Interaction
AR+S   The Role Of Abstraction In Human Computer InteractionAR+S   The Role Of Abstraction In Human Computer Interaction
AR+S The Role Of Abstraction In Human Computer Interaction
Ali Rıza SARAL
Ìý
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody. Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Sarah Ju-En Tan
Ìý
File class.48
File class.48File class.48
File class.48
myrajendra
Ìý
IoT and META-engineering with Service Templates
IoT and META-engineering with Service TemplatesIoT and META-engineering with Service Templates
IoT and META-engineering with Service Templates
Hans A. Kielland Aanesen
Ìý
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
Jussi Pohjolainen
Ìý
IEEE DEST CEE 2013 - Paper 4 (1) (1)
IEEE DEST CEE 2013 - Paper 4 (1) (1)IEEE DEST CEE 2013 - Paper 4 (1) (1)
IEEE DEST CEE 2013 - Paper 4 (1) (1)
Hans A. Kielland Aanesen
Ìý
Core java questions
Core java questionsCore java questions
Core java questions
Dinesh Reddy G
Ìý
Java topology abstraction service for ip-vp ns
Java  topology abstraction service for ip-vp nsJava  topology abstraction service for ip-vp ns
Java topology abstraction service for ip-vp ns
ecwayerode
Ìý
Java JDBC
Java JDBCJava JDBC
Java JDBC
Jussi Pohjolainen
Ìý
Java stereams
Java stereamsJava stereams
Java stereams
Jernej Virag
Ìý
Java lecture
Java lectureJava lecture
Java lecture
Nataraj Dg
Ìý
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
Anup Burange
Ìý
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
Pritom Chaki
Ìý
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
myrajendra
Ìý
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
Ìý
PAC
PACPAC
PAC
Talgat Islamgozhayev
Ìý
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
Jussi Pohjolainen
Ìý
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
Harry Potter
Ìý
Abstract class
Abstract classAbstract class
Abstract class
Harry Potter
Ìý
Operators in java
Operators in javaOperators in java
Operators in java
Ravi_Kant_Sahu
Ìý
AR+S The Role Of Abstraction In Human Computer Interaction
AR+S   The Role Of Abstraction In Human Computer InteractionAR+S   The Role Of Abstraction In Human Computer Interaction
AR+S The Role Of Abstraction In Human Computer Interaction
Ali Rıza SARAL
Ìý
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody. Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Final Animation Assessment Presentation: The Abstraction of Bohemian Rhapsody.
Sarah Ju-En Tan
Ìý
File class.48
File class.48File class.48
File class.48
myrajendra
Ìý
IoT and META-engineering with Service Templates
IoT and META-engineering with Service TemplatesIoT and META-engineering with Service Templates
IoT and META-engineering with Service Templates
Hans A. Kielland Aanesen
Ìý
IEEE DEST CEE 2013 - Paper 4 (1) (1)
IEEE DEST CEE 2013 - Paper 4 (1) (1)IEEE DEST CEE 2013 - Paper 4 (1) (1)
IEEE DEST CEE 2013 - Paper 4 (1) (1)
Hans A. Kielland Aanesen
Ìý
Core java questions
Core java questionsCore java questions
Core java questions
Dinesh Reddy G
Ìý
Java topology abstraction service for ip-vp ns
Java  topology abstraction service for ip-vp nsJava  topology abstraction service for ip-vp ns
Java topology abstraction service for ip-vp ns
ecwayerode
Ìý
Java stereams
Java stereamsJava stereams
Java stereams
Jernej Virag
Ìý
Java lecture
Java lectureJava lecture
Java lecture
Nataraj Dg
Ìý
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
Anup Burange
Ìý
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
Pritom Chaki
Ìý
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
myrajendra
Ìý
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
Ìý
Encapsulation anonymous class
Encapsulation anonymous classEncapsulation anonymous class
Encapsulation anonymous class
Harry Potter
Ìý
Abstract class
Abstract classAbstract class
Abstract class
Harry Potter
Ìý
Operators in java
Operators in javaOperators in java
Operators in java
Ravi_Kant_Sahu
Ìý

Similar to Interface and abstraction (20)

classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
Ìý
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
Ìý
C#2
C#2C#2
C#2
Sudhriti Gupta
Ìý
Procedure to create_the_calculator_application java
Procedure to create_the_calculator_application javaProcedure to create_the_calculator_application java
Procedure to create_the_calculator_application java
gthe
Ìý
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
Stefan Oprea
Ìý
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
Ìý
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
Naveen Sagayaselvaraj
Ìý
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
Abner Chih Yi Huang
Ìý
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
Durgesh Singh
Ìý
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
Ìý
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
rani marri
Ìý
JavaProgrammingManual
JavaProgrammingManualJavaProgrammingManual
JavaProgrammingManual
Naveen Sagayaselvaraj
Ìý
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
Ìý
Object Oriented Programming with Object Orinted Concepts
Object Oriented Programming with Object Orinted ConceptsObject Oriented Programming with Object Orinted Concepts
Object Oriented Programming with Object Orinted Concepts
farhanghafoor5
Ìý
ObjectivesÌýAssignment 09 Applications of StacksÌýCOS.docx
ObjectivesÌýAssignment 09 Applications of StacksÌýCOS.docxObjectivesÌýAssignment 09 Applications of StacksÌýCOS.docx
ObjectivesÌýAssignment 09 Applications of StacksÌýCOS.docx
dunhamadell
Ìý
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#
Umar Farooq
Ìý
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
Sudarshan Dhondaley
Ìý
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
Ìý
Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02
Sivakumar M
Ìý
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
Sbin m
Ìý
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
Ìý
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
Ìý
Procedure to create_the_calculator_application java
Procedure to create_the_calculator_application javaProcedure to create_the_calculator_application java
Procedure to create_the_calculator_application java
gthe
Ìý
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
Stefan Oprea
Ìý
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
Ìý
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
Durgesh Singh
Ìý
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
Ìý
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
rani marri
Ìý
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
Ìý
Object Oriented Programming with Object Orinted Concepts
Object Oriented Programming with Object Orinted ConceptsObject Oriented Programming with Object Orinted Concepts
Object Oriented Programming with Object Orinted Concepts
farhanghafoor5
Ìý
ObjectivesÌýAssignment 09 Applications of StacksÌýCOS.docx
ObjectivesÌýAssignment 09 Applications of StacksÌýCOS.docxObjectivesÌýAssignment 09 Applications of StacksÌýCOS.docx
ObjectivesÌýAssignment 09 Applications of StacksÌýCOS.docx
dunhamadell
Ìý
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#
Umar Farooq
Ìý
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
Ìý
Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02Object Oriented Design and Programming Unit-02
Object Oriented Design and Programming Unit-02
Sivakumar M
Ìý
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
Sbin m
Ìý

Interface and abstraction

  • 2. About Abstract Class • Abstract class in csharp is defined using "abstract" keyword • An abstract class may contain abstract methods and accesses (properties). • Non abstract methods or properties should provide actual implementation in an abstract class. • Abstract classes that cannot be instantiated mean we cannot able to create an object of an abstract class but abstract class can be implemented to child classes like a common base class. An abstract class can be partially implemented or not at all implemented.
  • 4. Abstract Method In an abstract class a method which has a keyword "abstract" and doesn't provide any implementation is called abstract method.The implementation logic of abstract methods is provided by the child classes or derived classes.Child classes use keyword "override" with same method name (as abstract method name) to provide further implementation of abstract methods.
  • 5. Non Abstract Method In an abstract class a method which doesn't have a keyword "abstract" and provide any implementation is called non abstract method.
  • 6. Why Abstract Class Abstract class enforces derived classes to provide all implementation logic for abstract methods or properties.
  • 7. create an abstract class "absCalculate" having abstract methods and non abstract methods. 01 02 03 04 05 06 07 08 09 10 11 abstract class absCalculate //A Non abstract method public int Addition(int Num1, int Num2) return Num1 + Num2; } //An abstract method, to be overridden in derived class public abstract int Multiplication(int Num1, int Num2); }
  • 8. create a class "clsCalculate" and implement it with abstract class "absCalculate". 1 2 3 4 class clsCalculate:absCalculate ...code }
  • 9. Now lets give implementation for an abstract method using override keyword implementing the abstract method 1 class clsCalculate:absCalculate 2 3 4 //using override keyword implementing the abstract method 5 public override int Multiplication(int Num1, int Num2) 6 7 return Num1 * Num2; 8 } 9 }
  • 10. create an object of class "clsCalculate" in our main program of an console application 01 class Program 02 03 04 static void Main(string[] args) 05 06 07 Console.WriteLine("Please Enter First Number"); 08 int num1 = Convert.ToInt16(Console.ReadLine()); 09 10 Console.WriteLine("Please Enter Second Number"); 11 int num2 = Convert.ToInt16(Console.ReadLine()); 12 13 absCalculate objabCal = new clsCalculate(); 14 int add = objabCal.Addition(num1, num2); 15 int multiplied = objabCal.Multiplication(num1, num2); 16 Console.WriteLine("Added Number is : 0}, Multiplied Number is : 1}", add, multiplied); 17 } 18 }
  • 12. What is an Interface? • An interface looks like a class but has got no implementation. In an interface we cannot do any implementation but we can declare signatures of properties, methods, delegates and events. • Implementation part is been handle by the class that implements the interface. Implemented interface enforces the class like a standard contract to provide all implementation of interface members. • We can implement single interface or multiple interfaces to the class. By default interfaces are public. • We declare interface by using "interface" keyword.
  • 13. interface "IEmployee" with signature of a method "DisplayEmployeeDetails". • • • • • 1 interface IEmployee{ 2 3 void DisplayEmployeeDetails(); 4 5 }
  • 14. Various Forms of implementing interface There are two of ways of implementing interfaces • Explicit • Implicit
  • 15. Explicit interface implementation • When we have two different interfaces with same method name then in that scenario we can use explicit interface implementation. • To implement an interface explicitly we have to define an interface name followed by (".") dot operator then the method name
  • 16. we have two different interfaces "IEmployee" and "ICompany" with same method name. 1 2 3 4 5 6 7 public interface IEmployee{ void DisplayEmployeeDetails(); } public interface ICompany{ void DisplayEmployeeDetails(); }
  • 17. In order to implement an interface explicitly we have to define the interface name followed by (".") dot operator before method name 1 public class Employee : IEmployee,ICompany 2 { 3 void ICompany.DisplayEmployeeDetails(){ 4 Console.WriteLine("ICompany Employee Name --> Questpond and Employee Code --> 009"); 5 } 6 void IEmployee.DisplayEmployeeDetails(){ 7 Console.WriteLine("IEmployee Employee Name --> Questpond and Employee Code --> 009"); 8 } 9 }
  • 18. create the objects of two interfaces "IEmployee" and "ICompany" in our main method of a Console Application program. 01 02 03 04 05 06 07 08 09 10 11 12 class Program{ static void Main(string[] args) { IEmployee IEmp = new clsEmployee(); IEmp.DisplayEmployeeDetails(); ICompany IComp = new clsEmployee(); IComp.DisplayEmployeeDetails(); } }
  • 21. demonstration of an interface "IEmployee" with signature of a method "DisplayEmployeeDetails". 1 2 3 4 interface IEmployee { void DisplayEmployeeDetails(); }
  • 22. create a class "Employee" and implement the interface implicitly 1 public class Employee : IEmployee 2 { 3 public void DisplayEmployeeDetails() 4 { 5 Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009"); 6 } 7 }
  • 23. create the objects of an interface "IEmployee" in our main method of a Console Application program 1 2 3 4 5 6 7 8 9 public class Program{ static void Main(string[] args) { IEmployee IEmp = new clsEmployee(); IEmp.DisplayEmployeeDetails(); } }