Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old July 6th, 2003, 07:56 AM
g_r g_r is offline
Registered User
Join Date: Jul 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Polymorphism and Casts

Maybe I don't really understand the definition of polymorphism yet, but I'm having a little problem with this: In section "Using Multiple Interfaces" in chapter 6 it says that after you cast a reference to a specific type, you can't get polymorphic behavior anymore.
But what if one introduces a new class, MyClass2 which extends MyClass, then creates an object of that class and stores the reference in the variable ac?
Now the statement on p. 290, ((MyClass)ac).powerOnOff(); would call powerOnOff of MyClass2, despite having casted the reference to MyClass, wouldn't it? Isn't that polymorphic behavior? Also, if one creates an instance of MyClass, stores a reference in ac, and casts to MyClass2? ((MyClass2)ac).powerOnOff(); This would compile just fine, but would result in a runtime error, since the object that ac points to isn't an instance of MyClass2. This doesn't really look like static binding. I'm quite confused, so please help me understand what's wrong here...
  #2 (permalink)  
Old July 13th, 2003, 08:38 AM
Registered User
Join Date: Jul 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts

What Ivor is saying is that if you have an object that is stored in a AbsoluteControl reference then you can't treat it as a RemoteControl object even though the underlying object is a RemoteControl object. Even though, theoretically, this would not cause a problem with the runtime you can't get it past the compiler.

You are correct that no static binding is involved. Java will do runtime binding for everything unless the class or method is final or static.

Associate Instructor - Hofstra University

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 02:36 AM
Need help with Polymorphism Piash Java Basics 1 August 8th, 2007 05:24 AM
What is Interface Polymorphism in C#? ramk_1978 C# 1 May 3rd, 2006 01:23 AM
Polymorphism->overloading? naveen J2EE 2 October 19th, 2005 02:34 AM
Polymorphism with Interfaces digby_dog VB.NET 2002/2003 Basics 2 May 11th, 2005 12:50 PM

All times are GMT -4. The time now is 11:16 PM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.