Tuesday, 18 October 2016

Interface

Interface

Interface are implemented by class or structure , it is a contract , how a class or structure will behave .Any object implement interface will make sure of implement property defined in the interface . An interface property have to define in the same data type , parameter and same property .For example an interface A has property calculateAge with return type integer .Now I am implementing interface A on class B , I have to declare calculateAge  with return type integer . either compiler will through error  “does not implement interface member Interfaceimplement.A.calcualtedag int”

A simple interface with no method

public interface MyInterface
{

}

A Simple class with Interface

public class MyClass : MyInterface
{

}
Now I am writing a interface with method

public interface MyInterface
{
    void MySuv(int x)
    {
    }
}
Now a class with Interface implementation

public class Myclass : MyInterface
{
    public void MySub (int x)
    {
    //do you own logic
    }
}
Some points to remember


  • Interface is a point where two point , organization subject together
  • Interface can not have a definition , that is we can not write body of Interface member
  • We ca not declare variable in interface
  • Interface  can not have fields
  • All interface method have to define in implemented class
  • We can not define access modifier on interface method
  • We can implement interface to Abstract class
  • We can apply access modifier on interface
What is use of Interface then ?
    Generally developer write class method different name with same implementation . For example a developer has declared a method name "Save" to class A , later he is writing "SaveData" in class B.But both purpose is same .Interface allow to overcome this problem.


Implementation Interface –Now we are discussing how method of Interface is implemented in class or structure , the first step define method with same name and signature in the class and do implementation.

public interface MyInterface
{
    void calculateAge(DateTime Date_of_Birth)
    {
    }
}
public class student : MyInterface
{
    public void calculateAge(DateTime Date_of_Birth)
    {
        int t_year=;//calculation logig here
    }
}

Multiple Interface Implementation : In.Net , it is not possible to implement multiple inheritance ,but it is possible to implement multiple Interface .


public interface MyInterface1
{
    void calculateAge(DateTime Date_of_Birth)
    {
    }
}

public interface MyInterface2
{
    void calculateMarks()
    {
    }
}


public class student : MyInterface1, MyInterface2
{
    public void calculateAge(DateTime Date_of_Birth)
    {
        int t_year=;//calculation logig here
    }
     public void calculateMarks()
    {
        int t_year=;//calculation logig here
    }
}

You need to declared all construct of the both interface .What happen when Multiple Interfece has same named  method , for example Interface1 and Interface2 has same name method A , what will happen when both has same parameter & same signature.C# provide us the way to tell compiler , with method to use.During calling , tell compiler the Interface name , then method name , for Example


public interface A
{
    void method1(DateTime Date_of_Birth)
    {
    }
}

public interface B
{
    void method1(DateTime Date_of_Birth)
    {
    }
}


public class student : A,B
{
    public void A.method1(DateTime Date_of_Birth)
    {
        int t_year=;//calculation logig here
    }
     public void B.method1(DateTime Date_of_Birth)
    {
        int t_year=;//calculation logig here
    }
}


Now the common question is what is the difference between Abstract Class and Interface .Why we should use Abstract Class and why we should Interface ?  here is the points .A class can inherits only single Abstract Class but a class can inherits multiple Interface In Abstract can contain contact , method , sub , but Interface can contain method ,sub.In Abstract Class , we can access modifier public , private , protected but Interface can not have such option.


No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...