Category Archives: OOPs

Interface Programming

 

According to the basic principle of object-oriented, classes should have not close knowledge of each other. If you are not using interface programming, classes defiantly somehow relates to each other. If you change in one class, it will affect in other classes also so you have recompile whole code.

What is interface?

Interface is a contract between class and outside of the world. As I said contract, it means if class have a contract then class must be implement all contracts of interface. If the class does not, it has broken the contract and compiler will complain and refuse to compile the code.

Declaring the Interface

[Attributes] [Modifier] Interface identifier [: base-list]

{Interface-body}

Attributes–>The attributes section is optional and attributes can contains any declarative information

Modifiers–>the modifier section is optional. The available modifiers are new, public, protected, internal       and internal protected.

Interface–>interface is used to tell the compiler that this is an interface.

Identifier–>this is the place to put name of interface.

: Base-list–>this is place to put the name of other interface. This is optional also.

{Interface-body}–> this section is used to put the methods, properties, indexer and event associated with contract.

 Interface

 Explicit and Implicit Interface Implementations

Implicit–> in case of explicit, user can be access contract by object in which contract is implemented. If two interface have same method, then you cannot implement in one class, hence code would not be compiled.

Example:

Public interface ISpeak

{

           Void Speak ();

}

Public class Dog: ISpeak

{

Public void  Speak ()

{

}

}

Hence you can use above contract like this:

ISpeak objISpeak=new Dog ();

objISpeak. Speak ();

OR

Dog objDog =new Dog (); //so what is the use of interface here

objDog. Speak ();

 

 

Explicit–> in case of implicit, the calling way of method here is different, you can see in example. You can implement same method, are in different interface. In explicit you must omit the access modifier otherwise code will not be compiled. Because access modifier is omitted, you cannot access method from class. You have to use interface.

Example:

Public interface ISpeak1

{

           Void Speak ();

}

Public interface ISpeak2

{

           Void Speak ();

}

 

Public class Dog: ISpeak1, ISpeak2

{

Void ISpeak1. Speak ()

{

//let says this is first contract.

}

Void ISpeak2. Speak ()

{

//this is second contract.

}

 

}

Hence you can use above contract like this:

ISpeak1 objISpeak1=new Dog ();

objISpeak1. Speak (); //first contract will be called.

ISpeak2 objISpeak2=new Dog ();

objISpeak2. Speak (); //second contract will be called.

Mapping the interface

By using this technique, you can decide which method has to use. Let’s have an example:

Namespace mappingtheinterface

{

Interface IDoSomething

{

Void DoSomething ();

}

Class work1: IDoSomething

{

Public void DoSomething ()

{

//let says first method1.

//Implicit implementation

}

}

Class work2:work1,IDoSomething

{

void IDoSomething .DoSomething()

{

//let says first method2.

//this is explicit implementation

}

 

}

Public new void DoSomething ()

{

//let says first method3.

//this is called shadowing, because you are hide method of base class

}

 

}

 

 

Now let’s call method like given below:

Work2 objwork2=new work2 (); //create object of class work2

Objwork2.DoSomething (); //method3 will be called

((IDoSomething) Objwork2).DoSomething (); //method2 will be called, here object is cast o interface

((work1) Objwork2).DoSomething (); //method1 will be called, here object is cast o work1 class

 

Empty Interface?

Empty interface means, nothing in contract, then this is violation of contract. Because if you have contract, have something in contract. The beauty of empty interface, you can use this as marker or you can check data type at code compile time. Let’s we have an example:

[Serializable] //attribute

Public Interface ISerialize

{

}

And above interface implemented to class

Public class Test: ISerialize

{

}

ISerialize interface is used as marker, class is Serializable.

 

If you have questions related to this blog just write your thoughts.

Thanks for reading!

Rukhsar Ahmad