[ajug-members] casting

Wilson, Jeff jw9615 at att.com
Tue Jul 1 16:33:09 EDT 2008


You absolutely can only cast up or down an inheritance hierarchy OR cast
an object to any interface that it or its parent classes implements. You
cannot cast, in your example, a Subtractor object to be an Adder object.
That makes no sense in an object oriented language.

Casting doesn't change the type of the object, it just narrows the array
of types that the object already implements.

+jeff (MJW)
---

"The only way to deal with bureaucrats is with stealth and sudden
violence."
      -- B. Boutros-Ghali

---------------------------------------------
M. Jeff Wilson, Lead Member Technical Staff
AT&T Services, Inc.
Operations & Service Dev
jw9615 at att.com 
+1 404.499.7235
> -----Original Message-----
> From: 521 [mailto:521 at ofig.org]
> Sent: Tuesday, July 01, 2008 4:12 PM
> To: ajug-members at ajug.org
> Subject: [ajug-members] casting
> 
> can I only cast up/down a hierarchy?
> 
> per below, what would the syntax be in order for a 'Subtractor' object
use
> the Adders classes' showadder method?  would it be like this- to cast
> Adder 'type' on the sub object- that is, in order to get to an Adder's
> method? (Adder)sub.showadder
> 
> 
> [code]
> public class ProgramDisplayer {
> 	public static void main(String args[]) {
> 		System.out.println("Hi!");
> 
> 		Adder add = new Adder();
> 		add.showadder();
> 
> 		Subtractor sub = new Subtractor();
> 		sub.showsubtractor();
> 	}
> }
> [/code]
> [code]
> public class Adder{
> 	public void showadder() {
> 		int low = 0;int high = 9;int count=low;
> 		while(count<high){System.out.println(count);count++;}
> 	}
> }
> [/code]
> [code]
> public class Subtractor {
> 	public void showsubtractor() {
> 		int low = -10;int high = 0;int count=high;
> 		while(count>low){System.out.println(count);count--;}
> 	}
> }
> [/code]
> 
> Thanks,
> 
> Barclay
> 
> 
> 
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621





More information about the ajug-members mailing list