none
Inheritance and Interface real world example RRS feed

  • Question

  • Hi,

    I know what is the difference between the two but I could not come with a real life scenario where Interface can be used and where inheritance(superclass-subclass) can be used.

    An interface is similar to an abstract Super class. However, Interface support multiple inheritance. But in case i dont want to use multiple inheritance, then, where to use what?

    Please help me out.

     

    Thanks in advance.


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    • Edited by Tiya01 Thursday, January 19, 2012 5:18 AM
    Thursday, January 19, 2012 4:25 AM

All replies

  • Just try to relate as

    Interface - Has a relationship

    Inheritence -Is a relationship

    As in realworld we take an example of a CAR class

    Car inherits Vehicle (any car is a vehicle) that means all the properties of Vehicle will be inherited

    Car implements Navigation(interface)

    Car may or may not have a Navigation, if it implements it, it will have all the feature of the Navigation else it wont have.

     

     

    Thursday, January 19, 2012 8:04 AM
  • There are entire chapters and many examples just on these topics so it is difficult to explain well in a few sentences.

    The main thing to remember when comparing Interfaces vs Abstract Classes is that Interfaces have no implementation code and Abstract Classes can have partial implementation details. This is important, because sometime you want the ability to dictate how the method or property will behave to inherited classes, and this is where and Abstract class is perfect. However there are times when just defining the contract or Interface is needed, and you want the implementer of the Interface to define the details.

    One of my favorite examples for using an Interface is for Repository methods. Let's say I define the following Interface:

    Public Interface IRepository
    
    Funtion GetAll() As List (Of MyType)
    
    Sub Save()
    
    End Interface
    
    
    

    Let's say I had (2) databases: 1 SQL and 1 Oracle. I could have 2 separate implementations but the calling method just knows all it needs to do is call IRepository.Save().

    Public Class SQLRepository
        Implements IRepository
    
        Public Function GetAll() As System.Collections.Generic.List(Of String) Implements IRepository.GetAll
            'SQL Implementation code
        End Function
    
        Public Sub Save() Implements IRepository.Save
            'SQL Implementation code
        End Sub
    End Class
    
    Public Class OracleRepository
        Implements IRepository
    
        Public Function GetAll() As System.Collections.Generic.List(Of String) Implements IRepository.GetAll
            'Oracle Implementation code
        End Function
    
        Public Sub Save() Implements IRepository.Save
            'Oracle Implementation code
        End Sub
    End Class
    

    And here would be an example of being able to call the method knowing that a Save will occur, but needing to know how it is done:

    Public Sub SaveDataExample()
     _myRepository.Save()   
    End Sub
    

    The idea here is to program to an abstraction rather than a concrete implementation. Interfaces provide polymorhic behavior which is a key Object Oriented concept. If all of this does not click or make sense just yet that is OK. It takes time for some of the more advanced programming techniques to become apparent. I recommend reading some books on Object Oriented Programming. The links below also have nice brief samples and explination as well.

    Interface-Based Polymorphism:
    http://msdn.microsoft.com/en-us/library/z9k8e08x(v=vs.90).aspx

    Abstract Classes:
    http://msdn.microsoft.com/en-us/library/k535acbf(VS.71).aspx

    Hope this helps! :)


    Thank you,
    • Proposed as answer by atconway Thursday, January 19, 2012 10:31 PM
    Thursday, January 19, 2012 10:31 PM
  • Hi everyone,

    Thanks for your valuable responses.

    I know the basic difference between the two but I often get confused when ppl ask this way :

    • I don need multiple inheritance
    • I use a complete abstract class
    • All the classes that extends that base abstract class will implement methods in their own way.
    • All the methods are implemented.

    Then this way abstract class is acting as a interface. Then when to use which? I am dead struck at this point. May be a real world example can help.

    Please guide.

    Thanks!

     


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    Tuesday, January 31, 2012 8:08 AM
  • Look at my responses below in bold:

    • I don need multiple inheritance - Well multiple inheritance is not allowed in .NET so this is not possible anyways. Implementing multiple Interfaces is possible.
    • I use a complete abstract class - ?
    • All the classes that extends that base abstract class will implement methods in their own way. - Typically when a method is marked as MustOverride (VB.NET) or Virtual (C#). See this link for more help on Access Modifiers in .NET: Modifiers (C# Reference)
    • All the methods are implemented. - This could still be an Abstract class where all methods were marked as MustOverride, but sounds more like an Interface where all methods are implmented.

    Interfaces and Abstract Classes, 2 key components of OOP, are difficult to understand in just a few sentances. I remember when I was trying to iron out uses of Interfaces several years ago and it took some time. I did provide you with a real world example of using Interfaces before. However, if this did not help enough, I really recommend reading a .NET Object Oriented Book. It will cover in depth Interfaces and Abstract Classes with real world examples.

    Head First Object-Oriented Analysis and Design:
    http://www.amazon.com/Head-First-Object-Oriented-Analysis-Design/dp/0596008678

    Professional ASP.NET Design Patterns:
    http://www.amazon.com/Professional-ASP-NET-Design-Patterns-Millett/dp/0470292784/ref=sr_1_1?s=books&ie=UTF8&qid=1328022160&sr=1-1

    The latter book above says ASP.NET, but has an overall teaching of patterns and OOP as applied to .NET and is still a book I recommend.


    Thank you,

    • Edited by atconway Tuesday, January 31, 2012 3:03 PM
    Tuesday, January 31, 2012 3:03 PM