Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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...
 
Old July 13th, 2003, 08:38 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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




Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 03: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.