Inner Class

The Java programming language allows to define a class within another class.
Such a class is called a nested class and is defined as

class OuterClass 
{
...

class NestedClass 
{
...
}
}

Nested classes are divided into two categories:

  • static (Discussed Later)
  • non-static.
  1. Nested classes that are declared static are called static nested classes.
  2. Non-static nested classes are called inner classes.

Example to show inner class implementation!

import java.lang.*;
class  Intrest
{    
    // Nested  class
	 class  Simple
	 {		 
		 double Calculate(double amt,double rate,double time)
		 {
		   double  i=(amt*rate*time)/100;
		   return(i);
		 }	 
	 }	 
	 class  Compound
	 {	 
		 double Calculate(double amt,double rate,double time)
		 {
		   double  i=(amt* Math.pow( (1+(rate/100)), time)) -amt;
		   return(i);
		 }
	 
	 }	
	
}

public class soft 
{

	public  static void main(String  args[])
	{
		Intrest  outer= new Intrest(); 
		
		Intrest.Simple si=  outer.new Simple();
		double  r= si.Calculate(1000, 5, 2);
		System.out.println("Simple Interest::"+r);
		
		Intrest.Compound ci=  outer.new Compound(); 
		r= ci.Calculate(1000, 5, 2);
		System.out.println("Compound Interest::"+r);
		
	}

}

Output is:
Simple Interest::100.0
Compound Interest::102.5