locked
Using Interface RRS feed

  • Question

  • User1459882081 posted

    Hi Folks,

    I am going to start very important and common thread. Yes, it's Interface. I am sure this will get many responses.
    I know good theoritical details about it, but I cannot think a case where I can use interface in my VB.NET code.

    Please check this code. Please ignore any coding syntax error!

    public Interface IInterface
    {
     public void method1()
    }

    class Class1 implements IINterface
    {
     ...
     method() { "Calling Class1 method()" }
     ...
    }
    class Class2 implements IINterface
    {
     ...
     method() { "Calling Class2 method()" }
     ...
    }

    main()
    {
     IInterface i1, i2;
     i1 = new Class1;
     i2 = new Class2;

     i1.method();
     i2.method();
    }

    Why we need to do so? It can be achieved without using Interface.

    I think this is enough to just get started. We'll go from when somebody provide input. Thanks.

    Wednesday, June 29, 2011 12:24 PM

All replies

  • User-843484705 posted

    Here is  example, how to use interface and implement in VB.net

    you can check here as well:http://www.java2s.com/Code/VB/Class/ImplementsanInterface.htm


    Happy Coding!

    Wednesday, June 29, 2011 12:34 PM
  • User-837620913 posted

    You would use a Factory pattern (such as Factory Method pattern) to so that in Main you would not be hard-coding which class type you are creating.

    For example, the Main method might look like this instead:

    IInterface i1, i2;
     i1 = ClassFactory.GetIInterfaceObject();  // don't know if class1 or class 2 was returned!
     i2 = ClassFactory.GetIInterfaceObject();  // don't know if class1 or class 2 was returned!

     i1.method(); // still works, calls the class1 or class2 method
     i2.method();

    Then your Main method, and indeed the object that contains main, would have no idea which class type (1 or 2) was returned from the GetIInterfaceObject() method - and doesn't care.

    Wednesday, June 29, 2011 12:35 PM
  • User-125547262 posted

    The whole idea of using an interface is to break out common behavior and break things into smaller reusable chunks .

     In your  case if you implement the interface then you know that class 1 and class 2 have the common behavior . Now if class3 comes along you just implement the interface. What I would even suggest is having an abstract class that implements this interface and your concrete classes inherit from that abstract class.

    Also take an example say you had to call the method that takes Class1 as the parameter and calls the method ; same with class 2. If you were using an interface you can have a common method like below

    public void MyMethod(IINterface intf)

    {

     

    inft.Method(); // You do not need have a specific method for each of the classes

    }

     

    Wednesday, June 29, 2011 12:41 PM
  • User1459882081 posted

    But why would I implement the interface when the Class3 comes in? Why I just simply cannot have the new class with its methods?

    Wednesday, June 29, 2011 12:53 PM
  • User-125547262 posted

    well if Class3 is expected to have the same behavior ; then it make sense to have the common behavior extracted out. Also looking at the code it  is immediately obvious to the developer that there is common behavior across all these classes

    Wednesday, June 29, 2011 12:56 PM
  • User1459882081 posted

    To simplify this....what benefit does the interface offer agains having classes with their methods withoud implementing an interface? In case of implementing an Interface, we are introducing a complexity of implementing them agains just classes with their methods!

    Wednesday, June 29, 2011 1:01 PM
  • User-125547262 posted

    It is all about reusability  .. see this article

    http://artofsoftwarereuse.com/2010/04/24/5-advantages-of-using-interfaces-for-reusable-assets/

    Wednesday, June 29, 2011 1:15 PM
  • User1459882081 posted

    I still don't understand how it helps in reusability.

    Any example that would show how it helps will be appreciated.

    Tuesday, July 5, 2011 4:43 PM