Solved

Method Inheritance?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

20 Experts available now in Live!

Get 1:1 Help Now