?
Solved

Method Inheritance?

Posted on 2008-10-24
3
Medium Priority
?
224 Views
Last Modified: 2013-11-12
Experts,

I'm wondering if it's possible for methods to "inherit" from one another in the sense that you can pass on the code from one method to another and then add to that.

In the midst of asking this question, I've run across the idea of passing an interface to a method.  Why would I ever pass an interface to a method?
0
Comment
Question by:NigelRocks
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
ororiole earned 1600 total points
ID: 22802061
No it is not possible for methods to inherit code from each other. Its an interesting idea but fraught with complications and cases in which in wouldn't work. For example if the base method ended with a return statement, then your derived method code would never execute.
As far as "passing an interface" you don't actually pass an interface, you pass a reference to a type that implements the interface. This is the same concept as returning a reference to a type that implements an interface.
For example, in the code below, I am returning an ArrayList, a specific type, but I am returning it as an IList. The calling method doesn't care which type of collection it gets, it is just going to call the methods or properties that belong to IList. In fact, thats all it can call.
Likewise when passing the parameter, you could pass a DataReader or your own custom type that you have defined. This has a method, in this case ManipulateData(), for your own application. GetData() cannot call it, but it can call Read() and GetInt32() and everything else that IDataReader declares.

//your custom reader
public class MyReader : IDataReader
    {
        public void ManipulateData()
        {
        }
 
        #region IDataReader Members
        //Note: snippet, not a complete implementation
        public bool Read()
        {
            throw new NotImplementedException();
        }
...
}
 
//in this class we use your custom reader, but pass it as an IDataReader
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MyReader rdr = new MyReader();
 
        IList list = GetData(rdr);
        int first = (int)list[0];
        int count = list.Count;
    }
 
    public System.Collections.IList GetData(IDataReader reader)
    {
        System.Collections.ArrayList al = new System.Collections.ArrayList();
        while (reader.Read())  //can call Read()
        {
            al.Add(reader.GetInt32(0)); //can call GetInt32()
        }
 
        //can't call reader.ManipulateData()
        return al;
    }

Open in new window

0
 
LVL 16

Assisted Solution

by:ororiole
ororiole earned 1600 total points
ID: 22802096
Now that I think about it, you can pass an interface, as a type. This would usually be done if you were writing code that operated on the type, as with reflection.

    public void UseReflection(Type type)
    {
        System.Reflection.MethodInfo info = type.GetMethod("Read");
 
        if (info != null)
        {
            //find out what this method returns.
            Type returnType = info.ReturnType;
        }
    }

Open in new window

0
 
LVL 21

Assisted Solution

by:mastoo
mastoo earned 400 total points
ID: 22821829
I might go a step further and say it is frequently desirable to pass interfaces.  Datareader is a good example.  Maybe I know the caller is really using a SqlDatareader but I don't need to use any functionality that is specific to the SqlDatareader.  It would be better to use the more generic IDatareader.

Also, say I have a class Person that implements a virtual method Eat.  Now I make a derived class PickyPerson that overrides Eat.  The pickyPerson extends the base method of Eat by examing the food first, and if it has vegetables in it the method throws an exception.  Otherwise, it calls the base person Eat.  I think this would be what you are asking about leveraging base class code.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Introduction This question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question