Results 1 to 7 of 7

Thread: [RESOLVED] Understanding Interfaces

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Resolved [RESOLVED] Understanding Interfaces

    I'm trying to fully understand the interface concept so bare with me

    let's say i created this interface:
    Code:
    public interface INameable
        {
             string FirstName { set; get; }
             string LastName { set; get; }
             string GetLastNameFirst();
        }
    and implement its properties and methods in this class:

    Code:
     public class entity : INameable
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string GetLastNameFirst()
            {
                return String.Format("{0} {1}", FirstName, LastName);
            }
        }
    now i want to implement this interface in another class but i don't want to
    re-write "GetLastNameFirst" method since i already wrote it in the above class
    (entity)

    so how do i re-use that method in my new class:
    Code:
     public class Worker : INameable
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
    
           // here i don't want to re-write the method, i just want to re-use the
           // method i wrote in the above entity class)
            public string GetLastNameFirst()
            {
                return "???";
            }
    
        }

    any pointers or direction will be appreciated!
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Understanding Interfaces

    This isn't really anything to do with interfaces. Your question is about inheritance. You want your Worker class to inherently have the same GetLastNameFirst implementation as your 'entity' class? Then Worker must inherit 'entity'. That's all there is to it.

    Your example is a bit contrived so it's a bit hard to transfer to a real-world scenario, but you might consider the way the .NET Framework handles similar situations. For instance, in order to be displayed in the VS Toolbox window, a class must implement the IComponent interface. Rather than implement IComponent directly though, most classes inherit the Component class, which is a simple, concrete implementation of the IComponent interface. Each class then overrides those members of Component that it needs to, if it needs to.

    In your case, you might define an abstract class that provides an implementation for GetLastNameFirst but declares FirstName and LastName abstract. Other classes could then inherit that class and implement just the properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Understanding Interfaces

    This is the reason i opened this thread, I still don't understand why and when should i use interfaces. i had the same problem few month ago when i learned inheritances things was still messy for me. I do understand and know how to make the above example to work when i use inheritance but i thought that i will need to use interfaces when i want to share methods / properties when i have bunch of classes that not share the same class hierarchy.

    I do see the usefulness of interfaces such as IEnumerable,IComparable and ICloneable but yet i don't understand how to make interfaces as useful as those interfaces.

    right now i'm just trying to really understand interfaces and its logic.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Understanding Interfaces

    I think the issue here is something that you know but something you don't REALLY know. Interfaces don't provide an implementation of the members. You're trying to make multiple types share an implementation of a member, so an interface can't help you. Only classes can provide implementation so only a common base class can provide a common implementation.

    The whole point of interfaces is that they don't provide implementation. They describe exactly how an object will look from the outside but nothing about how it will look on the inside. The main reason that they exist is for the sake of polymorphism. Think about the foreach statement. You can use a foreach statement to enumerate any object at all, as long as it implements the IEnumerable interface. In fact, the underlying infrastructure has no idea what type the object is beyond that interface. It doesn't care because it doesn't have to care.

    Another example is data-binding. In WinForms, all that any control cares about is that the data source is an IList or an IListSource. Beyond that, it has no interest.

    Basically, you trying to get an interface to make two types do the same thing when what interfaces are really about is getting two types that do different things to do them through members that look the same from the outside.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Understanding Interfaces

    Ok, i do understand that interfaces do not provide implementation and only supply abstract methods/ properties, but if we'll take your example of of the foreach, in the examples i saw when a class implement the IEnumerable interfaces it didn't had to re-define the all MoveNext,Current,Reset methods of the IEnumerator interfaces, they just used the GetEnumerator methods that already defined in the Array class.

    Anyway I'm sure something i still not set correctly in my mind about interfaces and i still need to understand and read more about this subject.

    if you have something to add that may help me to get there please do.

    as always, thanks a lot for the help JM
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Understanding Interfaces

    Quote Originally Posted by motil View Post
    Ok, i do understand that interfaces do not provide implementation and only supply abstract methods/ properties, but if we'll take your example of of the foreach, in the examples i saw when a class implement the IEnumerable interfaces it didn't had to re-define the all MoveNext,Current,Reset methods of the IEnumerator interfaces, they just used the GetEnumerator methods that already defined in the Array class.
    You're misreading the situation. What members does the IEnumerable class define? GetEnumerator and that's it. Any class you've seen that implements IEnumerable has implemented GetEnumerator, I guarantee you.

    What you're talking about is IEnumerator, which is completely different interface. The fact that you don't have to define a class that implements IEnumerator is completely irrelevant to implementing IEnumerable. If you want to implement IEnumerable then you have to implement every member of IEnumerable, i.e. GetEnumerator.

    The fact that you can create that implementation by calling GetEnumerator on an array is purely a function of the fact that you're using an array to store the data within your class. If you were using some other data structure to store the data internally then you may well have to define your own class that implemented IEnumerator as well.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Understanding Interfaces

    thanks JM, tough the subject is still not 100% clear to me (like everything else only with time and practice) you sure made it much more understandable for me.

    I'll keep reading the practicing the subject in the next few days to get better understanding.

    thank a lot.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width