Nested classes in java


Java allows to define a class inside another class, known as nested classes. There are four types of nested classes in java.

class OuterClass{
//define outer class properties and behaviour 
class InnerClass {
//define inner class properties and behaviour 
}
}
  • Static nested classes
  • Non-static nested classes
  • Anonymous classes
  • Local classes

Static nested classes

In the example below : Car is an outer class and whereas Wheel is a static inner class. Both classes has the print method. To create the instance of Wheel you must reference it by prefixing it with the outer class name. Have a look at the main method.

public class Car {

	public void print(){
		System.out.println("Car class : Outer class");
	}
	private static class Wheel{
		
		public void print(){
			System.out.println("Wheel class : Inner class");
		}
	}
	
	public static void main(String args[]){
           //Prefixing with outer class Car, to create the instance of Wheel 
		Car.Wheel w = new Car.Wheel();  
		w.print();
		
		Car c = new Car();
		c.print();
	}
}

Output of the program

Wheel class : Inner class
Car class : Outer class

Non-Static nested classes

Non-static nested classes are also called inner classes. They are associated with the instance of outer class therefore you need to create the instance of outer class to create the instance of inner class.

public class Car {
	private String text = "Car class : Outer class";
	public void print(){
		System.out.println("Car class : Outer class");
		
	}
	private  class Wheel{
		
		public void print(){
        //Non-static nested classes have access to the fields of the outer class
        //text is a data field of outer class and declared as private 
			System.out.println(text);
		}
	}
	
	public static void main(String args[]){
		Car c = new Car(); //Creating instance of outer class
		//creating instance of inner class
		Car.Wheel w = c.new Wheel(); 
		
		//Notic how new is used with the reference of outer class
		w.print();
		
	}
}

Shadowing

If an inner class declares fields or methods with the same names as field or methods in its outer class, the inner fields or methods are said to shadow over the outer fields or methods. Find out the example below:

public class Car {
	private String text = "Car class : Outer class";
	public void print(){
		System.out.println("Car class : Outer class");
		
	}
	private  class Wheel{
		private String text = "Wheel class : Inner class";
		public void print(){
			//calling a shadow method or data field of outer class
			Car.this.print();
			
			System.out.println(Car.this.text);
			System.out.println(text);
		}
	}
	
	public static void main(String args[]){
		Car c = new Car(); //Creating instance of outer class
		//creating instance of inner class
		Car.Wheel w = c.new Wheel(); 
		
		//Notic how new is used with the reference of outer class
		w.print();
		
	}
}

Output of the program shadowing

Car class : Outer class
Car class : Outer class
Wheel class : Inner class

Anonymous classes

A class defined without a class name are known as the anonymous class. Such classes can be defined as an experssion and instatiated on the fly. They are declared as either subclasses of an existing class or as implemenation of some interface. Here is an example that declares an anonymous subclass of a superclass called SuperClass:

public class AnonymousClassDemo {
	public void doIt() {
	    System.out.println("AnonymousclassDemo doIt method");
	  }
	
	public static void main(String args[]){
		AnonymousClassDemo ac = new AnonymousClassDemo(){
			public void doIt(){
				System.out.println("Anonymousclass Overriden doIt method");
			}
		};
		
		ac.doIt();
	}
}

Local classes

Local classes are like inner (non-static nested) classes that can be define in the context of a block as in a method body or a local block, just like as local variables can be defined in a method body and their scope is within the method body.

  • Local classes can only be accessed from inside the method or scope block in which they are defined
  • Local classes can access members (fields and methods) of its enclosing class just like regular inner classes
  • Local classes can also access local variables inside the same method or scope block, provided these variables are declared final
  • The same shadowing rules apply for local classes as for inner classes
public class LocalClassDemo {
public void methodOne(){
	class Local{
		public void method(){
			System.out.println("Local class inside a method");
		}
	}
	
	 new Local().method();
}
public static void main(String args[]){
	LocalClassDemo demo = new LocalClassDemo();
	demo.methodOne();
}
}

No Responses