Article View: borland.public.jbuilder.java.language
Article #17521Re: Dynamic method binding
From: Steve Schafer (TeamB)
Date: Fri, 06 Nov 1998 00:00
Date: Fri, 06 Nov 1998 00:00
26 lines
1489 bytes
1489 bytes
On 6 Nov 1998 11:42:34 GMT, "Vishal P Mahabeer" <vishalm@spl.co.za>
wrote:
>Supposing I have a Hashtable with key values of type String and value
>values of type CustomFrame (I defined this class). Consider the following
>method:
>
>public CustomFrame getValue(String key) {
> return this.hashTable.get(key);
>}
>
>This will not work because the method 'get(key)' returns an object of type
>Object and not CustomFrame. I need the CustomFrame object so that I can
>invoke methods on it.
Use a cast:
return (CustomFrame)this.hashTable.get(key);
This does two things: It tells the compiler to treat the result of
this.hashTable.get(key) as a CustomFrame, and it also causes the
insertion of some type-checking code that will raise an exception if
this.hashTable.get(key) returns something that isn't compatible with
CustomFrame.
-Steve
Technical Details
Message-ID:
<364c5683.73458309@90.0.0.40>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<01be08c8$d3d2edd0$c60924c4@vishalm> <364fd49f.36318590@forums.borland.com> <01be097a$c4239940$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: pandeng@telepath.com (Steve Schafer (TeamB)) Subject: Re: Dynamic method binding Date: 1998/11/06 Message-ID: <364c5683.73458309@90.0.0.40> X-Deja-AN: 409131346 Content-Transfer-Encoding: 7bit References: <01be08c8$d3d2edd0$c60924c4@vishalm> <364fd49f.36318590@forums.borland.com> <01be097a$c4239940$c60924c4@vishalm> Content-Type: text/plain; charset=us-ascii Organization: TeamB Mime-Version: 1.0 Reply-To: pandeng@telepath.com Newsgroups: borland.public.jbuilder.java.language