[ajug-members] casting

521 521 at ofig.org
Tue Jul 1 16:12:03 EDT 2008


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





More information about the ajug-members mailing list