PassPort Chapt 3




OverLoading Classes / Objects from outside the Inner Class / Anonymous Inner Classes / Bottom of Page


Example of OverLoading Class Methods.

/*
 * Created on Dec 28, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package TestPackage;

/**
 * @author dehueng
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class Adder
{
	public int addNumbers(int x, int y)
	{
		return x + y;
	}
	public double addNumbers(double x, double y)
	{
		return x + y;
	}
}

/*
 * Created on Dec 28, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package TestPackage;

/**
 * @author dehueng
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class testAdder
{
	public static void main(String[] args)
	{
		Adder a = new Adder();
		int ix = 23;
		int iy = 77;
		int intResults = a.addNumbers(ix,iy);
		System.out.println("Output from Int OverLoad => " + intResults);
		
		double dx = 23;
		double dy = 77;
		double doubleResults = a.addNumbers(dx,dy); 
		System.out.println("Output from Dbl OverLoad => " + doubleResults);
	}
}

Results:

Output from Int OverLoad => 100
Output from Dbl OverLoad => 100.0



Creating Inner Class Objects from outside the Inner Class.

/*
 * Created on Dec 28, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package TestPackage;

/**
 * @author dehueng
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class OuterInner
{
	int doStuff(final String s, int i)
	{
		int x = 23;
		final int y = 45;
		class myInner
		{
			//Can use s & y not i  x
			int intResults = (y + 11);
			
			//println("Output from Int OverLoad => " + intResults);
			//System.out.println("String s  => " + s);
		}
		myInner mi = new myInner();
		System.out.println("Results from mi => " + mi.intResults);
		return mi.intResults;
	}
	
	public static void main(String[] args)
	{
		OuterInner oi = new OuterInner();
		System.out.println("Results from mi => " + oi.doStuff("ssssssss", 33));
	}
}

Results:

Results from mi => 56
Results from mi => 56



Anonymous Inner Class:

/*
 * Created on Dec 27, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package TestPackage;

/**
 * @author dehueng
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class Dog extends animal
{
	private String name = "";
	public Dog(String cname)
	{
		System.out.println("Name " + cname);
	}	
	
	public Dog()
		{
			System.out.println("Mutt");
		}	
	
	public void makeNoise()
	{
		System.out.println("Dogs Bark");
	}
	
	
	public void eat()
	{
		System.out.println("Dogs Eat Meat");
	}
	
	void setName(String name)
	{
		this.name = name;
	}
	
	String getName()
	{
		return this.name;  
	}

	/**
	 * @param vet
	 */
	public void giveShot(Vet vet)
	{
		// TODO Auto-generated method stub
		
	}
	
}

/*
 * Created on Dec 28, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package TestPackage;

/**
 * @author dehueng
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public interface Vet
{
	public void innoculate();
}

/*
 * Created on Dec 28, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package TestPackage;

/**
 * @author dehueng
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class Unusual
{
	public void doStuff()
	{
		Dog d = new Dog();
		d.giveShot(new Vet() {
		public void innoculate() {
		System.out.println("Vet giving a shot");
		}
		});		
	}
	public static void main(String[] args)
	{
		Unusual ugly = new Unusual();  	
	}
}

Results:




Top of Page

URL :JavaPassport3.html

Revised:1/06/2005

Layout, design & revisions David E. Huenger