Solved

Method Inheritance?

Posted on 2008-10-24
3
220 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 400 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 400 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 100 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article discusses the Chain of Responsibility pattern, explaining What it is;Why it is; andHow it is At the end of this article, I hope you will be able to describe the use and benefits of Chain of Responsibility.  Backgrou…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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