Solved

Method Inheritance?

Posted on 2008-10-24
3
211 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The CRUD Functions CRUD, meaning "Create, Read, Update, Delete (http://en.wikipedia.org/wiki/Create,_read,_update_and_delete)" is a common term to data base developers.  It describes the essential functions of data base table maintenance.  This art…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now