[ajug-members] casting

Thomas, Dave dthomas at tandbergtv.com
Tue Jul 1 16:31:30 EDT 2008


2 ways: override toString or use interfaces.  In either case don't
couple your code to System.out

public interface Displayable{
	Public String display();
}

public class Adder implements Displayable{
	public String display(){
		int low = 0;int high = 9;int count=low;
		String retval="";
		while(count<high){retval += count + "\n";count++;}
	}
}

in main you do 
Displayable k = new Adder();
System.out.println(k.display());

-----Original Message-----
From: 521 [mailto:521 at ofig.org] 
Sent: Tuesday, July 01, 2008 1: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
**********************************************************************

This email, its content and any attachments is PRIVATE AND 
CONFIDENTIAL to TANDBERG Television, Part of the Ericsson Group. 
If received in error please notify the sender and destroy the original 
message and attachments.

www.tandbergtv.com
**********************************************************************



More information about the ajug-members mailing list