[ajug-members] casting

Marc Lawson lawsonma at bellsouth.net
Tue Jul 1 17:01:33 EDT 2008


If you want  the Subtractor class to be able to use the showadder()  
method in Adder then you need to have subtractor extend Adder.....

public class Subtractor extends Adder{
}


On Jul 1, 2008, at 4:12 PM, 521 wrote:

> 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




More information about the ajug-members mailing list