Upload
abdur-rehman-muhammadi
View
26
Download
0
Tags:
Embed Size (px)
Citation preview
C# Simplified / Session 3 / 1 of 28
What is interface
Presented by: M. AHMAD AWAN ROLL #25 ZAHID SIDDIQUE ROLL # 38 PRESENTED TO: SIR KHALID MEHMOOD
C# Simplified / Session 3 / 2 of 28
Interfaces
• An interface is a pure abstract base class• It can contain only abstract methods and no method
implementation• A class that implements a particular interface must
implement the members listed by that interface
public interface IFile{ int delFile(); void disFile();}
C# Simplified / Session 3 / 3 of 28
Interfaces - Examplepublic interface IFile{ int delFile(); void disFile();}public class MyFile : IFile{ public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); }}
C# Simplified / Session 3 / 4 of 28
Interfaces - Output
class InterfaceDemo{ public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); }}
public class BaseforInterface{ public void open() { System.Console.WriteLine ("This is the open method of BaseforInterface"); }}
C# Simplified / Session 3 / 5 of 28
Interfaces – Example with Inheritance
public interface IFile{ int delFile(); void disFile();}public class BaseforInterface{ public void open() { System.Console.WriteLine ("This is the open method of BaseforInterface"); }}
C# Simplified / Session 3 / 6 of 28
Interfaces – Example with Inheritance
public class MyFile : BaseforInterface, IFile
{ public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); }}
C# Simplified / Session 3 / 7 of 28
Interfaces – Output for Example with Inheritance
class Test{ static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.open(); }}
C# Simplified / Session 3 / 8 of 28
Multiple Interfaces
• C# allows multiple interface implementations
public interface IFileTwo{ void applySecondInterface();}
C# Simplified / Session 3 / 9 of 28
Multiple Interfaces - Example
public class MyFile : BaseforInterface, IFile, IFileTwo{ public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } public void applySecondInterface() { System.Console.WriteLine ("ApplySecondInterface Implementation!"); }}
C# Simplified / Session 3 / 10 of 28
Multiple Interfaces - Output
class MultipleInterfaces{ public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.open(); objMyFile.applySecondInterface(); }}
C# Simplified / Session 3 / 11 of 28
Explicit Interface
• Explicit interface implementation can be used when a method with the same name is available in 2 interfaces
public interface IFile{ int delFile(); void disFile();}public interface IFileTwo{ void applySecondInterface(); void disFile();}
C# Simplified / Session 3 / 12 of 28
Explicit Interface – Contd…
public class MyFile : BaseforInterface, IFile, IFileTwo{... void IFile.disFile() { System.Console.WriteLine ("IFile Implementation of DisFile"); } void IFileTwo.disFile() { System.Console.WriteLine ("IFileTwo Implementation of DisFile"); }..}
C# Simplified / Session 3 / 13 of 28
Interface Inheritance
• New Interfaces can be created by combining together other interfaces
• The syntax for this is similar to that used for inheritance, except that more than one interface can be merged to form a single interface.
interface IAllFile : IFile, IFileTwo{ //More operations can be added if necessary //(apart from that of IFile & IFileTwo)}
C# Simplified / Session 3 / 14 of 28
Summary
• Virtual functions are useful when we need to call the derived class method from an object of the base class.
• The difference between overriding and polymorphism is that in polymorphism, the decision as to which method to call is made at runtime.
• Abstract Base Classes are classes that contain at least one abstract member (method without implementation). New instances of abstract base classes cannot be created.
• The method with no implementation is known as an operation.
• An interface is a pure abstract base class. It can contain only abstract methods, and no method implementations.
• A class can implement more than one interface; in fact, a class can inherit from another class as well as implement an interface.