🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #17506

Re: Dynamic method binding

#17506
From: Chris Hill
Date: Thu, 05 Nov 1998 00:00
42 lines
1741 bytes
Given an Object O, a String containing the method name AND an array of
the parameter types (Classes):

1) Get the Class of O, call it C
2) Call C.getMethod(method name, array of parameter types) to get a
Method object, call it M
3) Call M.invoke(object to call method on, array of parameters) on the
Method object  

So the key methods are:
Object.getClass()
Class.getMethod(...)
Method.invoke(...)

On 5 Nov 1998 14:28:44 GMT, "Vishal P Mahabeer" <vishalm@spl.co.za>
wrote:

>Hi.
>
>Consider the following extract of code:
>
>1	public void addToDict (String key, Object value) {
>2		dict.put (key, value);		//dict is an instance of Hashtable
>3		value.setController (this);
>4	}
>
>In Line 1 (Object value):  I want this to be a place holder for an object
>that can be one of several classes.  Therefore, Line 3
>(value.setController) should invoke the method setController on whatever
>the 'value' object turns out to be.  The reason I use Object value in Line
>1 is that at compile time I do not yet know what that object will be, and
>it may change from one invocation to another.
>
>Is there a way to allow the method to dynamically be invoked on the 'value'
>object?
>
>Thanks
>Vishal
>vishal.mahabeer@ddbs.co.za

Chris Hill
hillca@cs.purdue.edu
Technical Details
Message-ID: <3641e319.1364079953@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01be08c8$d3d2edd0$c60924c4@vishalm>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,db2d30fe5814e4cb
X-Google-Attributes: gidf74b3,public
From: hillca@cs.purdue.edu (Chris Hill)
Subject: Re: Dynamic method binding
Date: 1998/11/05
Message-ID: <3641e319.1364079953@forums.borland.com>
X-Deja-AN: 408728274
References: <01be08c8$d3d2edd0$c60924c4@vishalm>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.language