wellnwill Marathahalli Bangalore

Nested class


To established relationship between classes, we declare one class or interface inside other class or interface i.e. if one object is strongly dependent to other object. This is called “strong has a” relationship.

We can achieve “week has a” relationship for objects by using “aggregation” concept.


Syntax:

class Outer //outer class
{
 class Inner //inner class
{
// inner class can access all the members of outer class
}
} 

Example 1- There are two objects i.e. car and engine. As we know without engine object there is no use of car object so we can use nested class concept here.

Example 2- There are two objects i.e. company and business unit. As we know without business unit thers is no use of company object so we can use nested class concept here.

A nested class can be declared as public, private, protected, default, abstract, final, strictfp.

We can declare the outer class as default, public, abstract, final, strictfp.

We cannot use static member inside of the inner class.

Simple inner class


A class is said to be simple inner class (non-static nested class) if it is defined directly inside top level class without using static keyword.


A nested class can be declared as public, private, protected, default, abstract, final, strictfp. We can declare the outer class as default, public, abstract, final, strictfp.

We cannot use static member inside of the inner class.

class Car
{
 static String name="jaguar";
 int door;
 int seat; 
 Car(int seat,int door)
 {
  this.seat=seat;
  this.door=door;
 }
 
 class Engine
 {
  int capacity;
  int engine_valve;
  Engine(int capacity,int engine_valve)
  {
   this.capacity=capacity;
   this.engine_valve=engine_valve;
  }
 }


void showDetail(Engine ce)
 {
System.out.println("name:"+name);
System.out.println("seater:"+seat);
System.out.println("engine_capacity:"+ce.capacity);
 }
}

class BuildCar
{
 public static void main(String args[])
 {
  Car c1=new Car(4,4);
  Car.Engine ce1=c1.new Engine(1000,3);
  c1.showDetail(ce1);
 }
}





We can access all the members of Outer class (static/non-static) inside inner class including private member but inner class members cannot be accessed outside inner class.

class Outer
{ 
    static int i = 50;
    int j = 40;
    private float k= 30;
     
    class Inner
    { 
        int l=20;
        void show()
        {
            System.out.println(i);
            System.out.println(j);
            System.out.println(k);
            System.out.println(l);
        }
    }
}
public class Test1
{
    public static void main(String[] args)
    {
       Outer o=new Outer();
       Outer.Inner oi=o.new Inner();
       oi.show();
     }
}


class Outer
{ 
   int i=50;
     
    class Inner
    { 
	  int j=20;
    }
	
   void show()
    {
      System.out.println(i);
      System.out.println(j);
    }
}
 public class Test2
{
    public static void main(String[] args)
    {
       Outer o=new Outer();
	   Outer.Inner oi=o.new Inner();
       o.show();
    }
}
Output-compile time error.



class Outer 
{
 int i=100;
   
   class Inner 
   {

        int i=50;
        void show(Outer o) 
		{
            System.out.println(i);
            System.out.println(o.i);
        }
    }
}

class Test3
{
 public static void main(String args[])
 {
  Outer o=new Outer();
  Outer.Inner oi=o.new Inner();
  oi.show(o);
 }
}



class Outer 
{
 int i=100;
   
   class Inner 
   {
        int i=50;
        void show() 
        {
            System.out.println(i);
            System.out.println(Outer.this.i);
        }
    }
}

class Test4
{
 public static void main(String args[])
 {
  Outer o=new Outer();
  Outer.Inner oi=o.new Inner();
  oi.show(o);
 }
}



class Outer 
{
   class Inner 
   {
      static int i=10;
        void show() 
		{
            System.out.println(i);
        }
    }
}
class Test5
{
 public static void main(String args[])
 {
  Outer o=new Outer();
  Outer.Inner oi=o.new Inner();
  oi.show();
 }
}
Output: compile time error



class Outer 
{
   class Inner 
   {
      static final int i=10;
        void show() 
       {
            System.out.println(i);
        }
    }
}
class Test6
{
 public static void main(String args[])
 {
  Outer o=new Outer();
  Outer.Inner oi=o.new Inner();
  oi.show();
 }
}



Footer with Map