1. Exact Use of Interface a. Interfaces are used to define the contract which has the implementation in the class it is implemented. b. An interface looks like a class, but has no implementation. The only thing it contains is definitions of events, indexers, methods and/or properties. The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined. c. They're great for putting together plug-n-play like architectures where components can be interchanged at will. Since all interchangeable components implement the same interface, they can be used without any extra programming. The interface forces each component to expose specific public members that will be used in a certain way. d. Interfaces must be implemented by derived classes and structs, they define a contract, which guarantees that the methods inside the interface will be implemented. 2. Can two methods with same name and different signatures reside in the same interface? a. No b. Example: This will give a compile time error.
namespace OOPS { interface IExample1 { String Add(); void Add(); void Update(); void Delete(); } }
3. Are all methods from the implemented interface necessary to be implemented in the inherited class a. Yes b. Example:
namespace OOPS { class Class1 : IExample1 { public String Add() { Console.WriteLine("Called the interface method ADD"); Console.ReadLine(); return ""; }
public void Update() { Console.WriteLine("Called the interface method Update"); Console.ReadLine(); }
public void Delete() { Console.WriteLine("Called the interface method Delete"); Console.ReadLine(); } } }
4. What if two interfaces with different name and same methods are inherited in the same class a. Yes b. Example 1: No need to specify the access modifier if explicitly mentioned the Interface name while declaring the implementation of the interface method (void IExample1.Update()).
namespace OOPS { class Class1 : IExample1, IExample2 { String IExample1.Add() { Console.WriteLine("Called the interface method ADD"); Console.ReadLine(); return ""; } void IExample1.Update() { Console.WriteLine("Called the interface method Update"); Console.ReadLine(); } void IExample1.Delete() { Console.WriteLine("Called the interface method Delete"); Console.ReadLine(); }
public void Add() { Console.WriteLine("Called the interface method ADD2"); Console.ReadLine(); } public void Update() { Console.WriteLine("Called the interface method Update2"); Console.ReadLine(); } public void Delete() { Console.WriteLine("Called the interface method Delete2"); Console.ReadLine(); } } }
c. Example 2: This will generate a compile time error as two methods from different interfaces having common method name are not allowed. To prevent this see Example 1
namespace OOPS { class Class1 : IExample1, IExample2 { public String Add() { Console.WriteLine("Called the interface method ADD"); Console.ReadLine(); return ""; }
public void Add() { Console.WriteLine("Called the interface method ADD2"); Console.ReadLine(); }
public void Update() { Console.WriteLine("Called the interface method Update"); Console.ReadLine(); }
public void Delete() { Console.WriteLine("Called the interface method Delete"); Console.ReadLine(); } } }
5. Calling interface a. Example 1: Class1 cls1 = new Class1(); IExample1 Ex1 = (IExample1)cls1; Ex1.Add(); b. For Ex1 only those method will be allowed to call those are declared in interface 6. Can the method implementation of interface be other than public a. No b. Example
namespace OOPS { class Class1 { public void Add() { Console.WriteLine("Called the interface method ADD"); Console.ReadLine(); } public void Update() { Console.WriteLine("Called the interface method Update"); Console.ReadLine(); } public void Delete() { Console.WriteLine("Called the interface method Delete"); Console.ReadLine(); } } }
7. Can interfaces be initiated like classes using the new keyword? a. No. b. Will generate an compilation Error c. For Example:
namespace OOPS { abstract class Class1 { IExample1 IEx1 = new IExample1(); } }
8. Can interface inherit from an interface a. Yes b. Example:
namespace OOPS { interface IExample1 : IExample2 { void Add(); void Update(); void Delete(); }
interface IExample2 { void Add(); void Update(); void Delete(); } }
9. Can a interface inherit a class a. No b. Example:
namespace OOPS { interface IExample1 : OOPS.Class2 { void Add(); void Update(); void Delete(); } }
10. Can the methods of the inherited interface be declared as abstract a. Yes b. Example:
namespace OOPS { abstract class Class1 : IExample1 { public abstract void Add(); public abstract void Update(); public abstract void Delete(); } }
11. Is it necessary to implement all the methods from one interface inherited from another interface a. No b. Example:
namespace OOPS { interface IExample1 : IExample2 { void Add(); void Delete(); }
interface IExample2 { void Add(); void Update(); void Delete(); } }
12. Can a inherited interface have non public access modifiers a. No. b. Example:
namespace OOPS { abstract class Class1 : IExample1 { public void Add() { Console.WriteLine("Called the interface method ADD"); Console.ReadLine(); } public void Update() { Console.WriteLine("Called the interface method Update"); Console.ReadLine(); } public void Delete() { Console.WriteLine("Called the interface method Delete"); Console.ReadLine(); } } }
|
| Author: Mahesh Raj 07 Jun 2008 | Member Level: Gold Points : 1 |
This is very good information,Continue posting such useful articles.
|
| Author: John Fernandez 08 Jun 2008 | Member Level: Gold Points : 1 |
Very well written Article.Thanks for sharing this information.
|
| Author: Ranjan Palai 25 Aug 2008 | Member Level: Bronze Points : 0 |
This is very useful information,Continue posting....
|