Solved

Method Inheritance?

Posted on 2008-10-24
3
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction Many of the most common information processing tasks require sorting data sets.  For example, you may want to find the largest or smallest value in a collection.  Or you may want to order the data set in numeric or alphabetical order. …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 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