Package and interface example

Assignment Problem:
Create a class named Circle which can take radius as constructor parameter, and has methods which calculates and returns diameter, circumference and surface area. Write another class named MainCircle, which has main method, and creates an object of Circle, and invokes all methods of Circle class, and print the return values?

Solution:

package p2;

public interface ICircle {
public final float pi=3.14f;
   public float getDiameter();
   public float getcircumference();
   public  float getarea();    
}
package p1;
import p2.ICircle;
public class CircleImpl implements ICircle{
        float radius;
    public CircleImpl(float radius){
        this.radius=radius;
    }
    public float getDiameter(){
        float diameter=2*radius;
        System.out.println("The diameter is: "+diameter);
        return diameter;
    }
    public float getcircumference(){
        float circumference=2*pi*radius;
        System.out.println("The circumference is: "+circumference);
        return circumference;
    }
    public float getarea(){
        float area=pi*radius*radius;
        System.out.println("The area is: "+area);
        return area;
    }
}

Below is main method, which creates object of CircleImpl class, and invokes its methods

package p3;
import p1.CircleImpl;
public class MainCircle {
    public static void main(String[]args){
CircleImpl obj=new CircleImpl(3f);
obj.getDiameter();
obj.getcircumference();
obj.getarea();
    }
}

Click here to download this code, on Eclipse
You may also like to read:
What is a package
What are access specifiers

Difference between Anonymous object and anonymous class

Below is an example of Anonymous object.
Anonymous object is an object without name. Generally anonymous object is used when only one method need to be invoked with the object, and invoking methods with the created object may not be further required in the program.

class InnerDemo
{
	void show(){
		System.out.println("show() in InnerDemo class");
	}
}

public class AnonymounsInnerClassObject {
	public static void main(String args[])
	{
		new InnerDemo().show();
	}
}

Anonymous Inner class is a class which does not have name, however there are some constraints like explicit constructor cannot be used with Anonymous Inner class.

Anonymous object or named object can be created using an Anonymous class.

Below is an example of creating Anonymous object of Anonymous class.

class InnerClass{

	public void met1()
	{
		System.out.println("met1() in InnerClass");
	}
}

public class AnonymousObj1 {
	public static void main(String args[])
	{
		new InnerClass(){
			public void met1(){
				System.out.println("met1() in Anonymous class");
			}
		}.met1();
	}
}

Below is an example of creating named object of Anonymous class.

class InnerClass1{

	public void met1()
	{
		System.out.println("met1() in InnerClass1");
	}
}

public class NamedObj {
	public static void main(String args[])
	{
		InnerClass1 obj = new InnerClass1(){
			public void met1(){
				System.out.println("met1() in Anonymous class");
			}
		};

		obj.met1();
	}
}

You may also like to read:
What is static inner class?
Create Thread by extending anonymous class from Thread class
What is inner interface?

Create Thread using Anonymous inner class

As briefed, a Thread is light weight process.

Below is an example, on how to create and start Thread, using Anonymous inner class.

Create Anonymous inner class, which extends from java.lang.Thread and override public void run() method.

public class ThreadAnonymousInnerClass {
	public static void main(String args[])
	{
		Thread t1=new Thread(){
		public void run(){
		for(int i=0;i<=10;i++){
		try{
		System.out.println("Thread t1: "+i);
		Thread.sleep(100);  //100 milliseconds
		}catch(InterruptedException e){
		System.out.println("Exception:"+e.getMessage());
		}
		}
		}
		};

		t1.start();

		for(int i=0;i<=10;i++){
		try{
		System.out.println("Main Thread: "+i);
		Thread.sleep(100);  //100 milliseconds
		}catch(InterruptedException e){
		System.out.println("Exception:"+e.getMessage());
		}
		}
	}
}

Create Anonymous class by implementing Runnable interface

How to create Thread, using anonymous class for Runnable. In below example, we are creating object of an anonymous class which implements Runnable interface.

public class ThreadAnonymousRunnable {
	public static void main(String args[])
	{
		new Thread( new Runnable() {
		    public void run() {

              for(int i=0;i<10;i++)
    	      {
    		System.out.println("child_thread: "+i);
    		try
    		{
    			Thread.sleep(100);
    		}catch(InterruptedException ie){
    			System.out.println(ie.getMessage());
    		}
    	     }
		    }
		}).start();

		for(int i=0;i<10;i++)
    	        {
    		System.out.println("main_thread: "+i);
    		try
    		{
    			Thread.sleep(100);
    		}catch(InterruptedException ie){
    			System.out.println(ie.getMessage());
    		}
    	        }
	}
}

Further reducing size of code

public class ThreadAnonymousRunnable {

	private static void thread_body(String thread_name)
	{
		for(int i=0;i<10;i++)
    	{
    		System.out.println(thread_name+": "+i);
    		try
    		{
    			Thread.sleep(100);
    		}catch(InterruptedException ie){
    			System.out.println(ie.getMessage());
    		}
    	}
	}

	public static void main(String args[])
	{
		new Thread( new Runnable() {
		    public void run() {

		    	thread_body("child_thread");
		    }
		}).start();

		thread_body("main_thread");
	}
}

You may also like to read:
purpose of synchronized keyword
how to use synchronized with a class
how to use synchronized with a method
what are concurrent collections